forked from estk/log4rs
-
Notifications
You must be signed in to change notification settings - Fork 1
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
6 changed files
with
150 additions
and
1 deletion.
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
125 changes: 125 additions & 0 deletions
125
src/append/rolling_file/policy/compound/trigger/onstartup.rs
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,125 @@ | ||
//! The OnStartUp trigger. | ||
//! | ||
//! Requires the `onstartup_trigger` feature. | ||
|
||
use std::sync::Once; | ||
|
||
use crate::append::rolling_file::{policy::compound::trigger::Trigger, LogFile}; | ||
|
||
#[cfg(feature = "config_parsing")] | ||
use crate::config::{Deserialize, Deserializers}; | ||
|
||
/// Configuration for the onstartup trigger. | ||
#[cfg(feature = "config_parsing")] | ||
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default, serde::Deserialize)] | ||
#[serde(deny_unknown_fields)] | ||
pub struct OnStartUpTriggerConfig { | ||
#[serde(default = "default_min_size")] | ||
min_size: u64, | ||
} | ||
|
||
fn default_min_size() -> u64 { | ||
1 | ||
} | ||
|
||
/// A trigger which rolls the log on startup. | ||
#[derive(Debug)] | ||
pub struct OnStartUpTrigger { | ||
min_size: u64, | ||
initial: Once, | ||
} | ||
|
||
impl OnStartUpTrigger { | ||
/// Returns a new trigger which rolls the log on startup. | ||
pub fn new(min_size: u64) -> OnStartUpTrigger { | ||
OnStartUpTrigger { | ||
min_size, | ||
initial: Once::new(), | ||
} | ||
} | ||
} | ||
|
||
impl Trigger for OnStartUpTrigger { | ||
fn trigger(&self, file: &LogFile) -> anyhow::Result<bool> { | ||
if !self.initial.is_completed() { | ||
self.initial.call_once(|| {}); | ||
if file.len_estimate() >= self.min_size { | ||
return Ok(true); | ||
} | ||
} | ||
Ok(false) | ||
} | ||
|
||
fn is_pre_process(&self) -> bool { | ||
true | ||
} | ||
} | ||
|
||
/// A deserializer for the `OnStartUpTrigger`. | ||
/// | ||
/// # Configuration | ||
/// | ||
/// ```yaml | ||
/// kind: onstartup | ||
/// | ||
/// ``` | ||
#[cfg(feature = "config_parsing")] | ||
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default)] | ||
pub struct OnStartUpTriggerDeserializer; | ||
|
||
#[cfg(feature = "config_parsing")] | ||
impl Deserialize for OnStartUpTriggerDeserializer { | ||
type Trait = dyn Trigger; | ||
|
||
type Config = OnStartUpTriggerConfig; | ||
|
||
fn deserialize( | ||
&self, | ||
config: OnStartUpTriggerConfig, | ||
_: &Deserializers, | ||
) -> anyhow::Result<Box<dyn Trigger>> { | ||
Ok(Box::new(OnStartUpTrigger::new(config.min_size))) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn pre_process() { | ||
let trigger = OnStartUpTrigger::new(0); | ||
assert!(trigger.is_pre_process()); | ||
} | ||
|
||
#[test] | ||
fn trigger1() { | ||
let file = tempfile::tempdir().unwrap(); | ||
let logfile = LogFile { | ||
writer: &mut None, | ||
path: file.path(), | ||
len: 0, | ||
}; | ||
|
||
let trigger = OnStartUpTrigger::new(1); | ||
let result = trigger.trigger(&logfile).unwrap(); | ||
assert_eq!(result, false); | ||
} | ||
|
||
#[test] | ||
fn trigger2() { | ||
let file = tempfile::tempdir().unwrap(); | ||
let logfile = LogFile { | ||
writer: &mut None, | ||
path: file.path(), | ||
len: 0, | ||
}; | ||
|
||
let trigger = OnStartUpTrigger::new(0); | ||
let result = trigger.trigger(&logfile).unwrap(); | ||
assert_eq!(result, true); | ||
|
||
let result = trigger.trigger(&logfile).unwrap(); | ||
assert_eq!(result, false); | ||
} | ||
} |
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