|
@ -1,7 +1,6 @@ |
|
|
use std::cell::RefCell;
|
|
|
use std::cell::RefCell;
|
|
|
use std::cmp::min;
|
|
|
use std::cmp::min;
|
|
|
use std::io::stderr;
|
|
|
|
|
|
use std::io::Write;
|
|
|
|
|
|
|
|
|
use std::io::{stderr, Write};
|
|
|
use std::rc::Rc;
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
|
// The log crate defines
|
|
|
// The log crate defines
|
|
@ -12,34 +11,18 @@ pub type Level = usize; |
|
|
pub struct Entry<S>(pub Level, pub S);
|
|
|
pub struct Entry<S>(pub Level, pub S);
|
|
|
|
|
|
|
|
|
pub trait Logger {
|
|
|
pub trait Logger {
|
|
|
fn write<S: AsRef<str> + Into<String>>(&self, level: Level, msg: S)
|
|
|
|
|
|
where
|
|
|
|
|
|
Self: Sized;
|
|
|
|
|
|
fn writeln<S: AsRef<str> + Into<String>>(&self, level: Level, msg: S)
|
|
|
|
|
|
where
|
|
|
|
|
|
Self: Sized;
|
|
|
|
|
|
fn info<S: AsRef<str> + Into<String>>(&self, msg: S)
|
|
|
|
|
|
where
|
|
|
|
|
|
Self: Sized,
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
fn write(&self, level: Level, msg: &str);
|
|
|
|
|
|
fn writeln(&self, level: Level, msg: &str);
|
|
|
|
|
|
fn info(&self, msg: &str) {
|
|
|
self.writeln(3, msg);
|
|
|
self.writeln(3, msg);
|
|
|
}
|
|
|
}
|
|
|
fn debug<S: AsRef<str> + Into<String>>(&self, msg: S)
|
|
|
|
|
|
where
|
|
|
|
|
|
Self: Sized,
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
fn debug(&self, msg: &str) {
|
|
|
self.writeln(4, msg);
|
|
|
self.writeln(4, msg);
|
|
|
}
|
|
|
}
|
|
|
fn trace<S: AsRef<str> + Into<String>>(&self, msg: S)
|
|
|
|
|
|
where
|
|
|
|
|
|
Self: Sized,
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
fn trace(&self, msg: &str) {
|
|
|
self.writeln(5, msg);
|
|
|
self.writeln(5, msg);
|
|
|
}
|
|
|
}
|
|
|
fn put<S: AsRef<str> + Into<String>>(&self, entries: impl IntoIterator<Item = Entry<S>>) -> usize |
|
|
|
|
|
where
|
|
|
|
|
|
Self: Sized,
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
fn put<'a>(&self, entries: impl IntoIterator<Item = Entry<&'a str>>) -> usize {
|
|
|
let mut c = 0;
|
|
|
let mut c = 0;
|
|
|
for item in entries {
|
|
|
for item in entries {
|
|
|
self.writeln(item.0, item.1);
|
|
|
self.writeln(item.0, item.1);
|
|
@ -55,21 +38,21 @@ pub struct StdErrLogger { |
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
impl Logger for StdErrLogger {
|
|
|
impl Logger for StdErrLogger {
|
|
|
fn write<S: AsRef<str> + Into<String>>(&self, _level: Level, msg: S) {
|
|
|
|
|
|
|
|
|
fn write(&self, _level: Level, msg: &str) {
|
|
|
*self.line_started.borrow_mut() = true;
|
|
|
*self.line_started.borrow_mut() = true;
|
|
|
write!(&mut stderr(), "{}", msg.as_ref()).unwrap();
|
|
|
|
|
|
|
|
|
write!(&mut stderr(), "{msg}").expect("Could not write to stderr");
|
|
|
}
|
|
|
}
|
|
|
fn writeln<S: AsRef<str> + Into<String>>(&self, _level: Level, msg: S) {
|
|
|
|
|
|
|
|
|
fn writeln(&self, _level: Level, msg: &str) {
|
|
|
if self.line_started.replace(false) {
|
|
|
if self.line_started.replace(false) {
|
|
|
writeln!(&mut stderr()).unwrap();
|
|
|
|
|
|
|
|
|
writeln!(&mut stderr()).expect("Could not write to stderr");
|
|
|
}
|
|
|
}
|
|
|
writeln!(&mut stderr(), "{}", msg.as_ref()).unwrap();
|
|
|
|
|
|
|
|
|
writeln!(&mut stderr(), "{msg}").expect("Could not write to stderr");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
impl Drop for StdErrLogger {
|
|
|
impl Drop for StdErrLogger {
|
|
|
fn drop(&mut self) {
|
|
|
fn drop(&mut self) {
|
|
|
if *self.line_started.borrow() {
|
|
|
if *self.line_started.borrow() {
|
|
|
writeln!(&mut stderr()).unwrap();
|
|
|
|
|
|
|
|
|
writeln!(&mut stderr()).expect("Could not write to stderr");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@ -87,14 +70,14 @@ impl<'a, L> FilteringLogger<'a, L> { |
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
impl<'a, L: Logger> Logger for FilteringLogger<'a, L> {
|
|
|
impl<'a, L: Logger> Logger for FilteringLogger<'a, L> {
|
|
|
fn write<S: AsRef<str> + Into<String>>(&self, level: Level, str: S) {
|
|
|
|
|
|
|
|
|
fn write(&self, level: Level, msg: &str) {
|
|
|
if level <= self.max_level {
|
|
|
if level <= self.max_level {
|
|
|
self.logger.write(level, str);
|
|
|
|
|
|
|
|
|
self.logger.write(level, msg);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
fn writeln<S: AsRef<str> + Into<String>>(&self, level: Level, str: S) {
|
|
|
|
|
|
|
|
|
fn writeln(&self, level: Level, msg: &str) {
|
|
|
if level <= self.max_level {
|
|
|
if level <= self.max_level {
|
|
|
self.logger.writeln(level, str);
|
|
|
|
|
|
|
|
|
self.logger.writeln(level, msg);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@ -117,24 +100,21 @@ impl StoringLogger { |
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
impl Logger for StoringLogger {
|
|
|
impl Logger for StoringLogger {
|
|
|
fn write<S: AsRef<str> + Into<String>>(&self, level: Level, line: S) {
|
|
|
|
|
|
|
|
|
fn write(&self, level: Level, msg: &str) {
|
|
|
let mut log = self.log.borrow_mut();
|
|
|
let mut log = self.log.borrow_mut();
|
|
|
let entry = if log.0 {
|
|
|
let entry = if log.0 {
|
|
|
log
|
|
|
|
|
|
.1
|
|
|
|
|
|
.pop()
|
|
|
|
|
|
.map(|e| Entry(min(e.0, level), e.1 + line.as_ref()))
|
|
|
|
|
|
|
|
|
log.1.pop().map(|e| Entry(min(e.0, level), e.1 + msg))
|
|
|
} else {
|
|
|
} else {
|
|
|
None
|
|
|
None
|
|
|
}
|
|
|
}
|
|
|
.unwrap_or_else(|| Entry(level, line.into()));
|
|
|
|
|
|
|
|
|
.unwrap_or_else(|| Entry(level, msg.to_string()));
|
|
|
log.0 = true;
|
|
|
log.0 = true;
|
|
|
log.1.push(entry);
|
|
|
log.1.push(entry);
|
|
|
}
|
|
|
}
|
|
|
fn writeln<S: AsRef<str> + Into<String>>(&self, level: Level, line: S) {
|
|
|
|
|
|
|
|
|
fn writeln(&self, level: Level, msg: &str) {
|
|
|
let mut log = self.log.borrow_mut();
|
|
|
let mut log = self.log.borrow_mut();
|
|
|
log.0 = false;
|
|
|
log.0 = false;
|
|
|
log.1.push(Entry(level, line.into()));
|
|
|
|
|
|
|
|
|
log.1.push(Entry(level, msg.to_string()));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|