feat: add command line option for log file

This commit is contained in:
Cyrix126 2024-07-06 15:43:13 +02:00
parent c0e119339f
commit 4fe750a5be
5 changed files with 56 additions and 15 deletions

30
Cargo.lock generated
View file

@ -1853,6 +1853,20 @@ dependencies = [
"miniz_oxide", "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]] [[package]]
name = "flume" name = "flume"
version = "0.11.0" version = "0.11.0"
@ -2051,6 +2065,12 @@ dependencies = [
"xml-rs", "xml-rs",
] ]
[[package]]
name = "glob"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
[[package]] [[package]]
name = "glow" name = "glow"
version = "0.13.1" version = "0.13.1"
@ -2200,6 +2220,7 @@ dependencies = [
"env_logger", "env_logger",
"figment", "figment",
"flate2", "flate2",
"flexi_logger",
"image 0.25.1", "image 0.25.1",
"is_elevated", "is_elevated",
"log", "log",
@ -3072,6 +3093,15 @@ dependencies = [
"winapi", "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]] [[package]]
name = "num-bigint" name = "num-bigint"
version = "0.4.5" version = "0.4.5"

View file

@ -76,6 +76,7 @@ chrono = {version="0.4.38", default-features=false, features=["clock", "std"]}
enclose = "1.2.0" enclose = "1.2.0"
bounded-vec-deque = {version="0.1.1", default-features=false} bounded-vec-deque = {version="0.1.1", default-features=false}
cfg-if = "1.0" cfg-if = "1.0"
flexi_logger = "0.28"
# Unix dependencies # Unix dependencies
[target.'cfg(unix)'.dependencies] [target.'cfg(unix)'.dependencies]
eframe = { version = "0.27.2", features = ["wgpu"] } eframe = { version = "0.27.2", features = ["wgpu"] }

View file

@ -1,5 +1,3 @@
use std::process::exit;
use clap::crate_authors; use clap::crate_authors;
use clap::crate_description; use clap::crate_description;
use clap::crate_name; use clap::crate_name;
@ -9,6 +7,7 @@ use clap::Subcommand;
use log::debug; use log::debug;
use log::info; use log::info;
use log::warn; use log::warn;
use std::process::exit;
use crate::app::App; use crate::app::App;
use crate::miscs::print_disk_file; use crate::miscs::print_disk_file;
@ -29,6 +28,8 @@ use crate::resets::reset_state;
pub struct Cli { pub struct Cli {
#[command(subcommand)] #[command(subcommand)]
pub info: Option<GupaxxData>, pub info: Option<GupaxxData>,
#[clap(long, short, action)]
pub logfile: bool,
} }
#[derive(Subcommand)] #[derive(Subcommand)]

View file

@ -16,6 +16,7 @@ use egui::TextStyle::{Body, Button, Heading, Monospace, Name};
use egui::*; use egui::*;
use env_logger::fmt::style::Style; use env_logger::fmt::style::Style;
use env_logger::{Builder, WriteStyle}; use env_logger::{Builder, WriteStyle};
use flexi_logger::{FileSpec, Logger};
use log::LevelFilter; use log::LevelFilter;
use std::sync::Arc; use std::sync::Arc;
use std::time::Instant; use std::time::Instant;
@ -74,18 +75,25 @@ pub fn init_text_styles(ctx: &egui::Context, width: f32, pixels_per_point: f32)
#[cold] #[cold]
#[inline(never)] #[inline(never)]
pub fn init_logger(now: Instant) { pub fn init_logger(now: Instant, logfile: bool) {
let filter_env = std::env::var("RUST_LOG").unwrap_or_else(|_| "INFO".to_string()); if logfile {
let filter = match filter_env.as_str() { Logger::try_with_env_or_str("info")
"error" | "Error" | "ERROR" => LevelFilter::Error, .unwrap()
"warn" | "Warn" | "WARN" => LevelFilter::Warn, .log_to_file(FileSpec::default())
"debug" | "Debug" | "DEBUG" => LevelFilter::Debug, .start()
"trace" | "Trace" | "TRACE" => LevelFilter::Trace, .unwrap();
_ => LevelFilter::Info, } else {
}; let filter_env = std::env::var("RUST_LOG").unwrap_or_else(|_| "INFO".to_string());
std::env::set_var("RUST_LOG", format!("off,gupax={}", filter_env)); 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| { .format(move |buf, record| {
let level = record.level(); let level = record.level();
let level_style = buf.default_level_style(level); let level_style = buf.default_level_style(level);
@ -105,8 +113,9 @@ pub fn init_logger(now: Instant) {
.parse_default_env() .parse_default_env()
.format_timestamp_millis() .format_timestamp_millis()
.init(); .init();
info!("Log level ... {}", filter);
}
info!("init_logger() ... OK"); info!("init_logger() ... OK");
info!("Log level ... {}", filter);
} }
#[cold] #[cold]

View file

@ -62,7 +62,7 @@ fn main() {
crate::panic::set_panic_hook(now); crate::panic::set_panic_hook(now);
// Init logger. // Init logger.
init_logger(now); init_logger(now, args.logfile);
let mut app = App::new(now, args); let mut app = App::new(now, args);
init_auto(&mut app); init_auto(&mut app);