##// END OF EJS Templates
rust-chg: install logger if $CHGDEBUG is set...
Yuya Nishihara -
r40324:87c76e5f default
parent child Browse files
Show More
@@ -5,6 +5,7 b''
5
5
6 extern crate chg;
6 extern crate chg;
7 extern crate futures;
7 extern crate futures;
8 extern crate log;
8 extern crate tokio;
9 extern crate tokio;
9 extern crate tokio_hglib;
10 extern crate tokio_hglib;
10
11
@@ -15,10 +16,48 b' use futures::sync::oneshot;'
15 use std::env;
16 use std::env;
16 use std::io;
17 use std::io;
17 use std::process;
18 use std::process;
19 use std::time::Instant;
18 use tokio::prelude::*;
20 use tokio::prelude::*;
19 use tokio_hglib::UnixClient;
21 use tokio_hglib::UnixClient;
20
22
23 struct DebugLogger {
24 start: Instant,
25 }
26
27 impl DebugLogger {
28 pub fn new() -> DebugLogger {
29 DebugLogger {
30 start: Instant::now(),
31 }
32 }
33 }
34
35 impl log::Log for DebugLogger {
36 fn enabled(&self, metadata: &log::Metadata) -> bool {
37 metadata.target().starts_with("chg::")
38 }
39
40 fn log(&self, record: &log::Record) {
41 if self.enabled(record.metadata()) {
42 // just make the output looks similar to chg of C
43 let l = format!("{}", record.level()).to_lowercase();
44 let t = self.start.elapsed();
45 writeln!(io::stderr(), "chg: {}: {}.{:06} {}",
46 l, t.as_secs(), t.subsec_micros(), record.args()).unwrap_or(());
47 }
48 }
49
50 fn flush(&self) {
51 }
52 }
53
21 fn main() {
54 fn main() {
55 if env::var_os("CHGDEBUG").is_some() {
56 log::set_boxed_logger(Box::new(DebugLogger::new()))
57 .expect("any logger should not be installed yet");
58 log::set_max_level(log::LevelFilter::Debug);
59 }
60
22 let code = run().unwrap_or_else(|err| {
61 let code = run().unwrap_or_else(|err| {
23 writeln!(io::stderr(), "chg: abort: {}", err).unwrap_or(());
62 writeln!(io::stderr(), "chg: abort: {}", err).unwrap_or(());
24 255
63 255
General Comments 0
You need to be logged in to leave comments. Login now