-
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
12 changed files
with
133 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "single" | ||
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 @@ | ||
demo.log |
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,20 @@ | ||
use ftail::Ftail; | ||
use log::LevelFilter; | ||
|
||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
Ftail::new() | ||
.single("logs/demo.log", true, LevelFilter::Trace) | ||
.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 message"); | ||
|
||
log::error!("This is an error message"); | ||
|
||
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
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod console; | ||
pub mod single; | ||
pub mod stdout; |
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,61 @@ | ||
use log::Log; | ||
use std::{ | ||
fs::File, | ||
io::{LineWriter, Write}, | ||
sync::Mutex, | ||
}; | ||
|
||
pub struct SingleLogger { | ||
file: Mutex<LineWriter<File>>, | ||
} | ||
|
||
impl SingleLogger { | ||
pub fn new(path: &str, append: bool) -> Self { | ||
let file = std::fs::OpenOptions::new() | ||
.create(true) | ||
.write(true) | ||
.append(append) | ||
.open(path) | ||
.unwrap(); | ||
|
||
// @todo | ||
// let md = std::fs::metadata("logs").unwrap(); | ||
// let permissions = md.permissions(); | ||
|
||
// if permissions.readonly() { | ||
// return Err(P2000Error::ConfigurationError(format!( | ||
// "directory /logs is readonly" | ||
// ))); | ||
// } | ||
|
||
SingleLogger { | ||
file: Mutex::new(LineWriter::new(file)), | ||
} | ||
} | ||
} | ||
|
||
impl Log for SingleLogger { | ||
fn enabled(&self, _metadata: &log::Metadata) -> bool { | ||
true | ||
} | ||
|
||
fn log(&self, record: &log::Record) { | ||
let now = chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string(); | ||
|
||
let line = format!( | ||
"{} {} {} {}", | ||
now, | ||
record.level(), | ||
record.target(), | ||
record.args() | ||
); | ||
|
||
let mut file = self.file.lock().unwrap(); | ||
writeln!(file, "{}", line).unwrap(); | ||
file.flush().unwrap(); | ||
} | ||
|
||
fn flush(&self) { | ||
self.file.lock().unwrap().flush().unwrap(); | ||
} | ||
} |
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