From 4fe750a5be73415914cfb712852c53ee88b58b71 Mon Sep 17 00:00:00 2001 From: Cyrix126 Date: Sat, 6 Jul 2024 15:43:13 +0200 Subject: [PATCH] feat: add command line option for log file --- Cargo.lock | 30 ++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/cli.rs | 5 +++-- src/inits.rs | 33 +++++++++++++++++++++------------ src/main.rs | 2 +- 5 files changed, 56 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e47b0fd..6b8270d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1853,6 +1853,20 @@ dependencies = [ "miniz_oxide", ] +[[package]] +name = "flexi_logger" +version = "0.28.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cca927478b3747ba47f98af6ba0ac0daea4f12d12f55e9104071b3dc00276310" +dependencies = [ + "chrono", + "glob", + "log", + "nu-ansi-term", + "regex", + "thiserror", +] + [[package]] name = "flume" version = "0.11.0" @@ -2051,6 +2065,12 @@ dependencies = [ "xml-rs", ] +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + [[package]] name = "glow" version = "0.13.1" @@ -2200,6 +2220,7 @@ dependencies = [ "env_logger", "figment", "flate2", + "flexi_logger", "image 0.25.1", "is_elevated", "log", @@ -3072,6 +3093,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "nu-ansi-term" +version = "0.50.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd2800e1520bdc966782168a627aa5d1ad92e33b984bf7c7615d31280c83ff14" +dependencies = [ + "windows-sys 0.48.0", +] + [[package]] name = "num-bigint" version = "0.4.5" diff --git a/Cargo.toml b/Cargo.toml index f34b3a0..221c10a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -76,6 +76,7 @@ chrono = {version="0.4.38", default-features=false, features=["clock", "std"]} enclose = "1.2.0" bounded-vec-deque = {version="0.1.1", default-features=false} cfg-if = "1.0" +flexi_logger = "0.28" # Unix dependencies [target.'cfg(unix)'.dependencies] eframe = { version = "0.27.2", features = ["wgpu"] } diff --git a/src/cli.rs b/src/cli.rs index 74731c4..c27dd56 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,5 +1,3 @@ -use std::process::exit; - use clap::crate_authors; use clap::crate_description; use clap::crate_name; @@ -9,6 +7,7 @@ use clap::Subcommand; use log::debug; use log::info; use log::warn; +use std::process::exit; use crate::app::App; use crate::miscs::print_disk_file; @@ -29,6 +28,8 @@ use crate::resets::reset_state; pub struct Cli { #[command(subcommand)] pub info: Option, + #[clap(long, short, action)] + pub logfile: bool, } #[derive(Subcommand)] diff --git a/src/inits.rs b/src/inits.rs index c6583ae..36ad9b6 100644 --- a/src/inits.rs +++ b/src/inits.rs @@ -16,6 +16,7 @@ use egui::TextStyle::{Body, Button, Heading, Monospace, Name}; use egui::*; use env_logger::fmt::style::Style; use env_logger::{Builder, WriteStyle}; +use flexi_logger::{FileSpec, Logger}; use log::LevelFilter; use std::sync::Arc; use std::time::Instant; @@ -74,18 +75,25 @@ pub fn init_text_styles(ctx: &egui::Context, width: f32, pixels_per_point: f32) #[cold] #[inline(never)] -pub fn init_logger(now: Instant) { - let filter_env = std::env::var("RUST_LOG").unwrap_or_else(|_| "INFO".to_string()); - let filter = match filter_env.as_str() { - "error" | "Error" | "ERROR" => LevelFilter::Error, - "warn" | "Warn" | "WARN" => LevelFilter::Warn, - "debug" | "Debug" | "DEBUG" => LevelFilter::Debug, - "trace" | "Trace" | "TRACE" => LevelFilter::Trace, - _ => LevelFilter::Info, - }; - std::env::set_var("RUST_LOG", format!("off,gupax={}", filter_env)); +pub fn init_logger(now: Instant, logfile: bool) { + if logfile { + Logger::try_with_env_or_str("info") + .unwrap() + .log_to_file(FileSpec::default()) + .start() + .unwrap(); + } else { + let filter_env = std::env::var("RUST_LOG").unwrap_or_else(|_| "INFO".to_string()); + let filter = match filter_env.as_str() { + "error" | "Error" | "ERROR" => LevelFilter::Error, + "warn" | "Warn" | "WARN" => LevelFilter::Warn, + "debug" | "Debug" | "DEBUG" => LevelFilter::Debug, + "trace" | "Trace" | "TRACE" => LevelFilter::Trace, + _ => LevelFilter::Info, + }; + std::env::set_var("RUST_LOG", format!("off,gupax={}", filter_env)); - Builder::new() + Builder::new() .format(move |buf, record| { let level = record.level(); let level_style = buf.default_level_style(level); @@ -105,8 +113,9 @@ pub fn init_logger(now: Instant) { .parse_default_env() .format_timestamp_millis() .init(); + info!("Log level ... {}", filter); + } info!("init_logger() ... OK"); - info!("Log level ... {}", filter); } #[cold] diff --git a/src/main.rs b/src/main.rs index 2c96e62..6e17dc0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,7 +62,7 @@ fn main() { crate::panic::set_panic_hook(now); // Init logger. - init_logger(now); + init_logger(now, args.logfile); let mut app = App::new(now, args); init_auto(&mut app);