journal_log_writer/log/
config.rs1use journal_core::file::{
2 Compression, DEFAULT_COMPRESS_THRESHOLD, DEFAULT_JOURNAL_FILE_MODE, FieldNamePolicy,
3 normalize_compress_threshold,
4};
5use journal_registry::Origin;
6use std::time::Duration;
7use uuid::Uuid;
8
9#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
12pub enum LogOpenMode {
13 #[default]
16 Lazy,
17 Eager,
19}
20
21#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
23pub enum LogIdentityMode {
24 #[default]
30 Auto,
31 Strict,
33}
34
35#[derive(Debug, Copy, Clone, Default)]
40pub struct RotationPolicy {
41 pub size_of_journal_file: Option<u64>,
43 pub duration_of_journal_file: Option<Duration>,
45 pub number_of_entries: Option<usize>,
47}
48
49impl RotationPolicy {
50 pub fn with_size_of_journal_file(mut self, size_of_journal_file: u64) -> Self {
52 self.size_of_journal_file = Some(size_of_journal_file);
53 self
54 }
55
56 pub fn with_duration_of_journal_file(mut self, duration_of_journal_file: Duration) -> Self {
58 self.duration_of_journal_file = Some(duration_of_journal_file);
59 self
60 }
61
62 pub fn with_number_of_entries(mut self, number_of_entries: usize) -> Self {
64 self.number_of_entries = Some(number_of_entries);
65 self
66 }
67}
68
69#[derive(Debug, Copy, Clone, Default)]
74pub struct RetentionPolicy {
75 pub number_of_journal_files: Option<usize>,
77 pub size_of_journal_files: Option<u64>,
79 pub duration_of_journal_files: Option<Duration>,
81}
82
83impl RetentionPolicy {
84 pub fn with_number_of_journal_files(mut self, number_of_journal_files: usize) -> Self {
86 self.number_of_journal_files = Some(number_of_journal_files);
87 self
88 }
89
90 pub fn with_size_of_journal_files(mut self, size_of_journal_files: u64) -> Self {
92 self.size_of_journal_files = Some(size_of_journal_files);
93 self
94 }
95
96 pub fn with_duration_of_journal_files(mut self, duration_of_journal_files: Duration) -> Self {
98 self.duration_of_journal_files = Some(duration_of_journal_files);
99 self
100 }
101}
102
103#[derive(Debug, Clone)]
105pub struct Config {
106 pub origin: Origin,
107 pub rotation_policy: RotationPolicy,
109 pub retention_policy: RetentionPolicy,
111 pub compression: Compression,
113 pub compression_threshold: usize,
115 pub compact: bool,
117 pub open_mode: LogOpenMode,
119 pub identity_mode: LogIdentityMode,
121 pub boot_id: Option<Uuid>,
123 pub strict_systemd_naming: bool,
128 pub live_publish_every_entries: u64,
134 pub field_name_policy: FieldNamePolicy,
136 pub file_mode: u32,
140}
141
142impl Config {
143 pub fn new(
145 origin: Origin,
146 rotation_policy: RotationPolicy,
147 retention_policy: RetentionPolicy,
148 ) -> Self {
149 Self {
150 origin,
151 rotation_policy,
152 retention_policy,
153 compression: Compression::None,
154 compression_threshold: DEFAULT_COMPRESS_THRESHOLD,
155 compact: false,
156 open_mode: LogOpenMode::Lazy,
157 identity_mode: LogIdentityMode::Auto,
158 boot_id: None,
159 strict_systemd_naming: false,
160 live_publish_every_entries: 1,
161 field_name_policy: FieldNamePolicy::Journald,
162 file_mode: DEFAULT_JOURNAL_FILE_MODE,
163 }
164 }
165
166 pub fn with_rotation_policy(mut self, policy: RotationPolicy) -> Self {
168 self.rotation_policy = policy;
169 self
170 }
171
172 pub fn with_retention_policy(mut self, policy: RetentionPolicy) -> Self {
174 self.retention_policy = policy;
175 self
176 }
177
178 pub fn with_compression(mut self, compression: Compression) -> Self {
179 self.compression = compression;
180 self
181 }
182
183 pub fn with_compression_threshold(mut self, threshold: usize) -> Self {
184 self.compression_threshold = normalize_compress_threshold(threshold);
185 self
186 }
187
188 pub fn with_compact(mut self, compact: bool) -> Self {
189 self.compact = compact;
190 self
191 }
192
193 pub fn with_open_mode(mut self, open_mode: LogOpenMode) -> Self {
194 self.open_mode = open_mode;
195 self
196 }
197
198 pub fn with_identity_mode(mut self, identity_mode: LogIdentityMode) -> Self {
199 self.identity_mode = identity_mode;
200 self
201 }
202
203 pub fn with_boot_id(mut self, boot_id: Uuid) -> Self {
204 self.boot_id = Some(boot_id);
205 self
206 }
207
208 pub fn with_strict_systemd_naming(mut self, strict_systemd_naming: bool) -> Self {
209 self.strict_systemd_naming = strict_systemd_naming;
210 self
211 }
212
213 pub fn with_live_publish_every_entries(mut self, entries: u64) -> Self {
214 self.live_publish_every_entries = entries;
215 self
216 }
217
218 pub fn with_field_name_policy(mut self, policy: FieldNamePolicy) -> Self {
219 self.field_name_policy = policy;
220 self
221 }
222
223 pub fn with_file_mode(mut self, mode: u32) -> Self {
224 assert!(
225 mode <= 0o777,
226 "journal file mode must contain only permission bits"
227 );
228 self.file_mode = mode;
229 self
230 }
231}