Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make parking_lot feature fully optional #264

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
4 changes: 2 additions & 2 deletions src/append/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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(())
Expand Down
7 changes: 4 additions & 3 deletions src/append/rolling_file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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)
}
Expand Down
15 changes: 11 additions & 4 deletions src/append/rolling_file/policy/compound/roll/fixed_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
Expand Down
47 changes: 47 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,53 @@ use self::{append::Append, filter::Filter};

type FnvHashMap<K, V> = HashMap<K, V, BuildHasherDefault<FnvHasher>>;

#[cfg(feature = "parking_lot")]
#[allow(dead_code)]
pub(crate) mod sync {
#[derive(Debug)]
pub struct Mutex<T: ?Sized>(parking_lot::Mutex<T>);

impl<T> Mutex<T> {
#[inline]
pub fn new(val: T) -> Self {
Self(parking_lot::Mutex::new(val))
}
}

impl<T: ?Sized> Mutex<T> {
#[inline]
pub fn lock(&self) -> anyhow::Result<parking_lot::MutexGuard<'_, T>> {
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<T: ?Sized>(std::sync::Mutex<T>);

impl<T> Mutex<T> {
#[inline]
pub fn new(val: T) -> Self {
Self(std::sync::Mutex::new(val))
}
}

impl<T: ?Sized> Mutex<T> {
#[inline]
pub fn lock(&self) -> anyhow::Result<std::sync::MutexGuard<'_, T>> {
self.0.lock().map_err(|_| anyhow::anyhow!("Mutex poisoned"))
}
}

pub type Condvar = std::sync::Condvar;
}

#[derive(Debug)]
struct ConfiguredLogger {
level: LevelFilter,
Expand Down