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
148 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
123 changes: 123 additions & 0 deletions
123
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,123 @@ | ||
//! 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}; | ||
|
||
static INITIAL: Once = Once::new(); | ||
|
||
/// 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(Copy, Clone, Eq, PartialEq, Hash, Debug, Default)] | ||
pub struct OnStartUpTrigger { | ||
min_size: u64, | ||
} | ||
|
||
impl OnStartUpTrigger { | ||
/// Returns a new trigger which rolls the log on startup. | ||
pub fn new(min_size: u64) -> OnStartUpTrigger { | ||
OnStartUpTrigger { min_size } | ||
} | ||
} | ||
|
||
impl Trigger for OnStartUpTrigger { | ||
fn trigger(&self, file: &LogFile) -> anyhow::Result<bool> { | ||
if !INITIAL.is_completed() { | ||
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