From c6f41253118bd6d60f129f5675e768e87c2125eb Mon Sep 17 00:00:00 2001 From: Cassy343 Date: Thu, 9 Jun 2022 19:06:37 -0400 Subject: [PATCH] Make parking_lot optional --- Cargo.toml | 8 ++-- src/append/file.rs | 4 +- src/append/rolling_file/mod.rs | 7 +-- .../policy/compound/roll/fixed_window.rs | 15 ++++-- src/lib.rs | 47 +++++++++++++++++++ 5 files changed, 68 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a8435975..504f7dc7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,16 +10,16 @@ keywords = ["log", "logger", "logging", "log4"] edition = '2018' [features] -default = ["all_components", "config_parsing", "yaml_format"] +default = ["all_components", "config_parsing", "yaml_format", "parking_lot"] config_parsing = ["humantime", "serde", "serde-value", "typemap", "log/serde"] yaml_format = ["serde_yaml"] json_format = ["serde_json"] toml_format = ["toml"] -console_appender = ["console_writer", "simple_writer", "pattern_encoder"] -file_appender = ["parking_lot", "simple_writer", "pattern_encoder"] -rolling_file_appender = ["parking_lot", "simple_writer", "pattern_encoder"] +console_appender = ["console_writer", "simple_writer"] +file_appender = ["simple_writer", "pattern_encoder"] +rolling_file_appender = ["simple_writer", "pattern_encoder"] compound_policy = [] delete_roller = [] fixed_window_roller = [] diff --git a/src/append/file.rs b/src/append/file.rs index cf12943d..5e948520 100644 --- a/src/append/file.rs +++ b/src/append/file.rs @@ -4,7 +4,7 @@ use derivative::Derivative; use log::Record; -use parking_lot::Mutex; +use crate::sync::Mutex; use std::{ fs::{self, File, OpenOptions}, io::{self, BufWriter, Write}, @@ -43,7 +43,7 @@ pub struct FileAppender { impl Append for FileAppender { fn append(&self, record: &Record) -> anyhow::Result<()> { - let mut file = self.file.lock(); + let mut file = self.file.lock()?; self.encoder.encode(&mut *file, record)?; file.flush()?; Ok(()) diff --git a/src/append/rolling_file/mod.rs b/src/append/rolling_file/mod.rs index d391abb8..a053c2eb 100644 --- a/src/append/rolling_file/mod.rs +++ b/src/append/rolling_file/mod.rs @@ -18,7 +18,7 @@ use derivative::Derivative; use log::Record; -use parking_lot::Mutex; +use crate::sync::Mutex; use std::{ fs::{self, File, OpenOptions}, io::{self, BufWriter, Write}, @@ -165,7 +165,7 @@ pub struct RollingFileAppender { impl Append for RollingFileAppender { fn append(&self, record: &Record) -> anyhow::Result<()> { // TODO(eas): Perhaps this is better as a concurrent queue? - let mut writer = self.writer.lock(); + let mut writer = self.writer.lock()?; let len = { let writer = self.get_writer(&mut writer)?; @@ -273,7 +273,8 @@ impl RollingFileAppenderBuilder { } // open the log file immediately - appender.get_writer(&mut appender.writer.lock())?; + // unwrap guaranteed to succeed since this is the first time the mutex is being locked + appender.get_writer(&mut appender.writer.lock().unwrap())?; Ok(appender) } diff --git a/src/append/rolling_file/policy/compound/roll/fixed_window.rs b/src/append/rolling_file/policy/compound/roll/fixed_window.rs index 7b78e337..1cfbc5a5 100644 --- a/src/append/rolling_file/policy/compound/roll/fixed_window.rs +++ b/src/append/rolling_file/policy/compound/roll/fixed_window.rs @@ -4,7 +4,7 @@ use anyhow::bail; #[cfg(feature = "background_rotation")] -use parking_lot::{Condvar, Mutex}; +use crate::sync::{Condvar, Mutex}; #[cfg(feature = "background_rotation")] use std::sync::Arc; use std::{ @@ -128,7 +128,7 @@ impl Roll for FixedWindowRoller { // Wait for the state to be ready to roll let &(ref lock, ref cvar) = &*self.cond_pair.clone(); - let mut ready = lock.lock(); + let mut ready = lock.lock()?; if !*ready { cvar.wait(&mut ready); } @@ -142,11 +142,18 @@ impl Roll for FixedWindowRoller { let cond_pair = self.cond_pair.clone(); // rotate in the separate thread std::thread::spawn(move || { + use std::io::Write; + let &(ref lock, ref cvar) = &*cond_pair; - let mut ready = lock.lock(); + let mut ready = match lock.lock() { + Ok(guard) => guard, + Err(e) => { + let _ = writeln!(io::stderr(), "log4rs, error rotating: {}", e); + return; + } + }; if let Err(e) = rotate(pattern, compression, base, count, temp) { - use std::io::Write; let _ = writeln!(io::stderr(), "log4rs, error rotating: {}", e); } *ready = true; diff --git a/src/lib.rs b/src/lib.rs index 0178843c..090b7c9e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -210,6 +210,53 @@ use self::{append::Append, filter::Filter}; type FnvHashMap = HashMap>; +#[cfg(feature = "parking_lot")] +#[allow(dead_code)] +pub(crate) mod sync { + #[derive(Debug)] + pub struct Mutex(parking_lot::Mutex); + + impl Mutex { + #[inline] + pub fn new(val: T) -> Self { + Self(parking_lot::Mutex::new(val)) + } + } + + impl Mutex { + #[inline] + pub fn lock(&self) -> anyhow::Result> { + Ok(self.0.lock()) + } + } + + pub type Condvar = parking_lot::Condvar; +} + + +#[cfg(not(feature = "parking_lot"))] +#[allow(dead_code)] +pub(crate) mod sync { + #[derive(Debug)] + pub struct Mutex(std::sync::Mutex); + + impl Mutex { + #[inline] + pub fn new(val: T) -> Self { + Self(std::sync::Mutex::new(val)) + } + } + + impl Mutex { + #[inline] + pub fn lock(&self) -> anyhow::Result> { + self.0.lock().map_err(|_| anyhow::anyhow!("Mutex poisoned")) + } + } + + pub type Condvar = std::sync::Condvar; +} + #[derive(Debug)] struct ConfiguredLogger { level: LevelFilter,