|
| 1 | +use slog::{Drain, OwnedKVList, Record, KV}; |
| 2 | +use slog_term::{ |
| 3 | + timestamp_local, CompactFormatSerializer, CountingWriter, Decorator, RecordDecorator, |
| 4 | + Serializer, ThreadSafeTimestampFn, |
| 5 | +}; |
| 6 | +use std::cell::RefCell; |
| 7 | +use std::{io, io::Write}; |
| 8 | + |
| 9 | +pub use slog_async::Async; |
| 10 | +pub use slog_term::TermDecorator; |
| 11 | + |
| 12 | +pub struct PrefixedCompactFormat<D> |
| 13 | +where |
| 14 | + D: Decorator, |
| 15 | +{ |
| 16 | + decorator: D, |
| 17 | + history: RefCell<Vec<(Vec<u8>, Vec<u8>)>>, |
| 18 | + fn_timestamp: Box<dyn ThreadSafeTimestampFn<Output = io::Result<()>>>, |
| 19 | + prefix: String, |
| 20 | +} |
| 21 | + |
| 22 | +impl<D> Drain for PrefixedCompactFormat<D> |
| 23 | +where |
| 24 | + D: Decorator, |
| 25 | +{ |
| 26 | + type Ok = (); |
| 27 | + type Err = io::Error; |
| 28 | + |
| 29 | + fn log(&self, record: &Record<'_>, values: &OwnedKVList) -> Result<Self::Ok, Self::Err> { |
| 30 | + self.format_compact(record, values) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +impl<D> PrefixedCompactFormat<D> |
| 35 | +where |
| 36 | + D: Decorator, |
| 37 | +{ |
| 38 | + pub fn new(prefix: &str, d: D) -> PrefixedCompactFormat<D> { |
| 39 | + Self { |
| 40 | + fn_timestamp: Box::new(timestamp_local), |
| 41 | + decorator: d, |
| 42 | + history: RefCell::new(vec![]), |
| 43 | + prefix: prefix.to_owned(), |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + fn format_compact(&self, record: &Record<'_>, values: &OwnedKVList) -> io::Result<()> { |
| 48 | + self.decorator.with_record(record, values, |decorator| { |
| 49 | + let indent = { |
| 50 | + let mut history_ref = self.history.borrow_mut(); |
| 51 | + let mut serializer = CompactFormatSerializer::new(decorator, &mut *history_ref); |
| 52 | + |
| 53 | + values.serialize(record, &mut serializer)?; |
| 54 | + |
| 55 | + serializer.finish()? |
| 56 | + }; |
| 57 | + |
| 58 | + decorator.start_whitespace()?; |
| 59 | + |
| 60 | + for _ in 0..indent { |
| 61 | + write!(decorator, " ")?; |
| 62 | + } |
| 63 | + |
| 64 | + let comma_needed = |
| 65 | + print_msg_header(&self.prefix, &*self.fn_timestamp, decorator, record)?; |
| 66 | + { |
| 67 | + let mut serializer = Serializer::new(decorator, comma_needed, false); |
| 68 | + |
| 69 | + record.kv().serialize(record, &mut serializer)?; |
| 70 | + |
| 71 | + serializer.finish()?; |
| 72 | + } |
| 73 | + |
| 74 | + decorator.start_whitespace()?; |
| 75 | + writeln!(decorator)?; |
| 76 | + |
| 77 | + decorator.flush()?; |
| 78 | + |
| 79 | + Ok(()) |
| 80 | + }) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +pub fn print_msg_header( |
| 85 | + prefix: &str, |
| 86 | + fn_timestamp: &dyn ThreadSafeTimestampFn<Output = io::Result<()>>, |
| 87 | + mut rd: &mut dyn RecordDecorator, |
| 88 | + record: &Record<'_>, |
| 89 | +) -> io::Result<bool> { |
| 90 | + rd.start_timestamp()?; |
| 91 | + fn_timestamp(&mut rd)?; |
| 92 | + |
| 93 | + rd.start_whitespace()?; |
| 94 | + write!(rd, " ")?; |
| 95 | + |
| 96 | + rd.start_level()?; |
| 97 | + write!(rd, "{}", record.level().as_short_str())?; |
| 98 | + |
| 99 | + rd.start_whitespace()?; |
| 100 | + write!(rd, " ")?; |
| 101 | + |
| 102 | + rd.start_msg()?; |
| 103 | + write!(rd, "{}:", prefix)?; |
| 104 | + |
| 105 | + rd.start_whitespace()?; |
| 106 | + write!(rd, " ")?; |
| 107 | + |
| 108 | + rd.start_msg()?; |
| 109 | + let mut count_rd = CountingWriter::new(&mut rd); |
| 110 | + write!(count_rd, "{}", record.msg())?; |
| 111 | + Ok(count_rd.count() != 0) |
| 112 | +} |
0 commit comments