From d1135f776cb62b28a865d289a96193af7889a17f Mon Sep 17 00:00:00 2001 From: gibbz00 Date: Thu, 16 Feb 2023 13:29:00 +0100 Subject: [PATCH] Unify logging HELIX_LOG_LEVEL env variable can now also be set in a non integration test environment. --- helix-term/src/application.rs | 25 ------------------------- helix-term/src/main.rs | 35 ++++++++++++++++++++++------------- helix-term/src/ui/editor.rs | 2 +- 3 files changed, 23 insertions(+), 39 deletions(-) diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index ecf98503ec8c..2d7000d2f8da 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -79,28 +79,6 @@ pub struct Application { last_render: Instant, } -#[cfg(feature = "integration")] -fn setup_integration_logging() { - let level = std::env::var("HELIX_LOG_LEVEL") - .map(|lvl| lvl.parse().unwrap()) - .unwrap_or(log::LevelFilter::Info); - - // Separate file config so we can include year, month and day in file logs - let _ = fern::Dispatch::new() - .format(|out, message, record| { - out.finish(format_args!( - "{} {} [{}] {}", - chrono::Local::now().format("%Y-%m-%dT%H:%M:%S%.3f"), - record.target(), - record.level(), - message - )) - }) - .level(level) - .chain(std::io::stdout()) - .apply(); -} - fn restore_term() -> Result<(), Error> { let mut stdout = stdout(); // reset cursor shape @@ -125,9 +103,6 @@ impl Application { theme: Theme, langauge_configurations: LanguageConfigurations, ) -> Result { - #[cfg(feature = "integration")] - setup_integration_logging(); - #[cfg(not(feature = "integration"))] let backend = CrosstermBackend::new(stdout()); #[cfg(feature = "integration")] diff --git a/helix-term/src/main.rs b/helix-term/src/main.rs index 11584848b2fa..4536a74b96a8 100644 --- a/helix-term/src/main.rs +++ b/helix-term/src/main.rs @@ -97,19 +97,27 @@ async fn main_impl() -> Result { app.run(&mut EventStream::new()).await } -fn setup_logging(logpath: Option, verbosity: u64) -> Result<()> { - helix_loader::setup_log_file(logpath); +fn setup_logging(_logpath: Option, verbosity: u64) -> Result<()> { + let log_level = match verbosity { + 0 => match std::env::var("HELIX_LOG_LEVEL") { + Ok(str) => str.parse()?, + Err(_) => log::LevelFilter::Warn, + }, + 1 => log::LevelFilter::Info, + 2 => log::LevelFilter::Debug, + _3_or_more => log::LevelFilter::Trace, + }; + + #[cfg(feature = "integration")] + let logger: fern::Output = std::io::stdout().into(); - let mut base_config = fern::Dispatch::new(); - base_config = match verbosity { - 0 => base_config.level(log::LevelFilter::Warn), - 1 => base_config.level(log::LevelFilter::Info), - 2 => base_config.level(log::LevelFilter::Debug), - _3_or_more => base_config.level(log::LevelFilter::Trace), + #[cfg(not(feature = "integration"))] + let logger: fern::Output = { + helix_loader::setup_log_file(_logpath); + fern::log_file(helix_loader::log_file())?.into() }; - // Separate file config so we can include year, month and day in file logs - let file_config = fern::Dispatch::new() + fern::Dispatch::new() .format(|out, message, record| { out.finish(format_args!( "{} {} [{}] {}", @@ -119,7 +127,8 @@ fn setup_logging(logpath: Option, verbosity: u64) -> Result<()> { message )) }) - .chain(fern::log_file(helix_loader::log_file())?); - base_config.chain(file_config).apply()?; - Ok(()) + .level(log_level) + .chain(logger) + .apply() + .map_err(|err| anyhow::anyhow!(err)) } diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index fb81ad5a66a5..d31c51dd3301 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -794,7 +794,7 @@ impl EditorView { let mut last_mode = mode; self.pseudo_pending.extend(self.keymap.pending()); let key_result = self.keymap.get(mode, event); - let sort_infobox = cxt.editor.config.load().sorted_infobox; + let sort_infobox = cxt.editor.config().sorted_infobox; cxt.editor.autoinfo = self .keymap .sticky_keytrie()