[Add] Propper logging design

This commit is contained in:
Alex Emmet 2026-03-30 21:52:45 +02:00
commit de8b03bc84
4 changed files with 222 additions and 218 deletions

View file

@ -13,16 +13,13 @@ use ttp_core::{CommunicationValue, DataTypes, DataValue};
use crate::{
APP_STATE,
gui::{
elements::log_card::{LogEntry, Sender, UiLogEntry},
ui::UNIQUE,
},
gui::{elements::log_card::LogEntry, ui::UNIQUE},
langu::language_manager,
};
static LOGGER: OnceLock<mpsc::Sender<LogMessage>> = OnceLock::new();
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[allow(unused)]
pub enum PrintType {
Call,
@ -31,6 +28,20 @@ pub enum PrintType {
Omikron,
Omega,
General,
Command,
}
impl PrintType {
pub fn prefix_color(self) -> Color {
match self {
PrintType::Call => Color::Magenta,
PrintType::Client => Color::Green,
PrintType::Iota => Color::Yellow,
PrintType::Omikron => Color::Blue,
PrintType::Omega => Color::Cyan,
PrintType::General => Color::LightCyan,
PrintType::Command => Color::LightGreen,
}
}
}
struct LogMessage {
@ -38,10 +49,8 @@ struct LogMessage {
prefix: String,
kind: PrintType,
is_error: bool,
translation_key: Option<String>,
format_args: Vec<String>,
message: Option<String>,
}
@ -73,18 +82,8 @@ pub fn startup() {
msg.message.unwrap_or_default()
};
let ts = fixed_box(&msg.timestamp_ms.to_string(), 13);
let sender = match msg.kind {
PrintType::Client => Sender::User,
PrintType::Call
| PrintType::Iota
| PrintType::Omikron
| PrintType::Omega
| PrintType::General => Sender::System,
};
let entry = LogEntry::new(sender, resolved_message, msg.is_error);
let timestamp = format_timestamp_inline(msg.timestamp_ms);
let entry = LogEntry::new(msg.kind, resolved_message, msg.is_error);
let prefix = if msg.prefix.is_empty() {
String::new()
@ -92,30 +91,18 @@ pub fn startup() {
format!("{} ", msg.prefix)
};
let line = format!("{}| {}", prefix, entry.message,);
let _ = writeln!(
file,
"{} {} {}",
ts,
line,
format_timestamp_inline(entry.timestamp_ms)
"{} {}{}",
fixed_box(&msg.timestamp_ms.to_string(), 13),
prefix,
entry.message
);
let color = colorize(msg.kind, msg.is_error);
let _ = writeln!(file, " {}", timestamp);
let ui_entry = UiLogEntry {
sender,
message: entry.message.clone(),
timestamp_ms: entry.timestamp_ms,
is_error: msg.is_error,
color,
};
{
let mut state = APP_STATE.lock().unwrap();
state.push_log(ui_entry);
}
let mut state = APP_STATE.lock().unwrap();
state.push_log(entry.into());
}
});
}
@ -128,6 +115,16 @@ fn format_timestamp_inline(timestamp_ms: u128) -> String {
format!("[{:02}:{:02}:{:02}]", hours, minutes, seconds)
}
fn fixed_box(content: &str, width: usize) -> String {
let s: String = content.chars().take(width).collect();
let len = s.chars().count();
if len < width {
format!("[{}{}]", " ".repeat(width - len), s)
} else {
s
}
}
fn colorize(kind: PrintType, is_error: bool) -> Color {
if is_error {
return Color::Red;
@ -139,17 +136,8 @@ fn colorize(kind: PrintType, is_error: bool) -> Color {
PrintType::Iota => Color::Yellow,
PrintType::Omikron => Color::Blue,
PrintType::Omega => Color::Cyan,
PrintType::General => Color::White,
}
}
fn fixed_box(content: &str, width: usize) -> String {
let s: String = content.chars().take(width).collect();
let len = s.chars().count();
if len < width {
format!("[{}{}]", " ".repeat(width - len), s)
} else {
s
PrintType::General => Color::LightCyan,
PrintType::Command => Color::LightGreen,
}
}
@ -217,6 +205,7 @@ macro_rules! log_t {
)
};
}
#[macro_export]
macro_rules! log_t_err {
($key:expr) => {
@ -240,13 +229,32 @@ macro_rules! log_t_err {
};
}
/// Log a command message.
#[macro_export]
macro_rules! log_command {
($($arg:tt)*) => {
$crate::util::logger::log_internal(
$crate::util::logger::PrintType::Command,
"".to_string(),
false,
format!($($arg)*)
)
};
}
/// Log a general informational message.
#[macro_export]
macro_rules! log {
($($arg:tt)*) => {
$crate::util::logger::log_internal($crate::util::logger::PrintType::General, "".to_string(), false, format!($($arg)*))
$crate::util::logger::log_internal(
$crate::util::logger::PrintType::General,
"".to_string(),
false,
format!($($arg)*)
)
};
}
/// Log an inbound message (`>`).
#[macro_export]
macro_rules! log_in {
@ -259,6 +267,7 @@ macro_rules! log_in {
)
};
}
/// Log an outbound message (`<`).
#[macro_export]
macro_rules! log_out {
@ -271,6 +280,7 @@ macro_rules! log_out {
)
};
}
/// Log an error message (`>>`).
#[macro_export]
macro_rules! log_err {
@ -410,6 +420,7 @@ macro_rules! log_cv_in {
$crate::util::logger::log_cv_internal("> ", &$cv, None)
};
}
#[macro_export]
macro_rules! log_cv_out {
($kind:expr, $cv:expr) => {