use crate::core::config::LogLevel;
#[derive(Debug)]
pub struct LogInfo<'a> {
pub message: &'a str,
pub level: LogLevel,
pub timestamp: &'a str,
pub file: Option<&'a str>,
pub line: Option<u32>,
pub thread: Option<&'a str>,
}
impl<'a> LogInfo<'a> {
pub fn new(message: &'a str, level: LogLevel, timestamp: &'a str) -> Self {
Self {
message,
level,
timestamp,
file: None,
line: None,
thread: None,
}
}
pub fn with_location(mut self, file: &'a str, line: u32) -> Self {
self.file = Some(file);
self.line = Some(line);
self
}
pub fn with_thread(mut self, thread: &'a str) -> Self {
self.thread = Some(thread);
self
}
}
pub fn format_basic(info: &LogInfo) -> String {
format!(
"[{}] {}: {}",
info.timestamp,
info.level.as_str(),
info.message
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_basic_formatting() {
let info = LogInfo::new(
"Test message",
LogLevel::Info,
"2025-09-06 15:30:45"
);
let result = format_basic(&info);
assert_eq!(result, "[2025-09-06 15:30:45] INFO: Test message");
}
#[test]
fn test_different_log_levels() {
let timestamp = "2025-09-06 15:30:45";
let message = "Test message";
let error_info = LogInfo::new(message, LogLevel::Error, timestamp);
assert_eq!(format_basic(&error_info), "[2025-09-06 15:30:45] ERROR: Test message");
let debug_info = LogInfo::new(message, LogLevel::Debug, timestamp);
assert_eq!(format_basic(&debug_info), "[2025-09-06 15:30:45] DEBUG: Test message");
}
#[test]
fn test_loginfo_builder() {
let info = LogInfo::new("Test", LogLevel::Warning, "2025-09-06 15:30:45")
.with_location("main.rs", 42)
.with_thread("main");
let result = format_basic(&info);
assert_eq!(result, "[2025-09-06 15:30:45] WARNING: Test");
}
}