[Add] Logging
This commit is contained in:
parent
d138484c99
commit
d22367feea
2 changed files with 91 additions and 28 deletions
|
|
@ -8,6 +8,9 @@ use std::{
|
|||
};
|
||||
|
||||
use ansi_term::Color;
|
||||
use json::JsonValue;
|
||||
|
||||
use crate::data::communication::CommunicationValue;
|
||||
|
||||
static LOGGER: OnceLock<mpsc::Sender<LogMessage>> = OnceLock::new();
|
||||
|
||||
|
|
@ -31,8 +34,6 @@ struct LogMessage {
|
|||
message: String,
|
||||
}
|
||||
|
||||
/// Initialize the logging subsystem.
|
||||
/// Must be called exactly once during startup.
|
||||
pub fn startup() {
|
||||
let (tx, rx) = mpsc::channel::<LogMessage>();
|
||||
LOGGER.set(tx).expect("Logger already initialized");
|
||||
|
|
@ -62,10 +63,8 @@ pub fn startup() {
|
|||
|
||||
let line = format!("{} {} {} {}", ts, sender, msg.prefix, msg.message);
|
||||
|
||||
// Console (ANSI-colored)
|
||||
println!("{}", colorize(msg.kind, msg.is_error).paint(&line));
|
||||
|
||||
// File (plain text)
|
||||
let _ = writeln!(file, "{}", line);
|
||||
}
|
||||
});
|
||||
|
|
@ -96,9 +95,6 @@ fn fixed_box(content: &str, width: usize) -> String {
|
|||
}
|
||||
}
|
||||
|
||||
/** Internal async logging entry point.
|
||||
* Not exposed publicly; all access goes through macros.
|
||||
*/
|
||||
pub fn log_internal(
|
||||
sender: Option<i64>,
|
||||
kind: PrintType,
|
||||
|
|
@ -120,7 +116,7 @@ pub fn log_internal(
|
|||
});
|
||||
}
|
||||
}
|
||||
/// Log a general informational message.
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! log {
|
||||
// plain
|
||||
|
|
@ -144,7 +140,7 @@ macro_rules! log {
|
|||
$crate::util::logger::log_internal(None, $kind, "", false, format!($($arg)*))
|
||||
};
|
||||
}
|
||||
/// Log an inbound message (`>`).
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! log_in {
|
||||
// sender + actor
|
||||
|
|
@ -168,7 +164,7 @@ macro_rules! log_in {
|
|||
)
|
||||
};
|
||||
}
|
||||
/// Log an outbound message (`<`).
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! log_out {
|
||||
|
||||
|
|
@ -193,7 +189,7 @@ macro_rules! log_out {
|
|||
)
|
||||
};
|
||||
}
|
||||
/// Log an error message (`>>`).
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! log_err {
|
||||
|
||||
|
|
@ -218,3 +214,84 @@ macro_rules! log_err {
|
|||
)
|
||||
};
|
||||
}
|
||||
|
||||
// ******** COMMUNICATION VALUES ********
|
||||
pub fn log_cv_internal(
|
||||
prefix: &'static str,
|
||||
cv: &CommunicationValue,
|
||||
print_type: Option<PrintType>,
|
||||
) {
|
||||
let formatted = format_cv(cv);
|
||||
|
||||
log_internal(
|
||||
Some(cv.get_sender()),
|
||||
print_type.unwrap_or(PrintType::General),
|
||||
prefix,
|
||||
false,
|
||||
formatted,
|
||||
);
|
||||
}
|
||||
|
||||
pub fn format_cv(cv: &CommunicationValue) -> String {
|
||||
let mut parts = Vec::new();
|
||||
|
||||
let sender = cv.get_sender();
|
||||
let receiver = cv.get_receiver();
|
||||
|
||||
if sender > 0 && receiver > 0 {
|
||||
parts.push(format!("{} > {}", sender, receiver));
|
||||
} else if sender > 0 {
|
||||
parts.push(format!("{}", sender));
|
||||
} else if receiver > 0 {
|
||||
parts.push(format!("> {}", receiver));
|
||||
}
|
||||
|
||||
let comm_type = cv.get_type().to_string();
|
||||
parts.push(format!("{}", comm_type));
|
||||
|
||||
let mut data_parts = Vec::new();
|
||||
if let JsonValue::Object(data) = &cv.clone().to_json()["data"] {
|
||||
for (key, value) in data.iter() {
|
||||
let val_string = match value {
|
||||
JsonValue::String(s) => s.clone(),
|
||||
_ => value.dump(),
|
||||
};
|
||||
|
||||
data_parts.push(format!("{} {}", key, val_string));
|
||||
}
|
||||
}
|
||||
|
||||
if !data_parts.is_empty() {
|
||||
parts.push(format!("{}", data_parts.join(", ")));
|
||||
}
|
||||
|
||||
parts.join(": ")
|
||||
}
|
||||
#[macro_export]
|
||||
macro_rules! log_cv {
|
||||
($kind:expr, $cv:expr) => {
|
||||
$crate::util::logger::log_cv_internal("", &$cv, Some($kind))
|
||||
};
|
||||
($cv:expr) => {
|
||||
$crate::util::logger::log_cv_internal("", &$cv, None)
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! log_cv_in {
|
||||
($kind:expr, $cv:expr) => {
|
||||
$crate::util::logger::log_cv_internal("> ", &$cv, Some($kind))
|
||||
};
|
||||
($cv:expr) => {
|
||||
$crate::util::logger::log_cv_internal("> ", &$cv, None)
|
||||
};
|
||||
}
|
||||
#[macro_export]
|
||||
macro_rules! log_cv_out {
|
||||
($kind:expr, $cv:expr) => {
|
||||
$crate::util::logger::log_cv_internal("< ", &$cv, Some($kind))
|
||||
};
|
||||
($cv:expr) => {
|
||||
$crate::util::logger::log_cv_internal("< ", &$cv, None)
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue