Index: Cargo.toml ================================================================== --- Cargo.toml +++ Cargo.toml @@ -1,8 +1,8 @@ [package] name = "qsu" -version = "0.4.1" +version = "0.5.0" edition = "2021" license = "0BSD" # https://crates.io/category_slugs categories = [ "os" ] keywords = [ "service", "systemd", "winsvc" ] Index: src/lumberjack.rs ================================================================== --- src/lumberjack.rs +++ src/lumberjack.rs @@ -1,15 +1,15 @@ use std::{ - fmt, fs, + env, fmt, fs, io::Write, path::{Path, PathBuf}, str::FromStr }; use time::macros::format_description; -use tracing_subscriber::{fmt::time::UtcTime, FmtSubscriber}; +use tracing_subscriber::{fmt::time::UtcTime, EnvFilter}; #[cfg(feature = "clap")] use clap::ValueEnum; use crate::err::Error; @@ -27,11 +27,11 @@ /// Logging and tracing initialization. pub struct LumberJack { init: bool, log_out: LogOut, log_level: LogLevel, - trace_level: LogLevel, + trace_filter: Option, //log_file: Option, trace_file: Option } impl Default for LumberJack { @@ -39,12 +39,12 @@ /// /// This will set the `log` log level to the value of the `LOG_LEVEL` /// environment variable, or default to `warm` (if either not set or /// invalid). /// - /// The `tracing` trace level will use the environment variable `TRACE_LEVEL` - /// in a similar manner, but defaults to `off`. + /// The `tracing` trace level will use the environment variable + /// `TRACE_FILTER` in a similar manner, but defaults to `none`. /// /// If the environment variable `TRACE_FILE` is set the value will be the /// used as the file name to write the trace logs to. fn default() -> Self { let log_level = if let Ok(level) = std::env::var("LOG_LEVEL") { @@ -61,25 +61,21 @@ Some(PathBuf::from(v)) } else { None }; - let trace_level = if let Ok(level) = std::env::var("TRACE_LEVEL") { - if let Ok(level) = level.parse::() { - level - } else { - LogLevel::Off - } - } else { - LogLevel::Off + let trace_filter = if let Ok(var) = env::var("TRACE_FILTER") { + Some(var.to_string()) + } else { + None }; Self { init: true, log_out: Default::default(), log_level, - trace_level, + trace_filter, //log_file: None, trace_file } } } @@ -112,24 +108,23 @@ let loglevel = params .get_value::("LogLevel") .unwrap_or(String::from("warn")) .parse::() .unwrap_or(LogLevel::Warn); - let tracelevel = params.get_value::("TraceLevel"); + let tracefilter = params.get_value::("TraceFilter"); let tracefile = params.get_value::("TraceFile"); let mut this = Self::new().log_level(loglevel); this.log_out = LogOut::WinEvtLog { svcname: svcname.to_string() }; - let this = if let (Ok(tracelevel), Ok(tracefile)) = (tracelevel, tracefile) - { - let tracelevel = tracelevel.parse::().unwrap_or(LogLevel::Off); - this.trace_level(tracelevel).trace_file(tracefile) - } else { - this - }; + let this = + if let (Ok(tracefilter), Ok(tracefile)) = (tracefilter, tracefile) { + this.trace_filter(tracefilter).trace_file(tracefile) + } else { + this + }; Ok(this) } /// Set the `log` logging level. @@ -137,12 +132,12 @@ self.log_level = level; self } /// Set the `tracing` log level. - pub fn trace_level(mut self, level: LogLevel) -> Self { - self.trace_level = level; + pub fn trace_filter(mut self, filter: impl ToString) -> Self { + self.trace_filter = Some(filter.to_string()); self } /// Set a file to which `tracing` log entries are written (rather than to /// write to console). @@ -167,13 +162,13 @@ log::set_max_level(self.log_level.into()); } } if let Some(fname) = self.trace_file { - init_file_tracing(fname, self.trace_level); + init_file_tracing(fname, self.trace_filter.as_deref()); } else { - init_console_tracing(self.trace_level); + init_console_tracing(self.trace_filter.as_deref()); } Ok(()) } else { Ok(()) @@ -305,39 +300,49 @@ Ok(()) } -pub fn init_console_tracing(level: LogLevel) { - let lvl: Option = level.into(); - +pub fn init_console_tracing(filter: Option<&str>) { + /* let subscriber = FmtSubscriber::builder().with_max_level(lvl).finish(); tracing::subscriber::set_global_default(subscriber) .expect("setting default subscriber failed"); + */ + + + // When running on console, then disable tracing by default + let filter = if let Some(spec) = filter { + EnvFilter::new(spec) + } else { + EnvFilter::new("none") + }; + + tracing_subscriber::fmt() + .with_env_filter(filter) + //.with_timer(timer) + .init(); } /// Optionally set up tracing to a file. /// /// This function will attempt to set up tracing to a file. There are three /// conditions that must be true in order for tracing to a file to be enabled: /// - A filename must be specified (either via the `fname` argument or the /// `TRACE_FILE` environment variable). -/// - A trace level must be specified (either via the `level` argument or the -/// `TRACE_LEVEL` environment variable). +/// - A trace filter must be specified (either via the `filter` argument or the +/// `TRACE_FILTER` environment variable). /// - The file name must be openable for writing. /// /// Because tracing is an optional feature and intended for development only, /// this function will enable tracing if possible, and silently ignore and /// errors. -pub fn init_file_tracing

(fname: P, level: LogLevel) +pub fn init_file_tracing

(fname: P, filter: Option<&str>) where P: AsRef { - // If a level was set, convert it to an Option. - let lvl: Option = level.into(); - // // If both a trace file name and a trace level // let timer = UtcTime::new(format_description!( "[year]-[month]-[day] [hour]:[minute]:[second]" @@ -348,15 +353,33 @@ { f } else { return; }; + + // + // If TRACING_FILE is set, then default to filter out at warn level. + // Disable all tracing if TRACE_FILTER is not set + // + let filter = if let Some(spec) = filter { + EnvFilter::new(spec) + } else { + EnvFilter::new("warn") + /* + EnvFilter::builder() + .with_default_directive(LevelFilter::OFF.into()) + .parse("") + .unwrap() + */ + }; tracing_subscriber::fmt() + .with_env_filter(filter) .with_writer(f) .with_ansi(false) - .with_max_level(lvl) .with_timer(timer) .init(); + + //tracing_subscriber::registry().with(filter).init(); } // vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 : Index: www/changelog.md ================================================================== --- www/changelog.md +++ www/changelog.md @@ -1,17 +1,41 @@ # Change log +⚠️ indicates a breaking change. + ## [Unreleased] -[Details](/vdiff?from=qsu-0.4.1&to=trunk) +[Details](/vdiff?from=qsu-0.5.0&to=trunk) + +### Added + +### Changed + +### Removed + +--- + +## [0.5.0] - 2024-05-20 + +[Details](/vdiff?from=qsu-0.4.1&to=qsu-0.5.0) ### Added +- `TRACE_FILTER` environment variable used to configure tracing to output. +- `TraceFilter` registry key on Windows used to configure tracing output. + ### Changed + +- ⚠️ Instead of specifying a "trace level" (similar to "log level"), specify a + "trace filter", allowing fine-grained control of modules to include in + traces. ### Removed +- `TRACE_LEVEL` environment variable no longer used. +- `TraceLevel` registry key on Windows no longer used. + --- ## [0.4.1] - 2024-05-19 [Details](/vdiff?from=qsu-0.4.0&to=qsu-0.4.1)