Show More
@@ -1,59 +1,98 b'' | |||
|
1 | 1 | // Copyright 2018 Yuya Nishihara <yuya@tcha.org> |
|
2 | 2 | // |
|
3 | 3 | // This software may be used and distributed according to the terms of the |
|
4 | 4 | // GNU General Public License version 2 or any later version. |
|
5 | 5 | |
|
6 | 6 | extern crate chg; |
|
7 | 7 | extern crate futures; |
|
8 | extern crate log; | |
|
8 | 9 | extern crate tokio; |
|
9 | 10 | extern crate tokio_hglib; |
|
10 | 11 | |
|
11 | 12 | use chg::{ChgClientExt, ChgUiHandler}; |
|
12 | 13 | use chg::locator; |
|
13 | 14 | use chg::procutil; |
|
14 | 15 | use futures::sync::oneshot; |
|
15 | 16 | use std::env; |
|
16 | 17 | use std::io; |
|
17 | 18 | use std::process; |
|
19 | use std::time::Instant; | |
|
18 | 20 | use tokio::prelude::*; |
|
19 | 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 | 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 | 61 | let code = run().unwrap_or_else(|err| { |
|
23 | 62 | writeln!(io::stderr(), "chg: abort: {}", err).unwrap_or(()); |
|
24 | 63 | 255 |
|
25 | 64 | }); |
|
26 | 65 | process::exit(code); |
|
27 | 66 | } |
|
28 | 67 | |
|
29 | 68 | fn run() -> io::Result<i32> { |
|
30 | 69 | let current_dir = env::current_dir()?; |
|
31 | 70 | let sock_path = locator::prepare_server_socket_path()?; |
|
32 | 71 | let handler = ChgUiHandler::new(); |
|
33 | 72 | let (result_tx, result_rx) = oneshot::channel(); |
|
34 | 73 | let fut = UnixClient::connect(sock_path) |
|
35 | 74 | .and_then(|client| { |
|
36 | 75 | client.set_current_dir(current_dir) |
|
37 | 76 | }) |
|
38 | 77 | .and_then(|client| { |
|
39 | 78 | client.attach_io(io::stdin(), io::stdout(), io::stderr()) |
|
40 | 79 | }) |
|
41 | 80 | .and_then(|client| { |
|
42 | 81 | let pid = client.server_spec().process_id.unwrap(); |
|
43 | 82 | let pgid = client.server_spec().process_group_id; |
|
44 | 83 | procutil::setup_signal_handler_once(pid, pgid)?; |
|
45 | 84 | Ok(client) |
|
46 | 85 | }) |
|
47 | 86 | .and_then(|client| { |
|
48 | 87 | client.run_command_chg(handler, env::args_os().skip(1)) |
|
49 | 88 | }) |
|
50 | 89 | .map(|(_client, _handler, code)| { |
|
51 | 90 | procutil::restore_signal_handler_once()?; |
|
52 | 91 | Ok(code) |
|
53 | 92 | }) |
|
54 | 93 | .or_else(|err| Ok(Err(err))) // pass back error to caller |
|
55 | 94 | .map(|res| result_tx.send(res).unwrap()); |
|
56 | 95 | tokio::run(fut); |
|
57 | 96 | result_rx.wait().unwrap_or(Err(io::Error::new(io::ErrorKind::Other, |
|
58 | 97 | "no exit code set"))) |
|
59 | 98 | } |
General Comments 0
You need to be logged in to leave comments.
Login now