Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort of format for CursiveLogWriter #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ version = "0.5.0"
[dependencies]
cursive_core = "0.3"
arraydeque = "0.4"
flexi_logger = "0.22"
flexi_logger = "0.24.2"
lazy_static = "1.4"
log = "0.4"
unicode-width = "0.1"
Expand Down
75 changes: 58 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ lazy_static::lazy_static! {
/// }
/// ```
pub struct FlexiLoggerView {
pub indent: bool,
pub indent: bool
}

pub trait Indentable {
Expand Down Expand Up @@ -299,11 +299,45 @@ impl View for FlexiLoggerView {
}
}

///Possible log items
pub enum LogItems {
DATETIME,
THREAD,
FILE,
FileLine,
LEVEL,
MESSAGE,
S(String)
}

use crate::LogItems::{DATETIME, FILE, FileLine, LEVEL, MESSAGE, THREAD};

/// The `flexi_logger` `LogWriter` implementation for the `FlexiLoggerView`.
///
/// Use the `cursive_flexi_logger` function to create an instance of this struct.
pub struct CursiveLogWriter {
sink: CbSink,
format: Vec<LogItems>,
time_format: String,
}

pub trait FormattableLogWriter {
fn with_format(self, new_format: Vec<LogItems>) -> Self;
fn with_time_format(self, new_format: &str) -> Self;
}

impl FormattableLogWriter for Box<CursiveLogWriter> {
///Set up a custom log format
fn with_format(mut self, new_format: Vec<LogItems>) -> Self {
self.format = new_format;
self
}

/// Set up a custom format for a time
fn with_time_format(mut self, new_format: &str) -> Self {
self.time_format = new_format.to_string();
self
}
}

/// Creates a new `LogWriter` instance for the `FlexiLoggerView`. Use this to
Expand Down Expand Up @@ -343,13 +377,11 @@ pub struct CursiveLogWriter {
pub fn cursive_flexi_logger(siv: &Cursive) -> Box<CursiveLogWriter> {
Box::new(CursiveLogWriter {
sink: siv.cb_sink().clone(),
format: vec![DATETIME, THREAD, LEVEL, FileLine, MESSAGE],
time_format: "%T%.3f".to_string()
})
}

use time::{format_description::FormatItem, macros::format_description};

const FORMAT: &[FormatItem<'static>] = format_description!("%T%.3f");

impl LogWriter for CursiveLogWriter {
fn write(&self, now: &mut DeferredNow, record: &Record) -> std::io::Result<()> {
let color = Color::Dark(match record.level() {
Expand All @@ -361,18 +393,27 @@ impl LogWriter for CursiveLogWriter {
});

let mut line = StyledString::new();
line.append_styled(format!("{}", now.format(FORMAT)), color);
line.append_plain(format!(
" [{}] ",
thread::current().name().unwrap_or("(unnamed)"),
));
line.append_styled(format!("{}", record.level()), color);
line.append_plain(format!(
" <{}:{}> ",
record.file().unwrap_or("(unnamed)"),
record.line().unwrap_or(0),
));
line.append_styled(format!("{}", &record.args()), color);
for item in self.format.iter() {
match item {
DATETIME => line.append_styled(format!("{} ", now.format(&self.time_format)), color),
THREAD => line.append_plain(format!(
"[{}] ",
thread::current().name().unwrap_or("(unnamed) "),
)),
LEVEL => line.append_styled(format!("{} ", record.level()), color),
FILE => line.append_plain(format!(
"<{}> ",
record.file().unwrap_or("(unnamed)"),
)),
FileLine => line.append_plain(format!(
"<{}:{}> ",
record.file().unwrap_or("(unnamed)"),
record.line().unwrap_or(0),
)),
MESSAGE => line.append_styled(format!("{}", &record.args()), color),
LogItems::S(txt) => line.append_plain(txt)
}
}

LOGS.lock().unwrap().push_back(line);
self.sink.send(Box::new(|_siv| {})).map_err(|_| {
Expand Down