Skip to content

Commit

Permalink
console: configurable log levels, file logging (#359) (#488)
Browse files Browse the repository at this point in the history
* console: write to log file (configdir/client.log)

* console: change to log only Info and more important to terminal (but all to file)

* console: configurable log levels
  • Loading branch information
iceiix authored Jan 20, 2021
1 parent c7bdb60 commit 3daa9c0
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 9 deletions.
77 changes: 68 additions & 9 deletions src/console/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,42 @@ pub struct CVar<T: Sized + Any + 'static> {
pub default: &'static dyn Fn() -> T,
}

pub const LOG_LEVEL_TERM: CVar<String> = CVar {
ty: PhantomData,
name: "log_level_term",
description: "log level of messages to log to the terminal",
mutable: false,
serializable: true,
default: &|| "info".to_owned(),
};

pub const LOG_LEVEL_FILE: CVar<String> = CVar {
ty: PhantomData,
name: "log_level_file",
description: "log level of messages to log to the log file",
mutable: false,
serializable: true,
default: &|| "trace".to_owned(),
};

pub fn register_vars(vars: &mut Vars) {
vars.register(LOG_LEVEL_TERM);
vars.register(LOG_LEVEL_FILE);
}

fn log_level_from_str(s: &str, default: log::Level) -> log::Level {
// TODO: no opposite of FromStr in log crate?
use log::Level::*;
match s {
"trace" => Trace,
"debug" => Debug,
"info" => Info,
"warn" => Warn,
"error" => Error,
_ => default,
}
}

impl Var for CVar<i64> {
fn serialize(&self, val: &Box<dyn Any>) -> String {
val.downcast_ref::<i64>().unwrap().to_string()
Expand Down Expand Up @@ -203,10 +239,12 @@ impl Vars {
}
}

#[derive(Default)]
pub struct Console {
history: Vec<Component>,
dirty: bool,
logfile: fs::File,
log_level_term: log::Level,
log_level_file: log::Level,

elements: Option<ConsoleElements>,
active: bool,
Expand All @@ -218,18 +256,32 @@ struct ConsoleElements {
lines: Vec<ui::FormattedRef>,
}

impl Default for Console {
fn default() -> Self {
Self::new()
}
}

impl Console {
pub fn new() -> Console {
Console {
history: vec![Component::Text(TextComponent::new("")); 200],
dirty: false,
logfile: fs::File::create("client.log").expect("failed to open log file"),
log_level_term: log::Level::Info,
log_level_file: log::Level::Trace,

elements: None,
active: false,
position: -220.0,
}
}

pub fn configure(&mut self, vars: &Vars) {
self.log_level_term = log_level_from_str(&vars.get(LOG_LEVEL_TERM), log::Level::Info);
self.log_level_file = log_level_from_str(&vars.get(LOG_LEVEL_FILE), log::Level::Trace);
}

pub fn is_active(&self) -> bool {
self.active
}
Expand Down Expand Up @@ -319,16 +371,23 @@ impl Console {
file = &file[pos + 4..];
}

println_level(
let line = format!(
"[{}:{}][{}] {}",
file,
record.line().unwrap_or(0),
record.level(),
format!(
"[{}:{}][{}] {}",
file,
record.line().unwrap_or(0),
record.level(),
record.args()
),
record.args()
);

if record.level() <= self.log_level_file {
self.logfile.write_all(line.as_bytes()).unwrap();
self.logfile.write_all(b"\n").unwrap();
}

if record.level() <= self.log_level_term {
println_level(record.level(), line);
}

self.history.remove(0);
let mut msg = TextComponent::new("");
msg.modifier.extra = Some(vec![
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,12 @@ fn main2() {
let (vars, mut vsync) = {
let mut vars = console::Vars::new();
vars.register(CL_BRAND);
console::register_vars(&mut vars);
auth::register_vars(&mut vars);
settings::register_vars(&mut vars);
vars.load_config();
vars.save_config();
con.lock().unwrap().configure(&vars);
let vsync = *vars.get(settings::R_VSYNC);
(Rc::new(vars), vsync)
};
Expand Down

0 comments on commit 3daa9c0

Please sign in to comment.