logger: only log to file in release builds

This commit is contained in:
Alex Janka 2024-09-11 09:47:19 +10:00
parent f49bf7b949
commit 0ced792b8c
2 changed files with 34 additions and 32 deletions

View file

@ -14,12 +14,21 @@ use log4rs::{
use crate::prelude::*; use crate::prelude::*;
pub async fn init_logger() -> Res<()> { pub async fn init_logger(file: bool) -> Res<()> {
let stderr = ConsoleAppender::builder() let stderr = ConsoleAppender::builder()
.target(Target::Stderr) .target(Target::Stderr)
.encoder(Box::new(PatternEncoder::new("{h({l})}: {m}\n"))) .encoder(Box::new(PatternEncoder::new("{h({l})}: {m}\n")))
.build(); .build();
let mut builder = Config::builder().appender(
Appender::builder()
.filter(Box::new(ThresholdFilter::new(log::LevelFilter::Info)))
.build("stderr", Box::new(stderr)),
);
let mut root = Root::builder().appender("stderr");
if file {
eprintln!("logging to file");
let trigger = SizeTrigger::new(8 * 1024); let trigger = SizeTrigger::new(8 * 1024);
let roller = FixedWindowRoller::builder() let roller = FixedWindowRoller::builder()
.base(0) .base(0)
@ -37,21 +46,13 @@ pub async fn init_logger() -> Res<()> {
Box::new(policy), Box::new(policy),
) )
.unwrap(); .unwrap();
builder = builder.appender(Appender::builder().build("logfile", Box::new(logfile)));
root = root.appender("logfile");
} else {
eprintln!("not logging to file");
}
let config = Config::builder() let config = builder.build(root.build(LevelFilter::Trace)).unwrap();
.appender(Appender::builder().build("logfile", Box::new(logfile)))
.appender(
Appender::builder()
.filter(Box::new(ThresholdFilter::new(log::LevelFilter::Info)))
.build("stderr", Box::new(stderr)),
)
.build(
Root::builder()
.appender("logfile")
.appender("stderr")
.build(LevelFilter::Trace),
)
.unwrap();
let _handle = log4rs::init_config(config).map_err(|_| Error::Log4Rs)?; let _handle = log4rs::init_config(config).map_err(|_| Error::Log4Rs)?;

View file

@ -23,7 +23,8 @@ static DEFAULT_BORDER: tokio::sync::OnceCell<colour::Format> = tokio::sync::Once
#[tokio::main] #[tokio::main]
async fn main() -> Res<()> { async fn main() -> Res<()> {
let _watch_handle = config::parse_config().await?; let _watch_handle = config::parse_config().await?;
logger::init_logger().await?; // only log to file in release builds
logger::init_logger(!cfg!(debug_assertions)).await?;
let mut event_connection = swayipc_async::Connection::new().await?; let mut event_connection = swayipc_async::Connection::new().await?;
let mut autosplit_connection = swayipc_async::Connection::new().await?; let mut autosplit_connection = swayipc_async::Connection::new().await?;