diff --git a/Cargo.lock b/Cargo.lock index ae27f8cf..3cabed5c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -414,15 +414,6 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" -[[package]] -name = "deranged" -version = "0.3.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4" -dependencies = [ - "powerfmt", -] - [[package]] name = "difflib" version = "0.4.0" @@ -852,12 +843,6 @@ dependencies = [ "num-traits", ] -[[package]] -name = "num-conv" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" - [[package]] name = "num-derive" version = "0.4.2" @@ -900,15 +885,6 @@ dependencies = [ "libm", ] -[[package]] -name = "num_threads" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c7398b9c8b70908f6371f47ed36737907c87c52af34c268fed0bf0ceb92ead9" -dependencies = [ - "libc", -] - [[package]] name = "once_cell" version = "1.19.0" @@ -974,12 +950,6 @@ dependencies = [ "miniz_oxide", ] -[[package]] -name = "powerfmt" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" - [[package]] name = "ppv-lite86" version = "0.2.17" @@ -1313,17 +1283,6 @@ dependencies = [ "quote", ] -[[package]] -name = "simplelog" -version = "0.12.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16257adbfaef1ee58b1363bdc0664c9b8e1e30aed86049635fb5f147d065a9c0" -dependencies = [ - "log", - "termcolor", - "time", -] - [[package]] name = "smallvec" version = "1.13.2" @@ -1378,7 +1337,6 @@ dependencies = [ "rayon", "rustix", "sd-notify", - "simplelog", "spin_sleep", "utils", "wayland-client", @@ -1416,15 +1374,6 @@ version = "0.12.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e1fc403891a21bcfb7c37834ba66a547a8f402146eba7265b5a6d88059c9ff2f" -[[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" @@ -1472,39 +1421,6 @@ dependencies = [ "weezl", ] -[[package]] -name = "time" -version = "0.3.36" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885" -dependencies = [ - "deranged", - "itoa", - "libc", - "num-conv", - "num_threads", - "powerfmt", - "serde", - "time-core", - "time-macros", -] - -[[package]] -name = "time-core" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" - -[[package]] -name = "time-macros" -version = "0.2.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f252a68540fde3a3877aeea552b832b40ab9a69e318efd078774a01ddee1ccf" -dependencies = [ - "num-conv", - "time-core", -] - [[package]] name = "tinytemplate" version = "1.2.1" diff --git a/daemon/Cargo.toml b/daemon/Cargo.toml index 52d4099c..5037c9b4 100644 --- a/daemon/Cargo.toml +++ b/daemon/Cargo.toml @@ -7,8 +7,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -log = { version = "0.4", features = ["max_level_debug", "release_max_level_info"] } -simplelog = "0.12" +log = { version = "0.4", default_features = false, features = ["max_level_debug", "release_max_level_info", "std"] } wayland-client = { version = "0.31", default-features = false, features = [ "log" ]} wayland-protocols = { version = "0.31", default-features = false, features = [ "client", "staging" ]} diff --git a/daemon/src/main.rs b/daemon/src/main.rs index d4a52122..b82b9c5b 100644 --- a/daemon/src/main.rs +++ b/daemon/src/main.rs @@ -11,7 +11,6 @@ use rustix::{ event::{poll, PollFd, PollFlags}, fd::OwnedFd, }; -use simplelog::{ColorChoice, TermLogger, TerminalMode, ThreadLogMode}; use wallpaper::Wallpaper; use wayland_protocols::wp::{ fractional_scale::v1::client::{ @@ -23,6 +22,7 @@ use wayland_protocols::wp::{ use std::{ fs, + io::{IsTerminal, Write}, num::NonZeroI32, path::PathBuf, sync::{ @@ -810,23 +810,66 @@ impl Dispatch for Daemon { } } +struct Logger { + level_filter: LevelFilter, + start: std::time::Instant, + is_term: bool, +} + +impl log::Log for Logger { + fn enabled(&self, metadata: &log::Metadata) -> bool { + metadata.level() <= self.level_filter + } + + fn log(&self, record: &log::Record) { + if self.enabled(record.metadata()) { + let time = self.start.elapsed().as_millis(); + + let level = if self.is_term { + match record.level() { + log::Level::Error => "\x1b[31m[ERROR]\x1b[0m", + log::Level::Warn => "\x1b[33m[WARN]\x1b[0m ", + log::Level::Info => "\x1b[32m[INFO]\x1b[0m ", + log::Level::Debug | log::Level::Trace => "\x1b[36m[DEBUG]\x1b[0m", + } + } else { + match record.level() { + log::Level::Error => "[ERROR]", + log::Level::Warn => "[WARN] ", + log::Level::Info => "[INFO] ", + log::Level::Debug | log::Level::Trace => "[DEBUG]", + } + }; + + let thread = std::thread::current(); + let thread_name = thread.name().unwrap_or("???"); + let msg = record.args(); + + let _ = std::io::stderr() + .lock() + .write_fmt(format_args!("{time:>8}ms {level} ({thread_name}) {msg}\n")); + } + } + + fn flush(&self) { + //no op (we do not buffer anything) + } +} + fn make_logger(quiet: bool) { - let config = simplelog::ConfigBuilder::new() - .set_thread_level(LevelFilter::Error) // let me see where the processing is happening - .set_thread_mode(ThreadLogMode::Both) - .build(); - - TermLogger::init( - if quiet { - LevelFilter::Error - } else { - LevelFilter::Debug - }, - config, - TerminalMode::Stderr, - ColorChoice::AlwaysAnsi, - ) - .expect("Failed to initialize logger. Cancelling..."); + let level_filter = if quiet { + LevelFilter::Error + } else { + LevelFilter::Debug + }; + + log::set_boxed_logger(Box::new(Logger { + level_filter, + start: std::time::Instant::now(), + is_term: std::io::stderr().is_terminal(), + })) + .map(|()| log::set_max_level(level_filter)) + .unwrap(); } pub fn is_daemon_running(addr: &PathBuf) -> Result {