More public fields in cursive::logger

This commit is contained in:
Alexandre Bury 2019-05-18 08:38:24 -07:00
parent 5ee7e11619
commit 8d6b256b1c

View File

@ -7,7 +7,7 @@ use std::sync::Mutex;
/// Saves all log records in a global deque. /// Saves all log records in a global deque.
/// ///
/// Uses a `DebugView` to access it. /// Uses a `DebugView` to access it.
struct CursiveLogger; pub struct CursiveLogger;
static LOGGER: CursiveLogger = CursiveLogger; static LOGGER: CursiveLogger = CursiveLogger;
@ -27,12 +27,8 @@ lazy_static! {
Mutex::new(VecDeque::new()); Mutex::new(VecDeque::new());
} }
impl log::Log for CursiveLogger { /// Log a record in cursive's log queue.
fn enabled(&self, _metadata: &log::Metadata<'_>) -> bool { pub fn log(record: &log::Record<'_>) {
true
}
fn log(&self, record: &log::Record<'_>) {
let mut logs = LOGS.lock().unwrap(); let mut logs = LOGS.lock().unwrap();
// TODO: customize the format? Use colors? Save more info? // TODO: customize the format? Use colors? Save more info?
if logs.len() == logs.capacity() { if logs.len() == logs.capacity() {
@ -45,6 +41,15 @@ impl log::Log for CursiveLogger {
}); });
} }
impl log::Log for CursiveLogger {
fn enabled(&self, _metadata: &log::Metadata<'_>) -> bool {
true
}
fn log(&self, record: &log::Record<'_>) {
log(record);
}
fn flush(&self) {} fn flush(&self) {}
} }
@ -56,7 +61,7 @@ impl log::Log for CursiveLogger {
/// [`Cursive::toggle_debug_console()`](crate::Cursive::toggle_debug_console()). /// [`Cursive::toggle_debug_console()`](crate::Cursive::toggle_debug_console()).
pub fn init() { pub fn init() {
// TODO: Configure the deque size? // TODO: Configure the deque size?
LOGS.lock().unwrap().reserve(1_000); reserve_logs(1_000);
// This will panic if `set_logger` was already called. // This will panic if `set_logger` was already called.
log::set_logger(&LOGGER).unwrap(); log::set_logger(&LOGGER).unwrap();
@ -64,3 +69,22 @@ pub fn init() {
// TODO: read the level from env variable? From argument? // TODO: read the level from env variable? From argument?
log::set_max_level(log::LevelFilter::Trace); log::set_max_level(log::LevelFilter::Trace);
} }
/// Return a logger that stores records in cursive's log queue.
///
/// These logs can then be read by a [`DebugView`](crate::views::DebugView).
///
/// An easier alternative might be to use [`init()`].
pub fn get_logger() -> CursiveLogger {
reserve_logs(1_000);
CursiveLogger
}
/// Adds `n` more entries to cursive's log queue.
///
/// Most of the time you don't need to use this directly.
///
/// You should call this if you're not using `init()` nor `get_logger()`.
pub fn reserve_logs(n: usize) {
LOGS.lock().unwrap().reserve(n);
}