From 09cde4869dec78c520333834c94bf929047ca839 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BChlbacher?= Date: Fri, 19 Jul 2024 21:34:15 +0200 Subject: [PATCH] feat(logging): switch to custom file:line format MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This mostly tries to be similar to the default `env_logger` format but instead of using the more vague target in the log message, we instead put the file name and line number in the log. Signed-off-by: Thomas Mühlbacher --- Cargo.lock | 44 +++++++------------------------------------- Cargo.toml | 2 +- src/logging.rs | 27 ++++++++++++++++++++++++++- 3 files changed, 34 insertions(+), 39 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 37c2bd40..a7856599 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -85,6 +85,7 @@ dependencies = [ "errno 0.2.8", "libc", "log", + "owo-colors", "rustix", "strum", "strum_macros", @@ -258,9 +259,7 @@ version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4cd405aab171cb85d6735e5c8d9db038c17d3ca007a4d2c25f337935c3d90580" dependencies = [ - "is-terminal", "log", - "termcolor", ] [[package]] @@ -306,12 +305,6 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" -[[package]] -name = "hermit-abi" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" - [[package]] name = "home" version = "0.5.9" @@ -321,17 +314,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "is-terminal" -version = "0.4.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f23ff5ef2b80d608d61efee834934d862cd92461afc0560dedf493e4c033738b" -dependencies = [ - "hermit-abi", - "libc", - "windows-sys 0.52.0", -] - [[package]] name = "itertools" version = "0.12.1" @@ -428,6 +410,12 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +[[package]] +name = "owo-colors" +version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caff54706df99d2a78a5a4e3455ff45448d81ef1bb63c22cd14052ca0e993a3f" + [[package]] name = "paste" version = "1.0.14" @@ -567,15 +555,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "termcolor" -version = "1.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" -dependencies = [ - "winapi-util", -] - [[package]] name = "terminal_size" version = "0.3.0" @@ -643,15 +622,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" -[[package]] -name = "winapi-util" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d4cc384e1e73b93bafa6fb4f1df8c41695c8a91cf9c4c64358067d15a7b6c6b" -dependencies = [ - "windows-sys 0.52.0", -] - [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 151a9428..b3991a09 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,11 +24,11 @@ strum = { version = "0.26", features = ["derive"] } strum_macros = "0.26" zeroize = { version = "1", features = ["std", "zeroize_derive"] } rustix = { version = "0.38.34", features = ["termios"] } +owo-colors = "4" [dependencies.env_logger] version = "0.10" default-features = false -features = ["auto-color"] [profile.release] strip = "none" diff --git a/src/logging.rs b/src/logging.rs index 98ca091f..78c0d92d 100644 --- a/src/logging.rs +++ b/src/logging.rs @@ -1,5 +1,8 @@ +use std::io::Write; + use env_logger::WriteStyle; -use log::LevelFilter; +use log::{Level, LevelFilter}; +use owo_colors::{OwoColorize, Style}; pub fn setup(verbose: u8, color: bool) { let level_filter = match verbose { @@ -19,5 +22,27 @@ pub fn setup(verbose: u8, color: bool) { .filter_level(level_filter) .write_style(style) .parse_env("BCACHEFS_LOG") + .format(move |buf, record| { + let style = if style == WriteStyle::Never { + Style::new() + } else { + match record.level() { + Level::Trace => Style::new().cyan(), + Level::Debug => Style::new().blue(), + Level::Info => Style::new().green(), + Level::Warn => Style::new().yellow(), + Level::Error => Style::new().red().bold(), + } + }; + + writeln!( + buf, + "[{:<5} {}:{}] {}", + record.level().style(style), + record.file().unwrap(), + record.line().unwrap(), + record.args() + ) + }) .init(); }