diff --git a/src/log.rs b/src/log.rs index 0b0715b..c664e1d 100644 --- a/src/log.rs +++ b/src/log.rs @@ -1,11 +1,13 @@ use slog::{Drain, Logger}; use slog_async; +use slog_async::AsyncGuard; use slog_term::{TermDecorator, FullFormat}; -pub fn init() -> Logger { +pub fn init() -> (Logger, AsyncGuard) { let decorator = TermDecorator::new().build(); let drain = FullFormat::new(decorator).build().fuse(); - let drain = slog_async::Async::new(drain).build().fuse(); + let (drain, guard) = slog_async::Async::new(drain).build_with_guard(); + let drain = drain.fuse(); - return slog::Logger::root(drain, o!()); + return (slog::Logger::root(drain, o!()), guard); } diff --git a/src/main.rs b/src/main.rs index 3e7df9b..5d73524 100644 --- a/src/main.rs +++ b/src/main.rs @@ -115,7 +115,8 @@ fn main() { // on. // TODO: Now would be a really good time to close stdin/out and move logging to syslog // Log is in an Arc so we can do very cheap clones in closures. - let log = Arc::new(log::init()); + let (log, guard) = log::init(); + let log = Arc::new(log); info!(log, "Starting"); match maybe(matches, log.clone()) { @@ -125,9 +126,8 @@ fn main() { retval = -1; } } + drop(guard); } - - std::process::exit(retval); } // Returning a `Result` from `main` allows us to use the `?` shorthand. diff --git a/src/server.rs b/src/server.rs index a50ef99..c51e8fb 100644 --- a/src/server.rs +++ b/src/server.rs @@ -27,11 +27,15 @@ pub fn serve_api_connections(log: Arc, config: Config, db: Databases, nw -> Result<(), Error> { let signal = Box::pin(async { + use signal_hook::consts::signal::*; let (tx, mut rx) = UnixStream::pair()?; // Initialize signal handler. // We currently only care about Ctrl-C so SIGINT it is. // TODO: Make this do SIGHUP and a few others too. (By cloning the tx end of the pipe) - sigpipe::register(signal_hook::consts::SIGINT, tx.as_raw_fd())?; + let fd = tx.as_raw_fd(); + sigpipe::register(SIGINT, fd)?; + sigpipe::register(SIGQUIT, fd)?; + sigpipe::register(SIGTERM, fd)?; // When a signal is received this future can complete and read a byte from the underlying // socket — the actual data is discarded but the act of being able to receive data tells us // that we received a SIGINT.