|
| 1 | +//! The OnStartUp trigger. |
| 2 | +//! |
| 3 | +//! Requires the `onstartup_trigger` feature. |
| 4 | +
|
| 5 | +use std::sync::Once; |
| 6 | + |
| 7 | +use crate::append::rolling_file::{policy::compound::trigger::Trigger, LogFile}; |
| 8 | + |
| 9 | +#[cfg(feature = "config_parsing")] |
| 10 | +use crate::config::{Deserialize, Deserializers}; |
| 11 | + |
| 12 | +/// Configuration for the onstartup trigger. |
| 13 | +#[cfg(feature = "config_parsing")] |
| 14 | +#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default, serde::Deserialize)] |
| 15 | +#[serde(deny_unknown_fields)] |
| 16 | +pub struct OnStartUpTriggerConfig { |
| 17 | + #[serde(default = "default_min_size")] |
| 18 | + min_size: u64, |
| 19 | +} |
| 20 | + |
| 21 | +#[cfg(feature = "config_parsing")] |
| 22 | +fn default_min_size() -> u64 { |
| 23 | + 1 |
| 24 | +} |
| 25 | + |
| 26 | +/// A trigger which rolls the log on startup. |
| 27 | +#[derive(Debug)] |
| 28 | +pub struct OnStartUpTrigger { |
| 29 | + min_size: u64, |
| 30 | + initial: Once, |
| 31 | +} |
| 32 | + |
| 33 | +impl OnStartUpTrigger { |
| 34 | + /// Returns a new trigger which rolls the log on startup. |
| 35 | + pub fn new(min_size: u64) -> OnStartUpTrigger { |
| 36 | + OnStartUpTrigger { |
| 37 | + min_size, |
| 38 | + initial: Once::new(), |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +impl Trigger for OnStartUpTrigger { |
| 44 | + fn trigger(&self, file: &LogFile) -> anyhow::Result<bool> { |
| 45 | + let mut result = false; |
| 46 | + self.initial.call_once(|| { |
| 47 | + if file.len_estimate() >= self.min_size { |
| 48 | + result = true; |
| 49 | + } |
| 50 | + }); |
| 51 | + Ok(result) |
| 52 | + } |
| 53 | + |
| 54 | + fn is_pre_process(&self) -> bool { |
| 55 | + true |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/// A deserializer for the `OnStartUpTrigger`. |
| 60 | +/// |
| 61 | +/// # Configuration |
| 62 | +/// |
| 63 | +/// ```yaml |
| 64 | +/// kind: onstartup |
| 65 | +/// |
| 66 | +/// ``` |
| 67 | +#[cfg(feature = "config_parsing")] |
| 68 | +#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default)] |
| 69 | +pub struct OnStartUpTriggerDeserializer; |
| 70 | + |
| 71 | +#[cfg(feature = "config_parsing")] |
| 72 | +impl Deserialize for OnStartUpTriggerDeserializer { |
| 73 | + type Trait = dyn Trigger; |
| 74 | + |
| 75 | + type Config = OnStartUpTriggerConfig; |
| 76 | + |
| 77 | + fn deserialize( |
| 78 | + &self, |
| 79 | + config: OnStartUpTriggerConfig, |
| 80 | + _: &Deserializers, |
| 81 | + ) -> anyhow::Result<Box<dyn Trigger>> { |
| 82 | + Ok(Box::new(OnStartUpTrigger::new(config.min_size))) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +#[cfg(test)] |
| 87 | +mod test { |
| 88 | + use super::*; |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn pre_process() { |
| 92 | + let trigger = OnStartUpTrigger::new(0); |
| 93 | + assert!(trigger.is_pre_process()); |
| 94 | + } |
| 95 | + |
| 96 | + fn trigger_with_file_size(file_size: u64) -> (bool, bool) { |
| 97 | + let file = tempfile::tempdir().unwrap(); |
| 98 | + let logfile = LogFile { |
| 99 | + writer: &mut None, |
| 100 | + path: file.path(), |
| 101 | + len: file_size, |
| 102 | + }; |
| 103 | + |
| 104 | + let trigger = OnStartUpTrigger::new(1); // default |
| 105 | + let result1 = trigger.trigger(&logfile).unwrap(); |
| 106 | + let result2 = trigger.trigger(&logfile).unwrap(); |
| 107 | + (result1, result2) |
| 108 | + } |
| 109 | + |
| 110 | + #[test] |
| 111 | + fn trigger() { |
| 112 | + // When the file size < min_size, the trigger should return false. |
| 113 | + assert_eq!(trigger_with_file_size(0), (false, false)); |
| 114 | + // When the file size == min_size, the trigger should return true for the first time. |
| 115 | + assert_eq!(trigger_with_file_size(1), (true, false)); |
| 116 | + // When the file size >= min_size, the trigger should return true for the first time. |
| 117 | + assert_eq!(trigger_with_file_size(2), (true, false)); |
| 118 | + } |
| 119 | +} |
0 commit comments