[PATCH 3 of 6] rust-chg: install logger if $CHGDEBUG is set

Yuya Nishihara yuya at tcha.org
Sun Oct 14 04:18:20 EDT 2018


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1538879562 -32400
#      Sun Oct 07 11:32:42 2018 +0900
# Node ID efdf82aa259682f10c9211e283aceffd861f77cd
# Parent  b46cd0c69558e0aa92871f180d98a167bd108f97
rust-chg: install logger if $CHGDEBUG is set

This is modeled after the example logger and debugmsg() of chg/util.c.

https://docs.rs/log/0.4.5/log/#implementing-a-logger

diff --git a/rust/chg/src/main.rs b/rust/chg/src/main.rs
--- a/rust/chg/src/main.rs
+++ b/rust/chg/src/main.rs
@@ -5,6 +5,7 @@
 
 extern crate chg;
 extern crate futures;
+extern crate log;
 extern crate tokio;
 extern crate tokio_hglib;
 
@@ -15,10 +16,48 @@ use futures::sync::oneshot;
 use std::env;
 use std::io;
 use std::process;
+use std::time::Instant;
 use tokio::prelude::*;
 use tokio_hglib::UnixClient;
 
+struct DebugLogger {
+    start: Instant,
+}
+
+impl DebugLogger {
+    pub fn new() -> DebugLogger {
+        DebugLogger {
+            start: Instant::now(),
+        }
+    }
+}
+
+impl log::Log for DebugLogger {
+    fn enabled(&self, metadata: &log::Metadata) -> bool {
+        metadata.target().starts_with("chg::")
+    }
+
+    fn log(&self, record: &log::Record) {
+        if self.enabled(record.metadata()) {
+            // just make the output looks similar to chg of C
+            let l = format!("{}", record.level()).to_lowercase();
+            let t = self.start.elapsed();
+            writeln!(io::stderr(), "chg: {}: {}.{:06} {}",
+                     l, t.as_secs(), t.subsec_micros(), record.args()).unwrap_or(());
+        }
+    }
+
+    fn flush(&self) {
+    }
+}
+
 fn main() {
+    if env::var_os("CHGDEBUG").is_some() {
+        log::set_boxed_logger(Box::new(DebugLogger::new()))
+            .expect("any logger should not be installed yet");
+        log::set_max_level(log::LevelFilter::Debug);
+    }
+
     let code = run().unwrap_or_else(|err| {
         writeln!(io::stderr(), "chg: abort: {}", err).unwrap_or(());
         255


More information about the Mercurial-devel mailing list