src/handlers/formatters/unixtimestamp_formatter.rs

Lines

100.00 %

Functions

100.00 %

Regions

100.00 %

LineCountSource (jump to first uncovered line)
1
//
2
// File Name:    unixtimestamp_formatter.rs
3
// Directory:    src/handlers/formatters
4
// Project Name: flogging
5
//
6
// Copyright (C) 2025 Bradley Willcott
7
//
8
// SPDX-License-Identifier: GPL-3.0-or-later
9
//
10
// This library (crate) is free software: you can redistribute it and/or modify
11
// it under the terms of the GNU General Public License as published by
12
// the Free Software Foundation, either version 3 of the License, or
13
// (at your option) any later version.
14
//
15
// This library (crate) is distributed in the hope that it will be useful,
16
// but WITHOUT ANY WARRANTY; without even the implied warranty of
17
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
// GNU General Public License for more details.
19
//
20
// You should have received a copy of the GNU General Public License
21
// along with this library (crate).  If not, see <https://www.gnu.org/licenses/>.
22
//
23
24
//!
25
//! # UnixTimeStamp Formatter
26
//!
27
28
use std::fmt;
29
use crate::FormatTrait;
30
31
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
32
33
///
34
/// Unix Timestamp format.
35
///
36
/// The first part (before the decimal point) is
37
/// the number of seconds since 1970-01-01 00:00 UTC.
38
///
39
/// The second part is the number of nanoseconds since
40
/// the last whole second.
41
///
42
/// Example:
43
/// ```text
44
/// 1752817859.157970496
45
/// ```
46
/// Template:
47
/// - `dt` in the template would be the datetime string, similar to the above.
48
/// - `mod_path`, `fn_name`, `level`, and `message` all come out of the `LogEntry`
49
///   provided to the [`format()`][UnixTimestampFormatter::format] method.
50
///
51
/// ```ignore
52
/// format!("{dt} {mod_path}->{fn_name} [{level:7}] {message}");
53
/// ```
54
/// Sample output:
55
/// ```text
56
/// 1752818461.051538870 flogging->main [SEVERE ] Hurricanes are windy!
57
/// ```
58
///
59
pub struct UnixTimestampFormatter {
60
    dt_fmt: String,
61
    fmt_string: String,
62
}
63
64
impl UnixTimestampFormatter {
65
    ///
66
    /// Creates a new instance of `UnixTimestampFormatter`.
67
    ///
682
    pub fn new() -> Self {
692
        Self {
702
            dt_fmt: "%s.%f".to_string(),
712
            fmt_string: "{dt} {mod_path}->{fn_name} [{level:7}] {message}".to_string(),
722
        }
732
    }
74
75
    ///
76
    /// Returns the date/time format string.
77
    ///
783
    pub fn dt_fmt(&self) -> String {
793
        self.dt_fmt.clone()
803
    }
81
82
    ///
83
    /// Returns the primary format string.
84
    ///
853
    pub fn fmt_string(&self) -> String {
863
        self.fmt_string.clone()
873
    }
88
}
89
90
impl Default for UnixTimestampFormatter {
912
    fn default() -> Self {
922
        Self::new()
932
    }
94
}
95
96
impl fmt::Display for UnixTimestampFormatter {
971
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
981
        write!(
991
            f,
1001
            "dt_fmt: \"{}\" - fmt_string: \"{}\"",
101
            self.dt_fmt, self.fmt_string
102
        )
1031
    }
104
}
105
106
impl FormatTrait for UnixTimestampFormatter {
1073
    fn format(&self, log_entry: &crate::LogEntry) -> String {
1083
        self.ft_fmt(self.dt_fmt(), self.fmt_string(), log_entry)
1093
    }
110
}