-
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
0 parents
commit 70c15b6
Showing
7 changed files
with
136 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/target | ||
/examples/*/target | ||
Cargo.lock | ||
/examples/*/Cargo.lock |
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,15 @@ | ||
[package] | ||
name = "ftail" | ||
version = "0.1.0" | ||
edition = "2021" | ||
license = "MIT" | ||
description = "Ftail is a simple logging library for Rust" | ||
repository = "https://github.com/tjardoo/ftail" | ||
keywords = ["log", "logging"] | ||
|
||
[dependencies] | ||
log = { version = "0.4", features = ["std"] } | ||
chrono = "0.4" | ||
|
||
[workspace] | ||
members = ["examples/console"] |
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,9 @@ | ||
[package] | ||
name = "console" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] | ||
log = "0.4" | ||
ftail = { path = "../../../ftail" } |
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,21 @@ | ||
use ftail::{drivers::console::ConsoleLogger, Ftail}; | ||
|
||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
println!("Hello, world!"); | ||
|
||
Ftail::new() | ||
.add_driver(ConsoleLogger::new(), log::LevelFilter::Debug) | ||
.init()?; | ||
|
||
log::trace!("This is a trace message"); | ||
|
||
log::debug!("This is a debug message"); | ||
|
||
log::info!(target: "foo", "bar"); | ||
|
||
log::warn!("This is a warning"); | ||
|
||
log::error!("This is an error"); | ||
|
||
Ok(()) | ||
} |
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 @@ | ||
pub struct ConsoleLogger {} | ||
|
||
impl ConsoleLogger { | ||
pub fn new() -> Box<ConsoleLogger> { | ||
Box::new(ConsoleLogger {}) | ||
} | ||
} | ||
|
||
impl log::Log for ConsoleLogger { | ||
fn enabled(&self, metadata: &log::Metadata) -> bool { | ||
metadata.level() <= log::Level::Debug | ||
} | ||
|
||
fn log(&self, record: &log::Record) { | ||
if self.enabled(record.metadata()) { | ||
println!( | ||
"{} [{}] {} {}", | ||
chrono::Local::now().format("%Y-%m-%d %H:%M:%S"), | ||
record.level(), | ||
record.target(), | ||
record.args() | ||
); | ||
} | ||
} | ||
|
||
fn flush(&self) {} | ||
} |
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 @@ | ||
pub mod console; |
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,59 @@ | ||
use log::Log; | ||
|
||
pub mod drivers; | ||
|
||
pub struct LogDriver { | ||
driver: Box<dyn Log>, | ||
level: log::LevelFilter, | ||
} | ||
|
||
pub struct Ftail { | ||
drivers: Vec<LogDriver>, | ||
} | ||
|
||
impl Ftail { | ||
pub fn new() -> Self { | ||
Self { | ||
drivers: Vec::new(), | ||
} | ||
} | ||
|
||
pub fn add_driver(mut self, driver: Box<dyn Log>, level: log::LevelFilter) -> Self { | ||
self.drivers.push(LogDriver { driver, level }); | ||
|
||
self | ||
} | ||
|
||
pub fn init(self) -> Result<(), log::SetLoggerError> { | ||
log::set_max_level(log::LevelFilter::Trace); | ||
log::set_boxed_logger(Box::new(self)) | ||
} | ||
} | ||
|
||
impl Log for Ftail { | ||
fn enabled(&self, metadata: &log::Metadata) -> bool { | ||
self.drivers | ||
.iter() | ||
.any(|driver| metadata.level() <= driver.level && driver.driver.enabled(metadata)) | ||
} | ||
|
||
fn log(&self, record: &log::Record) { | ||
for driver in &self.drivers { | ||
if driver.level >= record.level() { | ||
driver.driver.log(record); | ||
} | ||
} | ||
} | ||
|
||
fn flush(&self) { | ||
for driver in &self.drivers { | ||
driver.driver.flush(); | ||
} | ||
} | ||
} | ||
|
||
impl Default for Ftail { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} |