fastlogging 0.8.0

A lightning fast logger, written in Rust, for multiple platforms and languages.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
use std::fmt;
use std::fs;
use std::path::{Path, PathBuf};
use std::str;

const CONFIG_FILE_SIZE_MAX: u64 = 4096;

use crate::LoggingError;
use crate::WriterConfigEnum;
use crate::WriterEnum;
use crate::WriterTypeEnum;
use crate::callback::CallbackWriter;
use crate::level2string;
use crate::{
    ClientWriter, ConsoleWriter, FileWriter, LevelSyms, LoggingServer, MessageStructEnum, NOTSET,
    SyslogWriter,
};

use super::LoggingInstance;

#[repr(C)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum FileMerge {
    Replace,      // Replace complete configuration by config file contents
    Merge,        // Add only new writers
    MergeReplace, // Add new writers and replace existing writers
}

#[repr(C)]
#[derive(Debug, Serialize, Deserialize)]
pub struct FileConfig {
    pub(crate) level: u8,
    pub(crate) domain: String,
    pub(crate) hostname: Option<String>,
    pub(crate) pname: String,
    pub(crate) pid: u32,
    pub(crate) tname: bool,
    pub(crate) tid: bool,
    pub(crate) structured: MessageStructEnum,
    pub(crate) level2sym: LevelSyms,
    pub(crate) configs: Vec<WriterConfigEnum>,
}

impl Default for FileConfig {
    fn default() -> Self {
        Self {
            level: NOTSET,
            domain: "".to_string(),
            hostname: None,
            pname: "".to_string(),
            pid: 0,
            tname: false,
            tid: false,
            structured: MessageStructEnum::String,
            level2sym: LevelSyms::Sym,
            configs: Vec::new(),
        }
    }
}

#[repr(C)]
#[derive(Debug)]
pub struct ConfigFile {
    pub(crate) path: PathBuf,
    pub(crate) config: FileConfig, // Configuration from file
}

pub fn default_config_file() -> (PathBuf, Vec<u8>) {
    if let Ok(path) = std::env::var("FASTLOGGING_CONFIG_FILE") {
        let path = PathBuf::from(path);
        if path.exists()
            && let Some(ext) = path.extension()
        {
            let ext = ext.as_encoded_bytes().to_ascii_lowercase();
            if ext == b"json" || ext == b"yaml" || ext == b"xml" {
                return (path, ext);
            }
        }
    }
    #[cfg(feature = "config_json")]
    if Path::new("fastlogging.json").exists() {
        return (PathBuf::from("fastlogging.json"), b"json".to_vec());
    }
    #[cfg(feature = "config_yaml")]
    if Path::new("fastlogging.yaml").exists() {
        return (PathBuf::from("fastlogging.yaml"), b"yaml".to_vec());
    }
    #[cfg(feature = "config_xml")]
    if Path::new("fastlogging.xml").exists() {
        return (PathBuf::from("fastlogging.xml"), b"xml".to_vec());
    }
    (PathBuf::new(), Vec::new())
}

impl ConfigFile {
    pub fn new() -> Self {
        Self {
            path: PathBuf::new(),
            config: FileConfig::default(),
        }
    }

