rust/chg/src/main.rs
changeset 40288 87c76e5f3427
parent 40286 af52181f71ff
child 43836 ce088b38f92b
equal deleted inserted replaced
40287:7623199def92 40288:87c76e5f3427
     3 // This software may be used and distributed according to the terms of the
     3 // This software may be used and distributed according to the terms of the
     4 // GNU General Public License version 2 or any later version.
     4 // GNU General Public License version 2 or any later version.
     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 
    11 use chg::{ChgClientExt, ChgUiHandler};
    12 use chg::{ChgClientExt, ChgUiHandler};
    12 use chg::locator;
    13 use chg::locator;
    13 use chg::procutil;
    14 use chg::procutil;
    14 use futures::sync::oneshot;
    15 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
    25     });
    64     });
    26     process::exit(code);
    65     process::exit(code);