1
1
//! An object that can be used to pass through a channel and be cloned. It can therefore be used
2
2
//! via the broadcast channel.
3
3
4
+ use parking_lot:: Mutex ;
5
+ use serde:: ser:: SerializeMap ;
6
+ use serde:: serde_if_integer128;
7
+ use serde:: Serialize ;
4
8
use slog:: {
5
9
BorrowedKV , Drain , Key , Level , OwnedKVList , Record , RecordStatic , Serializer , SingleKV , KV ,
6
10
} ;
11
+ use std:: cell:: RefCell ;
7
12
use std:: fmt;
13
+ use std:: fmt:: Write ;
14
+ use std:: io;
8
15
use std:: sync:: Arc ;
9
16
use take_mut:: take;
10
17
18
+ thread_local ! {
19
+ static TL_BUF : RefCell <String > = RefCell :: new( String :: with_capacity( 128 ) )
20
+ }
21
+
11
22
/// Serialized record.
12
23
#[ derive( Clone ) ]
13
24
pub struct AsyncRecord {
@@ -16,7 +27,7 @@ pub struct AsyncRecord {
16
27
location : Box < slog:: RecordLocation > ,
17
28
tag : String ,
18
29
logger_values : OwnedKVList ,
19
- kv : Arc < dyn KV + Send > ,
30
+ kv : Arc < Mutex < dyn KV + Send > > ,
20
31
}
21
32
22
33
impl AsyncRecord {
@@ -34,7 +45,7 @@ impl AsyncRecord {
34
45
location : Box :: new ( * record. location ( ) ) ,
35
46
tag : String :: from ( record. tag ( ) ) ,
36
47
logger_values : logger_values. clone ( ) ,
37
- kv : Arc :: new ( ser. finish ( ) ) ,
48
+ kv : Arc :: new ( Mutex :: new ( ser. finish ( ) ) ) ,
38
49
}
39
50
}
40
51
@@ -46,8 +57,9 @@ impl AsyncRecord {
46
57
tag : & self . tag ,
47
58
} ;
48
59
60
+ let kv = self . kv . lock ( ) ;
49
61
drain. log (
50
- & Record :: new ( & rs, & format_args ! ( "{}" , self . msg) , BorrowedKV ( & self . kv ) ) ,
62
+ & Record :: new ( & rs, & format_args ! ( "{}" , self . msg) , BorrowedKV ( & ( * kv ) ) ) ,
51
63
& self . logger_values ,
52
64
)
53
65
}
@@ -60,8 +72,9 @@ impl AsyncRecord {
60
72
tag : & self . tag ,
61
73
} ;
62
74
75
+ let kv = self . kv . lock ( ) ;
63
76
f (
64
- & Record :: new ( & rs, & format_args ! ( "{}" , self . msg) , BorrowedKV ( & self . kv ) ) ,
77
+ & Record :: new ( & rs, & format_args ! ( "{}" , self . msg) , BorrowedKV ( & ( * kv ) ) ) ,
65
78
& self . logger_values ,
66
79
)
67
80
}
@@ -167,11 +180,137 @@ impl Serializer for ToSendSerializer {
167
180
take ( & mut self . kv , |kv| Box :: new ( ( kv, SingleKV ( key, val) ) ) ) ;
168
181
Ok ( ( ) )
169
182
}
183
+ }
170
184
171
- #[ cfg( feature = "nested-values" ) ]
172
- fn emit_serde ( & mut self , key : Key , value : & slog:: SerdeValue ) -> slog:: Result {
173
- let val = value. to_sendable ( ) ;
174
- take ( & mut self . kv , |kv| Box :: new ( ( kv, SingleKV ( key, val) ) ) ) ;
185
+ impl Serialize for AsyncRecord {
186
+ fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
187
+ where
188
+ S : serde:: Serializer ,
189
+ {
190
+ let rs = RecordStatic {
191
+ location : & * self . location ,
192
+ level : self . level ,
193
+ tag : & self . tag ,
194
+ } ;
195
+ let mut map_serializer = SerdeSerializer :: new ( serializer) ?;
196
+ let kv = self . kv . lock ( ) ;
197
+ let message = format_args ! ( "{}" , self . msg) ;
198
+ let record = Record :: new ( & rs, & message, BorrowedKV ( & ( * kv) ) ) ;
199
+
200
+ self . logger_values
201
+ . serialize ( & record, & mut map_serializer)
202
+ . map_err ( |e| serde:: ser:: Error :: custom ( e) ) ?;
203
+ record
204
+ . kv ( )
205
+ . serialize ( & record, & mut map_serializer)
206
+ . map_err ( serde:: ser:: Error :: custom) ?;
207
+ map_serializer. end ( )
208
+ }
209
+ }
210
+
211
+ struct SerdeSerializer < S : serde:: Serializer > {
212
+ /// Current state of map serializing: `serde::Serializer::MapState`
213
+ ser_map : S :: SerializeMap ,
214
+ }
215
+
216
+ impl < S : serde:: Serializer > SerdeSerializer < S > {
217
+ fn new ( ser : S ) -> Result < Self , S :: Error > {
218
+ let ser_map = ser. serialize_map ( None ) ?;
219
+ Ok ( SerdeSerializer { ser_map } )
220
+ }
221
+
222
+ /// Finish serialization, and return the serializer
223
+ fn end ( self ) -> Result < S :: Ok , S :: Error > {
224
+ self . ser_map . end ( )
225
+ }
226
+ }
227
+
228
+ // NOTE: This is borrowed from slog_json
229
+ macro_rules! impl_m(
230
+ ( $s: expr, $key: expr, $val: expr) => ( {
231
+ let k_s: & str = $key. as_ref( ) ;
232
+ $s. ser_map. serialize_entry( k_s, $val)
233
+ . map_err( |e| std:: io:: Error :: new( std:: io:: ErrorKind :: Other , format!( "serde serialization error: {}" , e) ) ) ?;
175
234
Ok ( ( ) )
235
+ } ) ;
236
+ ) ;
237
+
238
+ impl < S > slog:: Serializer for SerdeSerializer < S >
239
+ where
240
+ S : serde:: Serializer ,
241
+ {
242
+ fn emit_bool ( & mut self , key : Key , val : bool ) -> slog:: Result {
243
+ impl_m ! ( self , key, & val)
244
+ }
245
+
246
+ fn emit_unit ( & mut self , key : Key ) -> slog:: Result {
247
+ impl_m ! ( self , key, & ( ) )
248
+ }
249
+
250
+ fn emit_char ( & mut self , key : Key , val : char ) -> slog:: Result {
251
+ impl_m ! ( self , key, & val)
252
+ }
253
+
254
+ fn emit_none ( & mut self , key : Key ) -> slog:: Result {
255
+ let val: Option < ( ) > = None ;
256
+ impl_m ! ( self , key, & val)
257
+ }
258
+ fn emit_u8 ( & mut self , key : Key , val : u8 ) -> slog:: Result {
259
+ impl_m ! ( self , key, & val)
260
+ }
261
+ fn emit_i8 ( & mut self , key : Key , val : i8 ) -> slog:: Result {
262
+ impl_m ! ( self , key, & val)
263
+ }
264
+ fn emit_u16 ( & mut self , key : Key , val : u16 ) -> slog:: Result {
265
+ impl_m ! ( self , key, & val)
266
+ }
267
+ fn emit_i16 ( & mut self , key : Key , val : i16 ) -> slog:: Result {
268
+ impl_m ! ( self , key, & val)
269
+ }
270
+ fn emit_usize ( & mut self , key : Key , val : usize ) -> slog:: Result {
271
+ impl_m ! ( self , key, & val)
272
+ }
273
+ fn emit_isize ( & mut self , key : Key , val : isize ) -> slog:: Result {
274
+ impl_m ! ( self , key, & val)
275
+ }
276
+ fn emit_u32 ( & mut self , key : Key , val : u32 ) -> slog:: Result {
277
+ impl_m ! ( self , key, & val)
278
+ }
279
+ fn emit_i32 ( & mut self , key : Key , val : i32 ) -> slog:: Result {
280
+ impl_m ! ( self , key, & val)
281
+ }
282
+ fn emit_f32 ( & mut self , key : Key , val : f32 ) -> slog:: Result {
283
+ impl_m ! ( self , key, & val)
284
+ }
285
+ fn emit_u64 ( & mut self , key : Key , val : u64 ) -> slog:: Result {
286
+ impl_m ! ( self , key, & val)
287
+ }
288
+ fn emit_i64 ( & mut self , key : Key , val : i64 ) -> slog:: Result {
289
+ impl_m ! ( self , key, & val)
290
+ }
291
+ fn emit_f64 ( & mut self , key : Key , val : f64 ) -> slog:: Result {
292
+ impl_m ! ( self , key, & val)
293
+ }
294
+ serde_if_integer128 ! {
295
+ fn emit_u128( & mut self , key: Key , val: u128 ) -> slog:: Result {
296
+ impl_m!( self , key, & val)
297
+ }
298
+ fn emit_i128( & mut self , key: Key , val: i128 ) -> slog:: Result {
299
+ impl_m!( self , key, & val)
300
+ }
301
+ }
302
+ fn emit_str ( & mut self , key : Key , val : & str ) -> slog:: Result {
303
+ impl_m ! ( self , key, & val)
304
+ }
305
+ fn emit_arguments ( & mut self , key : Key , val : & fmt:: Arguments ) -> slog:: Result {
306
+ TL_BUF . with ( |buf| {
307
+ let mut buf = buf. borrow_mut ( ) ;
308
+
309
+ buf. write_fmt ( * val) . unwrap ( ) ;
310
+
311
+ let res = { || impl_m ! ( self , key, & * buf) } ( ) ;
312
+ buf. clear ( ) ;
313
+ res
314
+ } )
176
315
}
177
316
}
0 commit comments