    pub fn load(&mut self, path: &Path) -> Result<(), LoggingError> {
        // Initialize settings with default settings from optional config file.
        let (path, lextension) = if let Some(extension) = path.extension() {
            (
                path.to_owned(),
                extension.as_encoded_bytes().to_ascii_lowercase(),
            )
        } else {
            return Err(LoggingError::InvalidValue(
                "Config file has no extension".to_string(),
            ));
        };
        if !path.is_file() {
            return Err(LoggingError::InvalidFile(format!(
                "File {path:?} not found or not accessible"
            )));
        }
        match fs::metadata(&path) {
            Ok(m) => {
                if m.len() > CONFIG_FILE_SIZE_MAX {
                    return Err(LoggingError::InvalidValue(
                        "Config file is too big".to_string(),
                    ));
                }
            }
            Err(err) => {
                return Err(LoggingError::InvalidValue(format!(
                    "Failed to read config file metadata: {err:?}"
                )));
            }
        }
        let data = match fs::read_to_string(&path) {
            Ok(d) => d,
            Err(err) => {
                return Err(LoggingError::InvalidValue(format!(
                    "Failed to read config file: {err:?}"
                )));
            }
        };
        self.config = match if lextension == b"json" {
            ConfigFile::from_json(&path, &data)
        } else if lextension == b"xml" {
            ConfigFile::from_xml(&path, &data)
        } else if lextension == b"yaml" {
            ConfigFile::from_yaml(&path, &data)
        } else {
            return Err(LoggingError::InvalidValue(format!(
                "Unsupported config file type {}",
                str::from_utf8(&lextension).unwrap()
            )));
        } {
            Ok(d) => d,
            Err(err) => {
                return Err(LoggingError::InvalidValue(format!(
                    "Failed to read config file {path:?}: {err:?}"
                )));
            }
        };
        self.path = path;
        Ok(())
    }

    pub fn save(&mut self, path: Option<&Path>) -> Result<(), LoggingError> {
        let path = path.unwrap_or(&self.path);
        let (path, lextension) = {
            if let Some(extension) = path.extension() {
                (
                    path.to_owned(),
                    extension.as_encoded_bytes().to_ascii_lowercase(),
                )
            } else {
                return Err(LoggingError::InvalidValue(
                    "Config file has no extension".to_string(),
                ));
            }
        };
        let data = (if lextension == b"json" {
            self.to_json()
        } else if lextension == b"xml" {
            self.to_xml()
        } else if lextension == b"yaml" {
            self.to_yaml()
        } else {
            return Err(LoggingError::InvalidValue(format!(
                "Unsupported config file type {}",
                str::from_utf8(&lextension).unwrap()
            )));
        })?;
        fs::write(&path, data)?;
        self.path = path.to_path_buf();
        Ok(())
    }

