mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-23 17:35:00 +00:00
More public fields in cursive::logger
This commit is contained in:
parent
5ee7e11619
commit
8d6b256b1c
@ -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,22 +27,27 @@ lazy_static! {
|
|||||||
Mutex::new(VecDeque::new());
|
Mutex::new(VecDeque::new());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Log a record in cursive's log queue.
|
||||||
|
pub fn log(record: &log::Record<'_>) {
|
||||||
|
let mut logs = LOGS.lock().unwrap();
|
||||||
|
// TODO: customize the format? Use colors? Save more info?
|
||||||
|
if logs.len() == logs.capacity() {
|
||||||
|
logs.pop_front();
|
||||||
|
}
|
||||||
|
logs.push_back(Record {
|
||||||
|
level: record.level(),
|
||||||
|
message: format!("{}", record.args()),
|
||||||
|
time: chrono::Utc::now(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
impl log::Log for CursiveLogger {
|
impl log::Log for CursiveLogger {
|
||||||
fn enabled(&self, _metadata: &log::Metadata<'_>) -> bool {
|
fn enabled(&self, _metadata: &log::Metadata<'_>) -> bool {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
fn log(&self, record: &log::Record<'_>) {
|
fn log(&self, record: &log::Record<'_>) {
|
||||||
let mut logs = LOGS.lock().unwrap();
|
log(record);
|
||||||
// TODO: customize the format? Use colors? Save more info?
|
|
||||||
if logs.len() == logs.capacity() {
|
|
||||||
logs.pop_front();
|
|
||||||
}
|
|
||||||
logs.push_back(Record {
|
|
||||||
level: record.level(),
|
|
||||||
message: format!("{}", record.args()),
|
|
||||||
time: chrono::Utc::now(),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user