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",
]
[[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"

View file

@ -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"] }

View file

@ -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<GupaxxData>,
#[clap(long, short, action)]
pub logfile: bool,
}
#[derive(Subcommand)]

View file

@ -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]

View file

@ -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);