    pub fn merge(
        &self,
        instance: &mut LoggingInstance,
        merge: FileMerge,
    ) -> Result<(), LoggingError> {
        if !self.path.is_file() {
            return Ok(());
        }
        if merge == FileMerge::Replace {
            instance.level = self.config.level;
            instance.domain = self.config.domain.clone();
            instance.hostname = self.config.hostname.clone();
            instance.tname = self.config.tname;
            instance.tid = self.config.tid;
            instance.structured = self.config.structured.clone();
            instance.level2sym = self.config.level2sym.clone();
            for (_wid, writer) in instance.writers.drain() {
                match writer {
                    WriterEnum::Root => {}
                    WriterEnum::Console(mut console_writer) => console_writer.shutdown()?,
                    WriterEnum::File(mut file_writer) => file_writer.shutdown()?,
                    WriterEnum::Client(mut client_writer) => client_writer.shutdown()?,
                    WriterEnum::Server(mut logging_server) => logging_server.shutdown()?,
                    WriterEnum::Callback(mut callback_writer) => callback_writer.shutdown()?,
                    WriterEnum::Syslog(mut syslog_writer) => syslog_writer.shutdown()?,
                }
            }
        } else {
            if self.config.level != NOTSET {
                instance.level = self.config.level;
            }
            if !self.config.domain.is_empty() {
                instance.domain = self.config.domain.clone();
            }
            if let Some(ref hostname) = self.config.hostname {
                instance.hostname = Some(hostname.clone());
            }
        }
        for config in self.config.configs.iter() {
            match config {
                WriterConfigEnum::Root(root_config) => {
                    instance.level = root_config.level;
                    instance.domain = root_config.domain.clone();
                    instance.hostname = root_config.hostname.clone();
                    instance.tname = root_config.tname;
                    instance.tid = root_config.tid;
                    instance.structured = root_config.structured.clone();
                    instance.level2sym = root_config.level2sym.clone();
                }
                WriterConfigEnum::Console(console_config) => {
                    let configs = instance.get_filtered_writer_configs(WriterTypeEnum::Console);
                    if merge == FileMerge::MergeReplace {
                        instance.remove_writers(Some(configs.into_keys().collect::<Vec<_>>()));
                    } else if merge == FileMerge::Merge && configs.is_empty() {
                        instance.add_writer(WriterEnum::Console(Box::new(ConsoleWriter::new(
                            console_config.clone(),
                            instance.stop.clone(),
                        )?)));
                    }
                }
                WriterConfigEnum::File(file_config) => {
                    let configs = instance.get_filtered_writer_configs(WriterTypeEnum::File(
                        file_config.path.to_str().unwrap().to_string(),
                    ));
                    if merge == FileMerge::MergeReplace {
                        instance.remove_writers(Some(configs.into_keys().collect::<Vec<_>>()));
                    } else if merge == FileMerge::Merge && configs.is_empty() {
                        instance.add_writer(WriterEnum::File(Box::new(FileWriter::new(
                            file_config.clone(),
                            instance.stop.clone(),
                        )?)));
                    }
                }
                WriterConfigEnum::Client(client_config) => {
                    let configs = instance.get_filtered_writer_configs(WriterTypeEnum::Client(
                        client_config.get_address_port(),
                    ));
                    if merge == FileMerge::MergeReplace {
                        instance.remove_writers(Some(configs.into_keys().collect::<Vec<_>>()));
                    } else if merge == FileMerge::Merge && configs.is_empty() {
                        instance.add_writer(WriterEnum::Client(Box::new(ClientWriter::new(
                            client_config.clone(),
                            instance.stop.clone(),
                        )?)));
                    }
                }
                WriterConfigEnum::Server(server_config) => {
                    let configs = instance.get_filtered_writer_configs(WriterTypeEnum::Server(
                        server_config.get_address_port(),
                    ));
                    if merge == FileMerge::MergeReplace {
                        instance.remove_writers(Some(configs.into_keys().collect::<Vec<_>>()));
                    } else if merge == FileMerge::Merge && configs.is_empty() {
                        instance.add_writer(WriterEnum::Server(Box::new(LoggingServer::new(
                            server_config.clone(),
                            instance.server_tx.clone(),
                            instance.stop.clone(),
                        )?)));
                    }
                }
                WriterConfigEnum::Callback(callback_config) => {
                    let configs = instance.get_filtered_writer_configs(WriterTypeEnum::Callback);
                    if merge == FileMerge::MergeReplace {
                        instance.remove_writers(Some(configs.into_keys().collect::<Vec<_>>()));
                    } else if merge == FileMerge::Merge && configs.is_empty() {
                        instance.add_writer(WriterEnum::Callback(Box::new(CallbackWriter::new(
                            callback_config.clone(),
                            instance.stop.clone(),
                        )?)));
                    }
                }
                WriterConfigEnum::Syslog(syslog_config) => {
                    let configs = instance.get_filtered_writer_configs(WriterTypeEnum::Syslog);
                    if merge == FileMerge::MergeReplace {
                        instance.remove_writers(Some(configs.into_keys().collect::<Vec<_>>()));
                    } else if merge == FileMerge::Merge && configs.is_empty() {
                        instance.add_writer(WriterEnum::Syslog(Box::new(SyslogWriter::new(
                            syslog_config.clone(),
                            instance.stop.clone(),
                        )?)));
                    }
                }
            }
        }
        Ok(())
    }

