use chrono::{DateTime, Local};
use std::{fmt, time::Instant};
use super::Level;
#[derive(Debug)]
pub struct LogEntry {
pub(crate) timestamp: DateTime<Local>,
pub(crate) mod_path: String,
pub(crate) fn_name: String,
pub(crate) level: Level,
pub(crate) message: String,
}
impl fmt::Display for LogEntry {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{} : {}->{} ({}) {}",
self.timestamp, self.mod_path, self.fn_name, self.level, self.message
)
}
}
#[allow(unused)]
impl LogEntry {
pub(crate) fn create(level: Level, fn_name: String, message: String) -> LogEntry {
LogEntry {
timestamp: Local::now(),
mod_path: String::new(),
fn_name,
level,
message,
}
}
pub(crate) fn fn_name(&self) -> String {
self.fn_name.clone()
}
pub(crate) fn level(&self) -> Level {
self.level
}
pub(crate) fn message(&self) -> String {
self.message.clone()
}
pub(crate) fn mod_path(&self) -> String {
self.mod_path.clone()
}
pub(crate) fn set_fn_name(&mut self, fn_name: String) {
self.fn_name = fn_name.clone();
}
pub(crate) fn set_mod_path(&mut self, mod_path: String) {
self.mod_path = mod_path.clone();
}
pub(crate) fn timestamp(&self) -> DateTime<Local> {
self.timestamp
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn to_string() {
let mut log_entry =
LogEntry::create(Level::INFO, "to_string".to_string(), "message".to_string());
log_entry.set_mod_path(module_path!().to_owned());
println!("\nlog_entry: {log_entry}\n");
assert_eq!(log_entry.level(), Level::INFO);
assert_eq!(log_entry.message(), "message".to_string());
assert_eq!(log_entry.fn_name(), "to_string".to_string());
assert_eq!(log_entry.mod_path(), module_path!());
assert!(log_entry.timestamp().timestamp() > 0);
log_entry.set_fn_name("fn_name".to_owned());
assert_eq!(log_entry.fn_name(), "fn_name".to_string());
}
}