1
- #[ cfg( not( feature = "spdlog-rs" ) ) ]
2
- use std:: collections:: BTreeMap ;
3
- #[ cfg( feature = "spdlog-rs" ) ]
4
1
use std:: fmt:: { self , Write } ;
5
-
6
2
use std:: { borrow:: Cow , time:: Duration } ;
7
3
8
- #[ cfg( feature = "spdlog-rs" ) ]
9
4
use chrono:: { DateTime , SecondsFormat , Utc } ;
10
5
use console:: { Color , StyledObject , style} ;
11
- #[ cfg( feature = "spdlog-rs" ) ]
12
6
use spdlog:: { Level , debug, formatter:: Formatter } ;
13
7
use termbg:: Theme ;
14
- #[ cfg( not( feature = "spdlog-rs" ) ) ]
15
- use tracing:: { Level , debug, field:: Field } ;
16
- #[ cfg( not( feature = "spdlog-rs" ) ) ]
17
- use tracing_subscriber:: Layer ;
18
8
19
9
pub use termbg;
20
10
21
- #[ cfg( feature = "spdlog-rs" ) ]
22
11
use crate :: writer:: gen_prefix;
23
- #[ cfg( not( feature = "spdlog-rs" ) ) ]
24
- use crate :: writer:: { Writeln , Writer } ;
25
12
26
13
#[ derive( Clone ) ]
27
14
enum StyleFollow {
@@ -146,7 +133,25 @@ fn term_color<D>(input: D, color: Action) -> StyledObject<D> {
146
133
}
147
134
}
148
135
149
- #[ cfg( feature = "spdlog-rs" ) ]
136
+ /// OmaFormatter
137
+ /// `OmaFormatter` is used for outputting oma-style logs to `spdlog-rs`
138
+ ///
139
+ /// # Example:
140
+ /// ```
141
+ /// use spdlog::{info, sink::StdStreamSink, Logger};
142
+ /// use oma_console::OmaLayer;
143
+ ///
144
+ /// let mut logger_builder = Logger::builder();
145
+ ///
146
+ /// let logger = logger_builder.sink(Arc::new(
147
+ /// StdStreamSink::builder().formatter(Box::new(OmaLayer::default())).build().unwrap()
148
+ /// )).build().unwrap();
149
+ ///
150
+ /// spdlog::set_default_logger(Arc::new(logger));
151
+ ///
152
+ /// info!("My name is oma!");
153
+ /// ```
154
+ ///
150
155
#[ derive( Clone ) ]
151
156
pub struct OmaFormatter {
152
157
with_ansi : bool ,
@@ -155,7 +160,6 @@ pub struct OmaFormatter {
155
160
prefix_len : u16 ,
156
161
}
157
162
158
- #[ cfg( feature = "spdlog-rs" ) ]
159
163
impl Default for OmaFormatter {
160
164
fn default ( ) -> Self {
161
165
Self {
@@ -167,7 +171,6 @@ impl Default for OmaFormatter {
167
171
}
168
172
}
169
173
170
- #[ cfg( feature = "spdlog-rs" ) ]
171
174
impl OmaFormatter {
172
175
pub fn new ( ) -> Self {
173
176
OmaFormatter :: default ( )
@@ -267,7 +270,6 @@ impl OmaFormatter {
267
270
}
268
271
}
269
272
270
- #[ cfg( feature = "spdlog-rs" ) ]
271
273
impl Formatter for OmaFormatter {
272
274
fn format (
273
275
& self ,
@@ -279,147 +281,3 @@ impl Formatter for OmaFormatter {
279
281
. map_err ( |e| spdlog:: Error :: FormatRecord ( e) )
280
282
}
281
283
}
282
-
283
- /// OmaLayer
284
- /// `OmaLayer` is used for outputting oma-style logs to `tracing`
285
- ///
286
- /// # Example:
287
- /// ```
288
- /// use tracing_subscriber::prelude::*;
289
- /// use oma_console::OmaLayer;
290
- /// use tracing::info;
291
- ///
292
- /// tracing_subscriber::registry()
293
- /// .with(OmaLayer::new())
294
- /// .init();
295
- ///
296
- /// info!("My name is oma!");
297
- /// ```
298
- ///
299
- #[ cfg( not( feature = "spdlog-rs" ) ) ]
300
- pub struct OmaLayer {
301
- /// Display result with ansi
302
- with_ansi : bool ,
303
- /// A Terminal writer to print oma-style message
304
- writer : Writer ,
305
- }
306
-
307
- #[ cfg( not( feature = "spdlog-rs" ) ) ]
308
- impl Default for OmaLayer {
309
- fn default ( ) -> Self {
310
- Self {
311
- with_ansi : true ,
312
- #[ cfg( not( feature = "spdlog-rs" ) ) ]
313
- writer : Writer :: default ( ) ,
314
- }
315
- }
316
- }
317
-
318
- #[ cfg( not( feature = "spdlog-rs" ) ) ]
319
- impl OmaLayer {
320
- pub fn new ( ) -> Self {
321
- OmaLayer :: default ( )
322
- }
323
-
324
- /// Display with ANSI colors
325
- ///
326
- /// Set to false to disable ANSI color sequences.
327
- pub fn with_ansi ( mut self , with_ansi : bool ) -> Self {
328
- self . with_ansi = with_ansi;
329
- self
330
- }
331
- }
332
-
333
- #[ cfg( not( feature = "spdlog-rs" ) ) ]
334
- impl < S > Layer < S > for OmaLayer
335
- where
336
- S : tracing:: Subscriber ,
337
- S : for < ' lookup > tracing_subscriber:: registry:: LookupSpan < ' lookup > ,
338
- {
339
- fn on_event (
340
- & self ,
341
- event : & tracing:: Event < ' _ > ,
342
- _ctx : tracing_subscriber:: layer:: Context < ' _ , S > ,
343
- ) {
344
- let level = * event. metadata ( ) . level ( ) ;
345
-
346
- let prefix = if self . with_ansi {
347
- Cow :: Owned ( match level {
348
- Level :: DEBUG => console:: style ( "DEBUG" ) . dim ( ) . to_string ( ) ,
349
- Level :: INFO => console:: style ( "INFO" ) . blue ( ) . bold ( ) . to_string ( ) ,
350
- Level :: WARN => console:: style ( "WARNING" ) . yellow ( ) . bold ( ) . to_string ( ) ,
351
- Level :: ERROR => console:: style ( "ERROR" ) . red ( ) . bold ( ) . to_string ( ) ,
352
- Level :: TRACE => console:: style ( "TRACE" ) . dim ( ) . to_string ( ) ,
353
- } )
354
- } else {
355
- Cow :: Borrowed ( match level {
356
- Level :: DEBUG => "DEBUG" ,
357
- Level :: INFO => "INFO" ,
358
- Level :: WARN => "WARNING" ,
359
- Level :: ERROR => "ERROR" ,
360
- Level :: TRACE => "TRACE" ,
361
- } )
362
- } ;
363
-
364
- let mut visitor = OmaRecorder ( BTreeMap :: new ( ) ) ;
365
- event. record ( & mut visitor) ;
366
-
367
- for ( k, v) in visitor. 0 {
368
- if k == "message" {
369
- if self . with_ansi {
370
- self . writer . writeln ( & prefix, & v) . ok ( ) ;
371
- } else {
372
- self . writer
373
- . writeln ( & prefix, & console:: strip_ansi_codes ( & v) )
374
- . ok ( ) ;
375
- }
376
- }
377
- }
378
- }
379
- }
380
- /// OmaRecorder
381
- /// `OmaRecorder` is used for recording oma-style logs.
382
- ///
383
- /// # Example:
384
- /// ```ignore
385
- /// let mut visitor = OmaRecorder(BTreeMap::new());
386
- /// event.record(&mut visitor);
387
- /// for (k, v) in visitor.0 {
388
- /// if k == "message" {
389
- /// self.writer.writeln(&prefix, &v).ok();
390
- /// }
391
- /// }
392
- /// ```
393
- #[ cfg( not( feature = "spdlog-rs" ) ) ]
394
- struct OmaRecorder < ' a > ( BTreeMap < & ' a str , String > ) ;
395
-
396
- #[ cfg( not( feature = "spdlog-rs" ) ) ]
397
- impl tracing:: field:: Visit for OmaRecorder < ' _ > {
398
- fn record_f64 ( & mut self , field : & Field , value : f64 ) {
399
- self . 0 . insert ( field. name ( ) , value. to_string ( ) ) ;
400
- }
401
-
402
- fn record_i64 ( & mut self , field : & Field , value : i64 ) {
403
- self . 0 . insert ( field. name ( ) , value. to_string ( ) ) ;
404
- }
405
-
406
- fn record_u64 ( & mut self , field : & Field , value : u64 ) {
407
- self . 0 . insert ( field. name ( ) , value. to_string ( ) ) ;
408
- }
409
-
410
- fn record_bool ( & mut self , field : & Field , value : bool ) {
411
- self . 0 . insert ( field. name ( ) , value. to_string ( ) ) ;
412
- }
413
-
414
- fn record_str ( & mut self , field : & Field , value : & str ) {
415
- self . 0 . insert ( field. name ( ) , value. to_string ( ) ) ;
416
- }
417
-
418
- fn record_error ( & mut self , field : & Field , value : & ( dyn std:: error:: Error + ' static ) ) {
419
- self . 0 . insert ( field. name ( ) , format ! ( "{value:#?}" ) ) ;
420
- }
421
-
422
- fn record_debug ( & mut self , field : & Field , value : & dyn std:: fmt:: Debug ) {
423
- self . 0 . insert ( field. name ( ) , format ! ( "{value:#?}" ) ) ;
424
- }
425
- }
0 commit comments