    pub fn from_instance(path: &Path, instance: &LoggingInstance) -> Self {
        Self {
            path: path.to_path_buf(),
            config: FileConfig {
                level: instance.level,
                domain: instance.domain.clone(),
                hostname: instance.hostname.clone(),
                pname: instance.pname.clone(),
                pid: instance.pid,
                tname: instance.tname,
                tid: instance.tid,
                structured: instance.structured.clone(),
                level2sym: instance.level2sym.clone(),
                configs: instance
                    .get_writer_configs()
                    .into_values()
                    .collect::<Vec<_>>(),
            },
        }
    }

    pub fn from_json(path: &Path, data: &str) -> Result<FileConfig, LoggingError> {
        #[cfg(feature = "config_json")]
        let file_data = serde_json::from_str(data).map_err(|e| {
            LoggingError::InvalidValue(format!("Failed to read config file {path:?}: {e:?}"))
        });
        #[cfg(not(feature = "config_json"))]
        let file_data = Err(LoggingError::InvalidValue(
            "Support for JSON type config files is not enabled".to_string(),
        ));
        file_data
    }

    pub fn to_json(&self) -> Result<String, LoggingError> {
        #[cfg(feature = "config_json")]
        let data = serde_json::to_string_pretty(&self.config).map_err(|e| {
            LoggingError::InvalidValue(format!("Failed to serialize file configuration: {e:?}"))
        });
        #[cfg(not(feature = "config_json"))]
        let data = Err(LoggingError::InvalidValue(
            "Support for JSON type config files is not enabled".to_string(),
        ));
        data
    }

    pub fn from_xml(path: &Path, data: &str) -> Result<FileConfig, LoggingError> {
        #[cfg(feature = "config_xml")]
        let file_data = quick_xml::de::from_str(data).map_err(|e| {
            LoggingError::InvalidValue(format!("Failed to read config file {path:?}: {e:?}"))
        });
        #[cfg(not(feature = "config_xml"))]
        let file_data = Err(LoggingError::InvalidValue(
            "Support for XML type config files is not enabled".to_string(),
        ));
        file_data
    }

    pub fn to_xml(&self) -> Result<String, LoggingError> {
        #[cfg(feature = "config_xml")]
        let data = quick_xml::se::to_string(&self.config).map_err(|e| {
            LoggingError::InvalidValue(format!("Failed to serialize file configuration: {e:?}"))
        });
        #[cfg(not(feature = "config_xml"))]
        let data = Err(LoggingError::InvalidValue(
            "Support for XML type config files is not enabled".to_string(),
        ));
        data
    }

    pub fn from_yaml(path: &Path, data: &str) -> Result<FileConfig, LoggingError> {
        #[cfg(feature = "config_yaml")]
        let file_data = serde_yaml::from_str(data).map_err(|e| {
            LoggingError::InvalidValue(format!("Failed to read config file {path:?}: {e:?}"))
        });
        #[cfg(not(feature = "config_yaml"))]
        let file_data = Err(LoggingError::InvalidValue(
            "Support for YAML type config files is not enabled".to_string(),
        ));
        file_data
    }

    pub fn to_yaml(&self) -> Result<String, LoggingError> {
        #[cfg(feature = "config_yaml")]
        let data = serde_yaml::to_string(&self.config).map_err(|e| {
            LoggingError::InvalidValue(format!("Failed to serialize file configuration: {e:?}"))
        });
        #[cfg(not(feature = "config_yaml"))]
        let data = Err(LoggingError::InvalidValue(
            "Support for YAML type config files is not enabled".to_string(),
        ));
        data
    }
}

impl fmt::Display for ConfigFile {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let config = &self.config;
        write!(
            f,
            "path={:?}\n\
                   level={:?}\n\
                   domain={:?}\n\
                   hostname={:?}\n\
                   pname={:?}\n\
                   pid={}\n\
                   tname={:?}\n\
                   tid={}\n\
                   structured={:?}\n\
                   configs={:?}",
            self.path,
            level2string(&config.level2sym, config.level),
            config.domain,
            config.hostname,
            config.pname,
            config.pid,
            config.tname,
            config.tid,
            config.structured,
            config.configs,
        )
    }
}