-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
133 additions
and
51 deletions.
There are no files selected for viewing
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
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
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,27 @@ | ||
use crate::writer::LogWriter; | ||
|
||
use super::Formatter; | ||
|
||
pub struct DefaultFormatter<'a> { | ||
record: &'a log::Record<'a>, | ||
} | ||
|
||
impl DefaultFormatter<'_> { | ||
pub fn new<'a>(record: &'a log::Record<'a>) -> DefaultFormatter<'a> { | ||
DefaultFormatter { record } | ||
} | ||
} | ||
|
||
impl<'a> Formatter for DefaultFormatter<'a> { | ||
fn format(&self) -> String { | ||
let writer = LogWriter::new(); | ||
|
||
format!( | ||
"{} {} {} {}", | ||
writer.get_datetime(), | ||
writer.get_level(self.record), | ||
writer.get_target(self.record), | ||
writer.get_args(self.record), | ||
) | ||
} | ||
} |
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,6 @@ | ||
pub mod default; | ||
pub mod readable; | ||
|
||
pub trait Formatter { | ||
fn format(&self) -> String; | ||
} |
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,46 @@ | ||
use crate::{ansi_escape::TextStyling, writer::LogWriter}; | ||
|
||
use super::Formatter; | ||
|
||
pub struct ReadableFormatter<'a> { | ||
record: &'a log::Record<'a>, | ||
} | ||
|
||
impl ReadableFormatter<'_> { | ||
pub fn new<'a>(record: &'a log::Record<'a>) -> ReadableFormatter<'a> { | ||
ReadableFormatter { record } | ||
} | ||
} | ||
|
||
impl<'a> Formatter for ReadableFormatter<'a> { | ||
fn format(&self) -> String { | ||
let writer = LogWriter::new(); | ||
|
||
let mut result = String::new(); | ||
|
||
let level = match self.record.level() { | ||
log::Level::Trace => writer.get_level(self.record).bold().black(), | ||
log::Level::Debug => writer.get_level(self.record).bold().blue(), | ||
log::Level::Info => writer.get_level(self.record).bold().green(), | ||
log::Level::Warn => writer.get_level(self.record).bold().yellow(), | ||
log::Level::Error => writer.get_level(self.record).bold().red(), | ||
}; | ||
|
||
result.push_str(&format!("{} · {}\n", writer.get_datetime().black(), level)); | ||
result.push_str(&format!("{}\n", writer.get_args(self.record).bold())); | ||
|
||
let file = writer.get_file(self.record); | ||
let line = writer.get_line(self.record); | ||
|
||
if file.is_some() && line.is_some() { | ||
result.push_str(&format!( | ||
"{}{}{}\n", | ||
file.unwrap().black(), | ||
":".black(), | ||
line.unwrap().black() | ||
)); | ||
} | ||
|
||
result | ||
} | ||
} |
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,33 @@ | ||
use log::Record; | ||
|
||
pub struct LogWriter {} | ||
|
||
impl LogWriter { | ||
pub fn new() -> Self { | ||
Self {} | ||
} | ||
|
||
pub fn get_datetime(&self) -> String { | ||
chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string() | ||
} | ||
|
||
pub fn get_level(&self, record: &Record) -> String { | ||
record.level().to_string() | ||
} | ||
|
||
pub fn get_target(&self, record: &Record) -> String { | ||
record.target().to_string() | ||
} | ||
|
||
pub fn get_args(&self, record: &Record) -> String { | ||
record.args().to_string() | ||
} | ||
|
||
pub fn get_file(&self, record: &Record) -> Option<String> { | ||
record.file().map(|f| f.to_string()) | ||
} | ||
|
||
pub fn get_line(&self, record: &Record) -> Option<u32> { | ||
record.line() | ||
} | ||
} |