src/handlers/formatters/simple_formatter.rs

Lines

100.00 %

Functions

100.00 %

Regions

100.00 %

LineCountSource
1
//
2
// File Name:    simple_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
//! # Simple Formatter
26
//!
27
28
use std::fmt;
29
use crate::FormatTrait;
30
31
///
32
/// Simple format.
33
///
34
/// Template:
35
/// - `mod_path`, `fn_name`, `level`, and `message` all come out of the `LogEntry`
36
///   provided to the [`format()`][SimpleFormatter::format] method.
37
/// ```ignore
38
/// format!("{mod_path}->{fn_name} [{level:7}] {message}");
39
/// ```
40
/// Sample output:
41
/// ```text
42
/// flogging->main [INFO   ] It is cloudy today.
43
/// ```
44
///
45
46
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
47
pub struct SimpleFormatter {
48
    dt_fmt: String,
49
    fmt_string: String,
50
}
51
52
impl SimpleFormatter {
53
    ///
54
    /// Creates a new instance of `SimpleFormatter`.
55
    ///
5652
    pub fn new() -> Self {
5752
        Self {
5852
            dt_fmt: "".to_string(),
5952
            fmt_string: "{mod_path}->{fn_name} [{level:7}] {message}".to_string(),
6052
        }
6152
    }
62
63
    ///
64
    /// Returns the date/time format string.
65
    ///
6692
    pub fn dt_fmt(&self) -> String {
6792
        self.dt_fmt.clone()
6892
    }
69
70
    ///
71
    /// Returns the primary format string.
72
    ///
7392
    pub fn fmt_string(&self) -> String {
7492
        self.fmt_string.clone()
7592
    }
76
}
77
78
impl Default for SimpleFormatter {
7950
    fn default() -> Self {
8050
        Self::new()
8150
    }
82
}
83
84
impl fmt::Display for SimpleFormatter {
8518
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
8618
        write!(
8718
            f,
8818
            "dt_fmt: \"{}\" - fmt_string: \"{}\"",
89
            self.dt_fmt, self.fmt_string
90
        )
9118
    }
92
}
93
94
impl FormatTrait for SimpleFormatter {
9592
    fn format(&self, log_entry: &crate::LogEntry) -> String {
9692
        self.ft_fmt(self.dt_fmt(), self.fmt_string(), log_entry)
9792
    }
98
}