Show More
@@ -1,59 +1,98 b'' | |||||
1 | // Copyright 2018 Yuya Nishihara <yuya@tcha.org> |
|
1 | // Copyright 2018 Yuya Nishihara <yuya@tcha.org> | |
2 | // |
|
2 | // | |
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); | |
27 | } |
|
66 | } | |
28 |
|
67 | |||
29 | fn run() -> io::Result<i32> { |
|
68 | fn run() -> io::Result<i32> { | |
30 | let current_dir = env::current_dir()?; |
|
69 | let current_dir = env::current_dir()?; | |
31 | let sock_path = locator::prepare_server_socket_path()?; |
|
70 | let sock_path = locator::prepare_server_socket_path()?; | |
32 | let handler = ChgUiHandler::new(); |
|
71 | let handler = ChgUiHandler::new(); | |
33 | let (result_tx, result_rx) = oneshot::channel(); |
|
72 | let (result_tx, result_rx) = oneshot::channel(); | |
34 | let fut = UnixClient::connect(sock_path) |
|
73 | let fut = UnixClient::connect(sock_path) | |
35 | .and_then(|client| { |
|
74 | .and_then(|client| { | |
36 | client.set_current_dir(current_dir) |
|
75 | client.set_current_dir(current_dir) | |
37 | }) |
|
76 | }) | |
38 | .and_then(|client| { |
|
77 | .and_then(|client| { | |
39 | client.attach_io(io::stdin(), io::stdout(), io::stderr()) |
|
78 | client.attach_io(io::stdin(), io::stdout(), io::stderr()) | |
40 | }) |
|
79 | }) | |
41 | .and_then(|client| { |
|
80 | .and_then(|client| { | |
42 | let pid = client.server_spec().process_id.unwrap(); |
|
81 | let pid = client.server_spec().process_id.unwrap(); | |
43 | let pgid = client.server_spec().process_group_id; |
|
82 | let pgid = client.server_spec().process_group_id; | |
44 | procutil::setup_signal_handler_once(pid, pgid)?; |
|
83 | procutil::setup_signal_handler_once(pid, pgid)?; | |
45 | Ok(client) |
|
84 | Ok(client) | |
46 | }) |
|
85 | }) | |
47 | .and_then(|client| { |
|
86 | .and_then(|client| { | |
48 | client.run_command_chg(handler, env::args_os().skip(1)) |
|
87 | client.run_command_chg(handler, env::args_os().skip(1)) | |
49 | }) |
|
88 | }) | |
50 | .map(|(_client, _handler, code)| { |
|
89 | .map(|(_client, _handler, code)| { | |
51 | procutil::restore_signal_handler_once()?; |
|
90 | procutil::restore_signal_handler_once()?; | |
52 | Ok(code) |
|
91 | Ok(code) | |
53 | }) |
|
92 | }) | |
54 | .or_else(|err| Ok(Err(err))) // pass back error to caller |
|
93 | .or_else(|err| Ok(Err(err))) // pass back error to caller | |
55 | .map(|res| result_tx.send(res).unwrap()); |
|
94 | .map(|res| result_tx.send(res).unwrap()); | |
56 | tokio::run(fut); |
|
95 | tokio::run(fut); | |
57 | result_rx.wait().unwrap_or(Err(io::Error::new(io::ErrorKind::Other, |
|
96 | result_rx.wait().unwrap_or(Err(io::Error::new(io::ErrorKind::Other, | |
58 | "no exit code set"))) |
|
97 | "no exit code set"))) | |
59 | } |
|
98 | } |
General Comments 0
You need to be logged in to leave comments.
Login now