-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dump debug logs to a file rather than stderr
Fixes #238 We now use our own `Log` implementation that logs to a file. The log file is created on demand (on first log) so most of the time there won't a file created. Note that default log level is still "error". Given that logging won't cause glithces in the UI by default now I think it makes sense to keep error logs enabled always.
- Loading branch information
Showing
4 changed files
with
118 additions
and
48 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
//! This module provides a logger (as in `log` and `env_logger` crates, rather than | ||
//! `libtiny_logger`) implementation for logging to a file rather than stdout/stderr (which is what | ||
//! `env_logger` provides). | ||
//! | ||
//! Some notes regarding implementation: | ||
//! | ||
//! - All IO errors ignored. Once initialized the logger never panics. | ||
//! - TINY_LOG is the env variable used for setting filters. | ||
//! - Filter syntax is unchanged (same as `env_logger` syntax). | ||
//! - Log file is created when logging for the first time. | ||
use env_logger::filter::{self, Filter}; | ||
use log::{Log, Record}; | ||
|
||
use std::fs::File; | ||
use std::io::Write; | ||
use std::mem::replace; | ||
use std::path::PathBuf; | ||
use std::sync::Mutex; | ||
|
||
pub(crate) fn init(path: PathBuf) { | ||
let filter = filter::Builder::from_env("TINY_LOG").build(); | ||
let sink = Mutex::new(LazyFile::new(path)); | ||
|
||
log::set_max_level(filter.filter()); | ||
log::set_boxed_logger(Box::new(Logger { sink, filter })).unwrap(); | ||
} | ||
|
||
struct Logger { | ||
sink: Mutex<LazyFile>, | ||
filter: Filter, | ||
} | ||
|
||
impl Log for Logger { | ||
fn enabled(&self, metadata: &log::Metadata) -> bool { | ||
self.filter.enabled(metadata) | ||
} | ||
|
||
fn log(&self, record: &Record) { | ||
if !self.filter.matches(record) { | ||
return; | ||
} | ||
|
||
self.sink.lock().unwrap().with_file(|file| { | ||
let _ = writeln!( | ||
file, | ||
"[{}] {} [{}:{}] {}", | ||
time::strftime("%F %T", &time::now()).unwrap(), | ||
record.level(), | ||
record.file().unwrap_or_default(), | ||
record.line().unwrap_or_default(), | ||
record.args() | ||
); | ||
}); | ||
} | ||
|
||
fn flush(&self) {} | ||
} | ||
|
||
enum LazyFile { | ||
NotOpen(PathBuf), | ||
Open(File), | ||
Error, | ||
} | ||
|
||
impl LazyFile { | ||
fn new(path: PathBuf) -> Self { | ||
LazyFile::NotOpen(path) | ||
} | ||
|
||
fn with_file<F>(&mut self, f: F) | ||
where | ||
F: Fn(&mut File), | ||
{ | ||
let mut file = match replace(self, LazyFile::Error) { | ||
LazyFile::NotOpen(path) => match File::create(path) { | ||
Ok(file) => file, | ||
Err(_) => { | ||
return; | ||
} | ||
}, | ||
LazyFile::Open(file) => file, | ||
LazyFile::Error => { | ||
return; | ||
} | ||
}; | ||
|
||
f(&mut file); | ||
*self = LazyFile::Open(file); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters