[Add] Logging
This commit is contained in:
parent
d138484c99
commit
d22367feea
2 changed files with 91 additions and 28 deletions
|
|
@ -7,7 +7,7 @@ use crate::sql::sql::{self, get_by_user_id, get_by_username, get_iota_by_id, get
|
|||
use crate::sql::user_online_tracker::{self};
|
||||
use crate::util::crypto_helper::encrypt;
|
||||
use crate::util::logger::PrintType;
|
||||
use crate::{get_private_key, get_public_key, log_in, log_out};
|
||||
use crate::{get_private_key, get_public_key, log_cv_in, log_cv_out, log_in, log_out};
|
||||
use actix::Addr;
|
||||
use base64::{Engine as _, engine::general_purpose::STANDARD};
|
||||
use dashmap::DashMap;
|
||||
|
|
@ -50,14 +50,7 @@ impl OmikronConnection {
|
|||
pub async fn send_message(&self, cv: &CommunicationValue) {
|
||||
let text = cv.to_json().to_string();
|
||||
|
||||
if !cv.is_type(CommunicationType::pong) {
|
||||
log_out!(
|
||||
*self.omikron_id.read().await,
|
||||
PrintType::Omikron,
|
||||
"{}",
|
||||
text
|
||||
);
|
||||
}
|
||||
log_cv_out!(PrintType::Omikron, cv);
|
||||
|
||||
self.ws_addr.read().await.do_send(WsSendMessage(text));
|
||||
}
|
||||
|
|
@ -75,18 +68,11 @@ impl OmikronConnection {
|
|||
|
||||
pub async fn handle_message(self: Arc<Self>, message: String) {
|
||||
let cv = CommunicationValue::from_json(&message);
|
||||
log_cv_in!(PrintType::Omikron, cv);
|
||||
if cv.is_type(CommunicationType::ping) {
|
||||
self.handle_ping(cv).await;
|
||||
return;
|
||||
}
|
||||
|
||||
log_in!(
|
||||
*self.omikron_id.read().await,
|
||||
PrintType::Omikron,
|
||||
"{}",
|
||||
message
|
||||
);
|
||||
|
||||
if let Some((_, task)) = self.waiting_tasks.remove(&cv.get_id()) {
|
||||
let _ = task(self.clone(), cv.clone());
|
||||
return;
|
||||
|
|
|
|||
|
|
@ -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