Skip to content

Commit

Permalink
Replicate default log level filter for tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
Serock3 committed Jul 26, 2024
1 parent fbe402f commit 8a83c66
Showing 1 changed file with 38 additions and 28 deletions.
66 changes: 38 additions & 28 deletions mullvad-daemon/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
sync::atomic::{AtomicBool, Ordering},
};
use talpid_core::logging::rotate_log;
use tracing_subscriber;
use tracing_subscriber::{self, filter::LevelFilter, EnvFilter};

#[derive(thiserror::Error, Debug)]
pub enum Error {
Expand Down Expand Up @@ -55,14 +55,9 @@ const SLIGHTLY_SILENCED_CRATES: &[&str] = &["mnl", "nftnl", "udp_over_tcp"];
// trace: Color::Black,
// };

#[cfg(not(windows))]
const LINE_SEPARATOR: &str = "\n";

#[cfg(windows)]
const LINE_SEPARATOR: &str = "\r\n";

const DATE_TIME_FORMAT_STR: &str = "[%Y-%m-%d %H:%M:%S%.3f]";

/// Whether a [log] logger has been initialized.
// the log crate doesn't provide a nice way to tell if a logger has been initialized :(
static LOG_ENABLED: AtomicBool = AtomicBool::new(false);
Expand All @@ -77,18 +72,43 @@ pub fn init_logger(
log_file: Option<&PathBuf>,
output_timestamp: bool,
) -> Result<(), Error> {
tracing_subscriber::fmt::init();
// for silenced_crate in WARNING_SILENCED_CRATES {
// top_dispatcher = top_dispatcher.level_for(*silenced_crate, log::LevelFilter::Error);
// }
// for silenced_crate in SILENCED_CRATES {
// top_dispatcher = top_dispatcher.level_for(*silenced_crate, log::LevelFilter::Warn);
// }
// for silenced_crate in SLIGHTLY_SILENCED_CRATES {
// top_dispatcher = top_dispatcher.level_for(*silenced_crate, one_level_quieter(log_level));
// }

if let Some(ref log_file) = log_file {
let level_filter = match log_level {
log::LevelFilter::Off => LevelFilter::OFF,
log::LevelFilter::Error => LevelFilter::ERROR,
log::LevelFilter::Warn => LevelFilter::WARN,
log::LevelFilter::Info => LevelFilter::INFO,
log::LevelFilter::Debug => LevelFilter::DEBUG,
log::LevelFilter::Trace => LevelFilter::TRACE,
};

let mut env_filter = EnvFilter::from_default_env().add_directive(level_filter.into());

for silenced_crate in WARNING_SILENCED_CRATES {
env_filter = env_filter.add_directive(format!("{silenced_crate}=error").parse().unwrap());
}
for silenced_crate in SILENCED_CRATES {
env_filter = env_filter.add_directive(format!("{silenced_crate}=warn").parse().unwrap());
}

for silenced_crate in SLIGHTLY_SILENCED_CRATES {
env_filter = env_filter.add_directive(
format!("{silenced_crate}={}", one_level_quieter(log_level))
.parse()
.unwrap(),
);
}

let fmt_subscriber = tracing_subscriber::fmt::fmt()
.with_env_filter(env_filter)
.with_ansi(true);

if output_timestamp {
fmt_subscriber.init();
} else {
fmt_subscriber.without_time().init();
}

if let Some(log_file) = log_file {
rotate_log(log_file).map_err(Error::RotateLog)?;
}
#[cfg(all(target_os = "android", debug_assertions))]
Expand Down Expand Up @@ -116,13 +136,3 @@ fn one_level_quieter(level: log::LevelFilter) -> log::LevelFilter {
Trace => Debug,
}
}

#[cfg(not(windows))]
fn escape_newlines(text: String) -> String {
text
}

#[cfg(windows)]
fn escape_newlines(text: String) -> String {
text.replace('\n', LINE_SEPARATOR)
}

0 comments on commit 8a83c66

Please sign in to comment.