1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#![warn(missing_docs)]
#[macro_use]
extern crate slog;
extern crate serde;
use std::{io, fmt};
use slog::ser;
use std::cell::RefCell;
use std::fmt::Write;
use std::result;
thread_local! {
static TL_BUF: RefCell<String> = RefCell::new(String::with_capacity(128))
}
pub struct SerdeSerializer<S: serde::Serializer>{
ser : S,
map_state : S::MapState,
}
impl<S: serde::Serializer> SerdeSerializer<S> {
pub fn start(mut ser : S) -> result::Result<Self, ser::Error> {
let map_state = try!(
ser.serialize_map(None)
.map_err(|_| io::Error::new(io::ErrorKind::Other, "serde serialization error"))
);
Ok(SerdeSerializer {
ser: ser,
map_state: map_state,
})
}
pub fn end(mut self) -> (S, ser::Result) {
let res = self.ser.serialize_map_end(self.map_state)
.map_err(|_| io::Error::new(io::ErrorKind::Other, "serde serialization error").into());
(self.ser, res)
}
}
macro_rules! impl_m(
($s:expr, $key:expr, $val:expr) => ({
try!(serde::Serializer::serialize_map_key(&mut $s.ser, &mut $s.map_state, $key)
.map_err(|_| io::Error::new(io::ErrorKind::Other, "serde serialization error")));
serde::Serializer::serialize_map_value(&mut $s.ser, &mut $s.map_state, $val)
.map_err(|_| io::Error::new(io::ErrorKind::Other, "serde serialization error").into())
});
);
impl<S> slog::ser::Serializer for SerdeSerializer<S>
where S: serde::Serializer
{
fn emit_bool(&mut self, key: &str, val: bool) -> ser::Result {
impl_m!(self, key, val)
}
fn emit_unit(&mut self, key: &str) -> ser::Result {
impl_m!(self, key, ())
}
fn emit_char(&mut self, key: &str, val: char) -> ser::Result {
impl_m!(self, key, val)
}
fn emit_none(&mut self, key: &str) -> ser::Result {
let val: Option<()> = None;
impl_m!(self, key, val)
}
fn emit_u8(&mut self, key: &str, val: u8) -> ser::Result {
impl_m!(self, key, val)
}
fn emit_i8(&mut self, key: &str, val: i8) -> ser::Result {
impl_m!(self, key, val)
}
fn emit_u16(&mut self, key: &str, val: u16) -> ser::Result {
impl_m!(self, key, val)
}
fn emit_i16(&mut self, key: &str, val: i16) -> ser::Result {
impl_m!(self, key, val)
}
fn emit_usize(&mut self, key: &str, val: usize) -> ser::Result {
impl_m!(self, key, val)
}
fn emit_isize(&mut self, key: &str, val: isize) -> ser::Result {
impl_m!(self, key, val)
}
fn emit_u32(&mut self, key: &str, val: u32) -> ser::Result {
impl_m!(self, key, val)
}
fn emit_i32(&mut self, key: &str, val: i32) -> ser::Result {
impl_m!(self, key, val)
}
fn emit_f32(&mut self, key: &str, val: f32) -> ser::Result {
impl_m!(self, key, val)
}
fn emit_u64(&mut self, key: &str, val: u64) -> ser::Result {
impl_m!(self, key, val)
}
fn emit_i64(&mut self, key: &str, val: i64) -> ser::Result {
impl_m!(self, key, val)
}
fn emit_f64(&mut self, key: &str, val: f64) -> ser::Result {
impl_m!(self, key, val)
}
fn emit_str(&mut self, key: &str, val: &str) -> ser::Result {
impl_m!(self, key, val)
}
fn emit_arguments(&mut self, key: &str, val: &fmt::Arguments) -> ser::Result {
TL_BUF.with(|buf| {
let mut buf = buf.borrow_mut();
buf.write_fmt(*val).unwrap();
let res = {
|| {
impl_m!(self, key, &*buf)
}
}();
buf.clear();
res
})
}
}