Skip to content

Commit

Permalink
Unify logging
Browse files Browse the repository at this point in the history
HELIX_LOG_LEVEL env variable can now also be set in a non integration
test environment.
  • Loading branch information
gibbz00 committed Feb 16, 2023
1 parent 95b227a commit 7e45727
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 38 deletions.
25 changes: 0 additions & 25 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -125,9 +103,6 @@ impl Application {
theme: Theme,
langauge_configurations: LanguageConfigurations,
) -> Result<Self, Error> {
#[cfg(feature = "integration")]
setup_integration_logging();

#[cfg(not(feature = "integration"))]
let backend = CrosstermBackend::new(stdout());
#[cfg(feature = "integration")]
Expand Down
32 changes: 20 additions & 12 deletions helix-term/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,25 @@ async fn main_impl() -> Result<i32> {
}

fn setup_logging(logpath: Option<PathBuf>, verbosity: u64) -> Result<()> {
helix_loader::setup_log_file(logpath);
let log_level = match verbosity {
0 => std::env::var("HELIX_LOG_LEVEL")?
.parse()
.unwrap_or(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!(
"{} {} [{}] {}",
Expand All @@ -119,7 +126,8 @@ fn setup_logging(logpath: Option<PathBuf>, 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))
}
2 changes: 1 addition & 1 deletion helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 7e45727

Please sign in to comment.