Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tjardoo committed Sep 10, 2024
0 parents commit 70c15b6
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/target
/examples/*/target
Cargo.lock
/examples/*/Cargo.lock
15 changes: 15 additions & 0 deletions Cargo.toml
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"]
9 changes: 9 additions & 0 deletions examples/console/Cargo.toml
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" }
21 changes: 21 additions & 0 deletions examples/console/src/main.rs
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(())
}
27 changes: 27 additions & 0 deletions src/drivers/console.rs
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) {}
}
1 change: 1 addition & 0 deletions src/drivers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod console;
59 changes: 59 additions & 0 deletions src/lib.rs
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()
}
}

0 comments on commit 70c15b6

Please sign in to comment.