Serialize KVs in CI log
@@ -22,36 +22,62 @@ use time;
use tikv;
+struct Serializer<'a>(&'a mut dyn std::io::Write);
+
+impl<'a> slog::ser::Serializer for Serializer<'a> {
+ fn emit_arguments(&mut self, key: slog::Key, val: &std::fmt::Arguments<'_>) -> slog::Result {
+ write!(self.0, ", {}: {}", key, val)?;
+ Ok(())
+ }
+}
+
/// A logger that add a test case tag before each line of log.
struct CaseTraceLogger {
f: Option<Mutex<File>>,
}
-impl Drain for CaseTraceLogger {
- type Ok = ();
- type Err = slog::Never;
- fn log(&self, record: &Record<'_>, _: &OwnedKVList) -> Result<Self::Ok, Self::Err> {
- let tag = tikv::util::get_tag_from_thread_name().map_or_else(|| "".into(), |s| s + " ");
+impl CaseTraceLogger {
+ fn write_log(
+ w: &mut dyn ::std::io::Write,
+ record: &Record<'_>,
+ values: &OwnedKVList,
+ ) -> Result<(), ::std::io::Error> {
+ use slog::KV;
+ let tag = tikv::util::get_tag_from_thread_name().map_or_else(|| "".to_owned(), |s| s + " ");
let t = time::now();
let time_str = time::strftime("%Y/%m/%d %H:%M:%S.%f", &t).unwrap();
- // todo allow formatter to be configurable.
- let message = format!(
- "{}{} {}:{}: [{}] {}\n",
+ write!(
+ w,
+ "{}{} {}:{}: [{}] {}",
tag,
&time_str[..time_str.len() - 6],
record.file().rsplit('/').nth(0).unwrap(),
record.line(),
record.level(),
record.msg(),
- );
+ )?;
+ {
+ let mut s = Serializer(w);
+ record.kv().serialize(record, &mut s)?;
+ values.serialize(record, &mut s)?;
+ }
+ writeln!(w)?;
+ w.flush()?;
+ Ok(())
+ }
+}
+impl Drain for CaseTraceLogger {
+ type Ok = ();
+ type Err = slog::Never;
+ fn log(&self, record: &Record<'_>, values: &OwnedKVList) -> Result<Self::Ok, Self::Err> {
if let Some(ref out) = self.f {
let mut w = out.lock().unwrap();
- let _ = w.write(message.as_bytes());
+ let _ = Self::write_log(&mut *w, record, values);
} else {
let mut w = io::stderr();
- let _ = w.write(message.as_bytes());
+ let _ = Self::write_log(&mut w, record, values);
}
Ok(())
}
