Skip to content

Commit

Permalink
logger: remove the parking_lot dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
chensoft committed Mar 11, 2024
1 parent af88d88 commit 39ddb4a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 18 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Expand Up @@ -23,7 +23,6 @@ anyhow = { version = "1.0", features = ["backtrace"] }
chrono = "0.4"
encoder = "0.2"
backtrace = "0.3"
parking_lot = "0.12"

[dev-dependencies]
tokio = { version = "1.36", features = ["full"] }
Expand Down
6 changes: 6 additions & 0 deletions HISTORY.md
Expand Up @@ -7,6 +7,12 @@
- highlight keywords in console output
- sampling by level

## [Unreleased] - 2024-xx-xx

### Changed

- Remove the parking_lot dependency

## [0.3.2] - 2024-03-07

### Added
Expand Down
4 changes: 1 addition & 3 deletions src/define.rs
@@ -1,9 +1,7 @@
//! Log levels and Encode trait
pub(crate) use std::io::Write;
pub(crate) use std::path::Path;
pub(crate) use std::cell::RefCell;
pub(crate) use parking_lot::Mutex;
pub(crate) use parking_lot::ReentrantMutex;
pub(crate) use std::sync::Mutex;

/// Any support
pub trait Any: std::any::Any {
Expand Down
16 changes: 8 additions & 8 deletions src/logger.rs
Expand Up @@ -12,7 +12,7 @@ pub struct Logger {
level: Level, // log level limit
alloc: usize, // record init capacity

records: Mutex<RefCell<Vec<Record>>>, // records pool
records: Mutex<Vec<Record>>, // records pool
plugins: Vec<Box<dyn Plugin>>, // middlewares
targets: Vec<Box<dyn Target>>, // output targets
default: Option<&'static dyn Target>, // default output
Expand All @@ -31,7 +31,7 @@ impl Logger {
Self {
level: LEVEL_TRACE,
alloc: 512,
records: Mutex::new(RefCell::new(vec![])),
records: Mutex::new(vec![]),
plugins: vec![],
targets: vec![],
default,
Expand Down Expand Up @@ -193,10 +193,9 @@ impl Logger {
return None;
}

let record = {
let guard = self.records.lock();
let mut array = guard.borrow_mut();
array.pop()
let record = match self.records.lock() {
Ok(mut obj) => obj.pop(),
Err(_) => return None
};

let mut record = match record {
Expand Down Expand Up @@ -258,7 +257,8 @@ impl Logger {
/// invoke it manually.
#[inline]
pub fn reuse(&self, record: Record) {
let guard = self.records.lock();
guard.borrow_mut().push(record);
if let Ok(mut obj) = self.records.lock() {
obj.push(record)
}
}
}
14 changes: 8 additions & 6 deletions src/target.rs
Expand Up @@ -56,7 +56,7 @@ impl Target for StderrTarget {
/// ```
pub struct FileTarget {
/// file handle
pub file: ReentrantMutex<RefCell<std::fs::File>>,
pub file: Mutex<std::fs::File>,
}

impl FileTarget {
Expand All @@ -66,17 +66,19 @@ impl FileTarget {
std::fs::create_dir_all(dir)?;
}

Ok(Self {file: ReentrantMutex::new(RefCell::new(std::fs::OpenOptions::new().create(true).append(true).open(path)?))})
Ok(Self {file: Mutex::new(std::fs::OpenOptions::new().create(true).append(true).open(path)?)})
}
}

impl Target for FileTarget {
#[inline]
fn write(&self, buf: &[u8]) {
let file = self.file.lock();
match file.borrow_mut().write_all(buf) {
Ok(_) => {}
Err(err) => { let _ = std::io::stderr().write_all(format!("Error: {}", err).as_bytes()); }
match self.file.lock() {
Ok(mut obj) => match obj.write_all(buf) {
Ok(_) => {}
Err(err) => { eprintln!("Error: {}", err); }
}
Err(err) => { eprintln!("Error: {}", err); }
};
}
}

0 comments on commit 39ddb4a

Please sign in to comment.