Skip to content

Commit

Permalink
Add trigger onstartup
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirreke committed Feb 2, 2024
1 parent 6c6ace0 commit 6ce95ac
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ delete_roller = []
fixed_window_roller = []
size_trigger = []
time_trigger = ["rand"]
onstartup_trigger = []
json_encoder = ["serde", "serde_json", "chrono", "log-mdc", "log/serde", "thread-id"]
pattern_encoder = ["chrono", "log-mdc", "thread-id"]
ansi_writer = []
Expand All @@ -43,6 +44,7 @@ all_components = [
"fixed_window_roller",
"size_trigger",
"time_trigger",
"onstartup_trigger",
"json_encoder",
"pattern_encoder",
"threshold_filter"
Expand Down
12 changes: 11 additions & 1 deletion docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ The new component is the _policy_ field. A policy must have the _kind_ field lik
other components, the default (and only supported) policy is `kind: compound`.

The _trigger_ field is used to dictate when the log file should be rolled. It
supports two types: `size`, and `time`.
supports three types: `size`, `time` and `onstartup`.

For `size`, it require a _limit_ field. The _limit_ field is a string which defines the maximum file size
prior to a rolling of the file. The limit field requires one of the following
Expand Down Expand Up @@ -233,6 +233,16 @@ trigger:
max_random_delay: 0
```

For `onstartup`, it has a optional field, _min_size_. It indicates the minimum size the file must have to roll over. A size of zero will cause a roll over no matter what the file size is. The default value is 1, which will prevent rolling over an empty file.

i.e.

```yml
trigger:
kind: onstartup
min_size: 1
```

The _roller_ field supports two types: delete, and fixed_window. The delete
roller does not take any other configuration fields. The fixed_window roller
supports three fields: pattern, base, and count. The most current log file will
Expand Down
3 changes: 3 additions & 0 deletions src/append/rolling_file/policy/compound/trigger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ pub mod size;
#[cfg(feature = "time_trigger")]
pub mod time;

#[cfg(feature = "onstartup_trigger")]
pub mod onstartup;

/// A trait which identifies if the active log file should be rolled over.
pub trait Trigger: fmt::Debug + Send + Sync + 'static {
/// Determines if the active log file should be rolled over.
Expand Down
123 changes: 123 additions & 0 deletions src/append/rolling_file/policy/compound/trigger/onstartup.rs
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);
}
}
8 changes: 8 additions & 0 deletions src/config/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ impl Default for Deserializers {
append::rolling_file::policy::compound::trigger::time::TimeTriggerDeserializer,
);

#[cfg(feature = "onstartup_trigger")]
d.insert(
"onstartup",
append::rolling_file::policy::compound::trigger::onstartup::OnStartUpTriggerDeserializer,
);

#[cfg(feature = "json_encoder")]
d.insert("json", encode::json::JsonEncoderDeserializer);

Expand Down Expand Up @@ -267,6 +273,8 @@ impl Deserializers {
/// * Requires the `size_trigger` feature.
/// * "time" -> `TimeTriggerDeserializer`
/// * Requires the `time_trigger` feature.
/// * "onstartup" -> `OnStartUpTriggerDeserializer`
/// * Requires the `onstartup_trigger` feature.
pub fn new() -> Deserializers {
Deserializers::default()
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
//! - Triggers
//! - [size](append/rolling_file/policy/compound/trigger/size/struct.SizeTriggerDeserializer.html#configuration): requires the `size_trigger` feature
//! - [time](append/rolling_file/policy/compound/trigger/tine/struct.TimeTriggerDeserializer.html#configuration): requires the `time_trigger` feature
//! - [onstartup](append/rolling_file/policy/compound/trigger/tine/struct.OnStartUpTriggerDeserializer.html#configuration): requires the `onstartup_trigger` feature
//!
//! ## Encoders
//!
Expand Down

0 comments on commit 6ce95ac

Please sign in to comment.