diff --git a/Cargo.lock b/Cargo.lock index 73ad595e..d2c64d59 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -195,7 +195,6 @@ version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" dependencies = [ - "humantime", "log", ] @@ -339,15 +338,6 @@ dependencies = [ "libc", ] -[[package]] -name = "humantime" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f" -dependencies = [ - "quick-error", -] - [[package]] name = "iovec" version = "0.1.4" @@ -359,9 +349,9 @@ dependencies = [ [[package]] name = "js-sys" -version = "0.3.44" +version = "0.3.45" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85a7e2c92a4804dd459b86c339278d0fe87cf93757fae222c3fa3ae75458bc73" +checksum = "ca059e81d9486668f12d455a4ea6daa600bd408134cd17e3d3fb5a32d1f016f8" dependencies = [ "wasm-bindgen", ] @@ -755,12 +745,6 @@ dependencies = [ "unicode-xid", ] -[[package]] -name = "quick-error" -version = "1.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" - [[package]] name = "quote" version = "1.0.7" @@ -1257,9 +1241,9 @@ checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" [[package]] name = "wasm-bindgen" -version = "0.2.67" +version = "0.2.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0563a9a4b071746dd5aedbc3a28c6fe9be4586fb3fbadb67c400d4f53c6b16c" +checksum = "1ac64ead5ea5f05873d7c12b545865ca2b8d28adfc50a49b84770a3a97265d42" dependencies = [ "cfg-if", "wasm-bindgen-macro", @@ -1267,9 +1251,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.67" +version = "0.2.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc71e4c5efa60fb9e74160e89b93353bc24059999c0ae0fb03affc39770310b0" +checksum = "f22b422e2a757c35a73774860af8e112bff612ce6cb604224e8e47641a9e4f68" dependencies = [ "bumpalo", "lazy_static", @@ -1282,9 +1266,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.67" +version = "0.2.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97c57cefa5fa80e2ba15641578b44d36e7a64279bc5ed43c6dbaf329457a2ed2" +checksum = "6b13312a745c08c469f0b292dd2fcd6411dba5f7160f593da6ef69b64e407038" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -1292,9 +1276,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.67" +version = "0.2.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "841a6d1c35c6f596ccea1f82504a192a60378f64b3bb0261904ad8f2f5657556" +checksum = "f249f06ef7ee334cc3b8ff031bfc11ec99d00f34d86da7498396dc1e3b1498fe" dependencies = [ "proc-macro2", "quote", @@ -1305,15 +1289,15 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.67" +version = "0.2.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93b162580e34310e5931c4b792560108b10fd14d64915d7fff8ff00180e70092" +checksum = "1d649a3145108d7d3fbcde896a468d1bd636791823c9921135218ad89be08307" [[package]] name = "web-sys" -version = "0.3.44" +version = "0.3.45" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dda38f4e5ca63eda02c059d243aa25b5f35ab98451e518c51612cd0f1bd19a47" +checksum = "4bf6ef87ad7ae8008e15a355ce696bed26012b7caa21605188cfd8214ab51e2d" dependencies = [ "js-sys", "wasm-bindgen", diff --git a/tiny/Cargo.toml b/tiny/Cargo.toml index 5a3ab1d6..4538eb16 100644 --- a/tiny/Cargo.toml +++ b/tiny/Cargo.toml @@ -17,7 +17,7 @@ desktop-notifications = ["libtiny_tui/desktop-notifications"] [dependencies] clap = "2.33" dirs = "1.0.2" -env_logger = { version = "0.7", default-features = false, features = ["humantime"] } +env_logger = { version = "0.7", default-features = false } futures = { version = "0.3.1", default-features = false } libtiny_client = { path = "../libtiny_client", default-features = false } libtiny_logger = { path = "../libtiny_logger" } diff --git a/tiny/src/debug_logging.rs b/tiny/src/debug_logging.rs new file mode 100644 index 00000000..dc4be57d --- /dev/null +++ b/tiny/src/debug_logging.rs @@ -0,0 +1,91 @@ +//! This module provides a logger (as in `log` and `env_logger` crates, rather than +//! `libtiny_logger`) implementation for logging to a file rather than stdout/stderr (which is what +//! `env_logger` provides). +//! +//! Some notes regarding implementation: +//! +//! - All IO errors ignored. Once initialized the logger never panics. +//! - TINY_LOG is the env variable used for setting filters. +//! - Filter syntax is unchanged (same as `env_logger` syntax). +//! - Log file is created when logging for the first time. + +use env_logger::filter::{self, Filter}; +use log::{Log, Record}; + +use std::fs::File; +use std::io::Write; +use std::mem::replace; +use std::path::PathBuf; +use std::sync::Mutex; + +pub(crate) fn init(path: PathBuf) { + let filter = filter::Builder::from_env("TINY_LOG").build(); + let sink = Mutex::new(LazyFile::new(path)); + + log::set_max_level(filter.filter()); + log::set_boxed_logger(Box::new(Logger { sink, filter })).unwrap(); +} + +struct Logger { + sink: Mutex, + filter: Filter, +} + +impl Log for Logger { + fn enabled(&self, metadata: &log::Metadata) -> bool { + self.filter.enabled(metadata) + } + + fn log(&self, record: &Record) { + if !self.filter.matches(record) { + return; + } + + self.sink.lock().unwrap().with_file(|file| { + let _ = writeln!( + file, + "[{}] {} [{}:{}] {}", + time::strftime("%F %T", &time::now()).unwrap(), + record.level(), + record.file().unwrap_or_default(), + record.line().unwrap_or_default(), + record.args() + ); + }); + } + + fn flush(&self) {} +} + +enum LazyFile { + NotOpen(PathBuf), + Open(File), + Error, +} + +impl LazyFile { + fn new(path: PathBuf) -> Self { + LazyFile::NotOpen(path) + } + + fn with_file(&mut self, f: F) + where + F: Fn(&mut File), + { + let mut file = match replace(self, LazyFile::Error) { + LazyFile::NotOpen(path) => match File::create(path) { + Ok(file) => file, + Err(_) => { + return; + } + }, + LazyFile::Open(file) => file, + LazyFile::Error => { + return; + } + }; + + f(&mut file); + *self = LazyFile::Open(file); + } +} diff --git a/tiny/src/main.rs b/tiny/src/main.rs index bb95ed07..7e4cf1bd 100644 --- a/tiny/src/main.rs +++ b/tiny/src/main.rs @@ -4,6 +4,7 @@ mod cli; mod cmd; mod config; mod conn; +mod debug_logging; mod ui; mod utils; @@ -12,7 +13,6 @@ use libtiny_logger::{Logger, LoggerInitError}; use libtiny_tui::{MsgTarget, TUI}; use libtiny_ui::UI; -use std::io::Write; use std::path::PathBuf; use std::process::exit; @@ -68,28 +68,23 @@ fn main() { } } +const DEBUG_LOG_FILE: &str = "tiny_debug_logs.txt"; + fn run( servers: Vec, defaults: config::Defaults, config_path: PathBuf, log_dir: Option, ) { - env_logger::builder() - .target(env_logger::Target::Stderr) - .format(|buf, record| { - let timestamp = buf.timestamp_seconds(); - - writeln!( - buf, - "[{}] {} [{}:{}] {}", - timestamp, - record.level(), - record.file().unwrap_or_default(), - record.line().unwrap_or_default(), - record.args() - ) - }) - .init(); + let debug_log_file = match log_dir.as_ref() { + Some(log_dir) => { + let mut log_dir = log_dir.clone(); + log_dir.push(DEBUG_LOG_FILE); + log_dir + } + None => DEBUG_LOG_FILE.into(), + }; + debug_logging::init(debug_log_file); // One task for each client to handle IRC events // One task for TUI events