src/logger/log_entry.rs

Lines

100.00 %

Functions

100.00 %

Regions

100.00 %

LineCountSource (jump to first uncovered line)
1
//
2
// File Name:    log_entry.rs
3
// Directory:    src/logger
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
//! # Log Entry
26
//!
27
28
use chrono::{DateTime, Local};
29
use std::{fmt, time::Instant};
30
use super::Level;
31
32
///
33
/// Used to provide relevant information about each log entry.
34
///
35
#[derive(Debug)]
36
pub struct LogEntry {
37
    pub(crate) timestamp: DateTime<Local>,
38
    pub(crate) mod_path: String,
39
    ///
40
    /// This is the name of the function/method inside which this
41
    /// log entry was generated.
42
    ///
43
    pub(crate) fn_name: String,
44
    pub(crate) level: Level,
45
    pub(crate) message: String,
46
}
47
48
impl fmt::Display for LogEntry {
491
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
501
        write!(
511
            f,
521
            "{} : {}->{} ({}) {}",
53
            self.timestamp, self.mod_path, self.fn_name, self.level, self.message
54
        )
551
    }
56
}
57
58
#[allow(unused)]
59
impl LogEntry {
60109
    pub(crate) fn create(level: Level, fn_name: String, message: String) -> LogEntry {
61109
        LogEntry {
62109
            timestamp: Local::now(),
63109
            mod_path: String::new(),
64109
            fn_name,
65109
            level,
66109
            message,
67109
        }
68109
    }
69
702
    pub(crate) fn fn_name(&self) -> String {
712
        self.fn_name.clone()
722
    }
73
749
    pub(crate) fn level(&self) -> Level {
759
        self.level
769
    }
77
785
    pub(crate) fn message(&self) -> String {
795
        self.message.clone()
805
    }
81
821
    pub(crate) fn mod_path(&self) -> String {
831
        self.mod_path.clone()
841
    }
85
861
    pub(crate) fn set_fn_name(&mut self, fn_name: String) {
871
        self.fn_name = fn_name.clone();
881
    }
89
90105
    pub(crate) fn set_mod_path(&mut self, mod_path: String) {
91105
        self.mod_path = mod_path.clone();
92105
    }
93
941
    pub(crate) fn timestamp(&self) -> DateTime<Local> {
951
        self.timestamp
961
    }
97
}
98
99
#[cfg(test)]
100
mod tests {
101
    use super::*;
102
103
    #[test]
1041
    fn to_string() {
1051
        let mut log_entry =
1061
            LogEntry::create(Level::INFO, "to_string".to_string(), "message".to_string());
107
1081
        log_entry.set_mod_path(module_path!().to_owned());
1091
        println!("\nlog_entry: {log_entry}\n");
110
1111
        assert_eq!(log_entry.level(), Level::INFO);
1121
        assert_eq!(log_entry.message(), "message".to_string());
1131
        assert_eq!(log_entry.fn_name(), "to_string".to_string());
1141
        assert_eq!(log_entry.mod_path(), module_path!());
1151
        assert!(log_entry.timestamp().timestamp() > 0);
116
1171
        log_entry.set_fn_name("fn_name".to_owned());
1181
        assert_eq!(log_entry.fn_name(), "fn_name".to_string());
1191
    }
120
}