src/handlers/formatters/mod.rs

Lines

100.00 %

Functions

100.00 %

Regions

100.00 %

LineCountSource
1
//
2
// File Name:    mod.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
//! # Formatter
26
//!
27
28
// #![allow(unused)]
29
30
mod format_trait;
31
mod format_type;
32
mod formatter;
33
mod iso8601_formatter;
34
mod mock_formatter;
35
mod simple_formatter;
36
mod unixtimestamp_formatter;
37
38
use crate::LogEntry;
39
pub use format_trait::FormatTrait;
40
pub use format_type::FormatType;
41
pub use formatter::Formatter;
42
pub use iso8601_formatter::Iso8601Formatter;
43
pub use mock_formatter::MockFormatter;
44
pub use simple_formatter::SimpleFormatter;
45
pub use unixtimestamp_formatter::UnixTimestampFormatter;
46
47
#[cfg(test)]
48
mod test {
49
    use std::io::{Result, Write};
50
    // use super::*;
51
    use crate::{Level::*, *};
52
    use regex::Regex;
53
54
    #[test]
551
    fn iso8601() {
561
        let re_str = "^
571
dt_fmt: \"%\\+\" - fmt_string: \"\\{dt:35} \\{mod_path}->\\{fn_name} \\[\\{level:7}] \\{message}\"
581
(?:\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{9}\\+\\d{2}:\\d{2}) ->iso8601 \\[INFO   ] This is a test message
591
$";
60
611
        let re = Regex::new(re_str).unwrap();
621
        let mut buf = Vec::new();
63
641
        let le = LogEntry::create(
651
            INFO,
661
            "iso8601".to_string(),
671
            "This is a test message".to_string(),
68
        );
69
701
        let f = FormatType::Iso8601.create(None);
711
        let fs = f.format(&le);
72
        // println!("\n{f:width$}\n{fs}", width = f.width());
731
        writeln!(&mut buf, "\n{f:width$}\n{fs}", width = f.width()).expect("writeln!() failed");
741
        let result = String::from_utf8(buf).unwrap();
75
        // println!("{result}",);
76
771
        assert!(re.is_match(&result));
781
    }
79
80
    #[test]
811
    fn simple_formatter() {
821
        let expected = "
831
dt_fmt: \"\" - fmt_string: \"{mod_path}->{fn_name} [{level:7}] {message}\"
841
->simple_formatter [INFO   ] This is a test message
851
"
861
        .to_string();
87
881
        let mut buf = Vec::new();
89
901
        let le = LogEntry::create(
911
            INFO,
921
            "simple_formatter".to_string(),
931
            "This is a test message".to_string(),
94
        );
95
961
        let f = FormatType::Simple.create(None);
971
        let fs = f.format(&le);
98
        // println!("\n{f:width$} {fs}\n", width = f.width());
991
        writeln!(&mut buf, "\n{f:width$}\n{fs}", width = f.width()).expect("writeln!() failed");
100
1011
        assert_eq!(expected, String::from_utf8(buf).unwrap());
1021
    }
103
104
    #[test]
1051
    fn unix_timestamp() {
1061
        let re_str = "^
1071
dt_fmt: \"%s\\.%f\" - fmt_string: \"\\{dt} \\{mod_path}->\\{fn_name} \\[\\{level:7}] \\{message}\"
1081
(?:\\d{10}\\.\\d{9}) ->unix_timestamp \\[INFO   ] This is a test message
1091
$";
110
1111
        let re = Regex::new(re_str).unwrap();
1121
        let mut buf = Vec::new();
113
1141
        let le = LogEntry::create(
1151
            INFO,
1161
            "unix_timestamp".to_string(),
1171
            "This is a test message".to_string(),
118
        );
119
1201
        let f = FormatType::UnixTimestamp.create(None);
1211
        let fs = f.format(&le);
122
        // println!("\n{f:width$}\n{fs}", width = f.width());
1231
        writeln!(&mut buf, "\n{f:width$}\n{fs}", width = f.width()).expect("writeln!() failed");
1241
        let result = String::from_utf8(buf).unwrap();
125
        // println!("{result}",);
126
1271
        assert!(re.is_match(&result));
1281
    }
129
130
    #[test]
1311
    fn custom() {
1321
        let expected = "\nMockFormatter
1331
MockFormatter
1341
"
1351
        .to_string();
136
1371
        let mut buf = Vec::new();
138
1391
        let le = LogEntry::create(
1401
            INFO,
1411
            "custom".to_string(),
1421
            "Testing MockFormatter".to_string(),
143
        );
1441
        let f = FormatType::Custom.create(Some(Box::new(MockFormatter::new())));
1451
        let fs = f.format(&le);
146
        // println!("\n{f:width$}\n{fs}", width = f.width());
1471
        writeln!(&mut buf, "\n{f:width$}\n{fs}", width = f.width()).expect("writeln!() failed");
148
1491
        assert_eq!(expected, String::from_utf8(buf).unwrap());
1501
    }
151
}