[Add] More logging
This commit is contained in:
parent
c38f4a820c
commit
d4a1b6922b
4 changed files with 93 additions and 19 deletions
|
|
@ -4,6 +4,7 @@ use async_tungstenite::{
|
||||||
tokio::{TokioAdapter, connect_async},
|
tokio::{TokioAdapter, connect_async},
|
||||||
tungstenite::protocol::Message,
|
tungstenite::protocol::Message,
|
||||||
};
|
};
|
||||||
|
use crossterm::style::Print;
|
||||||
use dashmap::DashMap;
|
use dashmap::DashMap;
|
||||||
use futures::prelude::*;
|
use futures::prelude::*;
|
||||||
use json::{JsonValue, number::Number};
|
use json::{JsonValue, number::Number};
|
||||||
|
|
@ -17,19 +18,19 @@ use tokio::{
|
||||||
use tokio_native_tls::TlsStream;
|
use tokio_native_tls::TlsStream;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::log_err;
|
|
||||||
use crate::{
|
use crate::{
|
||||||
data::{
|
data::{
|
||||||
communication::{CommunicationType, CommunicationValue, DataTypes},
|
communication::{CommunicationType, CommunicationValue, DataTypes},
|
||||||
user::UserStatus,
|
user::UserStatus,
|
||||||
},
|
},
|
||||||
get_private_key, log, log_in, log_out,
|
get_private_key, log, log_cv_out, log_in, log_out,
|
||||||
rho::rho_manager::{self, RHO_CONNECTIONS, connection_count},
|
rho::rho_manager::{self, RHO_CONNECTIONS, connection_count},
|
||||||
util::{
|
util::{
|
||||||
crypto_helper::{decrypt_b64, secret_key_to_base64},
|
crypto_helper::{decrypt_b64, secret_key_to_base64},
|
||||||
logger::PrintType,
|
logger::PrintType,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
use crate::{log_cv_in, log_err};
|
||||||
|
|
||||||
pub struct WaitingTask {
|
pub struct WaitingTask {
|
||||||
pub task: Box<dyn Fn(Arc<OmegaConnection>, CommunicationValue) -> bool + Send + Sync>,
|
pub task: Box<dyn Fn(Arc<OmegaConnection>, CommunicationValue) -> bool + Send + Sync>,
|
||||||
|
|
@ -349,12 +350,12 @@ impl OmegaConnection {
|
||||||
match msg {
|
match msg {
|
||||||
Some(Ok(Message::Text(msg))) => {
|
Some(Ok(Message::Text(msg))) => {
|
||||||
let cv = CommunicationValue::from_json(&msg);
|
let cv = CommunicationValue::from_json(&msg);
|
||||||
|
log_cv_in!(PrintType::Omega, cv);
|
||||||
if cv.is_type(CommunicationType::pong) || cv.is_type(CommunicationType::ping) {
|
if cv.is_type(CommunicationType::pong) || cv.is_type(CommunicationType::ping) {
|
||||||
self.handle_pong(&cv, true).await;
|
self.handle_pong(&cv, true).await;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let msg_id = cv.get_id();
|
let msg_id = cv.get_id();
|
||||||
log_in!(0, PrintType::Omega, "{}", &cv.to_json().to_string());
|
|
||||||
if let Some(task) = WAITING_TASKS.remove(&msg_id) {
|
if let Some(task) = WAITING_TASKS.remove(&msg_id) {
|
||||||
if (task.1.task)(self.clone(), cv.clone()) {
|
if (task.1.task)(self.clone(), cv.clone()) {
|
||||||
continue;
|
continue;
|
||||||
|
|
@ -371,9 +372,7 @@ impl OmegaConnection {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn send_message(&self, cv: &CommunicationValue) {
|
pub async fn send_message(&self, cv: &CommunicationValue) {
|
||||||
if !cv.is_type(CommunicationType::ping) {
|
log_cv_out!(PrintType::Omega, cv);
|
||||||
log_out!(0, PrintType::Omega, "{}", &cv.to_json().to_string());
|
|
||||||
}
|
|
||||||
let msg = cv.to_json().to_string();
|
let msg = cv.to_json().to_string();
|
||||||
|
|
||||||
let mut guard = self.write.lock().await;
|
let mut guard = self.write.lock().await;
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ use crate::{
|
||||||
},
|
},
|
||||||
omega::omega_connection::OmegaConnection,
|
omega::omega_connection::OmegaConnection,
|
||||||
};
|
};
|
||||||
use crate::{get_private_key, get_public_key, log_in, log_out};
|
use crate::{get_private_key, get_public_key, log_cv_in, log_in, log_out};
|
||||||
|
|
||||||
pub struct ClientConnection {
|
pub struct ClientConnection {
|
||||||
pub sender: Arc<RwLock<WebSocketSender<Compat<tokio::net::TcpStream>>>>,
|
pub sender: Arc<RwLock<WebSocketSender<Compat<tokio::net::TcpStream>>>>,
|
||||||
|
|
@ -131,12 +131,7 @@ impl ClientConnection {
|
||||||
self.handle_ping(cv).await;
|
self.handle_ping(cv).await;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
log_in!(
|
log_cv_in!(PrintType::Client, cv);
|
||||||
self.get_user_id().await,
|
|
||||||
PrintType::Client,
|
|
||||||
"{}",
|
|
||||||
&cv.to_json().to_string()
|
|
||||||
);
|
|
||||||
let identified = *self.identified.read().await;
|
let identified = *self.identified.read().await;
|
||||||
let challenged = *self.challenged.read().await;
|
let challenged = *self.challenged.read().await;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ use crate::calls::call_group::CallGroup;
|
||||||
use crate::calls::call_manager;
|
use crate::calls::call_manager;
|
||||||
use crate::get_private_key;
|
use crate::get_private_key;
|
||||||
use crate::get_public_key;
|
use crate::get_public_key;
|
||||||
|
use crate::log_cv_in;
|
||||||
use crate::log_err;
|
use crate::log_err;
|
||||||
use crate::log_in;
|
use crate::log_in;
|
||||||
use crate::log_out;
|
use crate::log_out;
|
||||||
|
|
@ -154,12 +155,7 @@ impl IotaConnection {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
log_in!(
|
log_cv_in!(PrintType::Iota, cv);
|
||||||
self.get_iota_id().await,
|
|
||||||
PrintType::Iota,
|
|
||||||
"{}",
|
|
||||||
cv.to_json().to_string()
|
|
||||||
);
|
|
||||||
|
|
||||||
let identified = *self.identified.read().await;
|
let identified = *self.identified.read().await;
|
||||||
let challenged = *self.challenged.read().await;
|
let challenged = *self.challenged.read().await;
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,9 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use ansi_term::Color;
|
use ansi_term::Color;
|
||||||
|
use json::JsonValue;
|
||||||
|
|
||||||
|
use crate::data::communication::CommunicationValue;
|
||||||
|
|
||||||
static LOGGER: OnceLock<mpsc::Sender<LogMessage>> = OnceLock::new();
|
static LOGGER: OnceLock<mpsc::Sender<LogMessage>> = OnceLock::new();
|
||||||
|
|
||||||
|
|
@ -138,3 +141,84 @@ macro_rules! log_err {
|
||||||
$crate::util::logger::log_internal($sender, $kind, ">>", true, format!($($arg)*))
|
$crate::util::logger::log_internal($sender, $kind, ">>", true, format!($($arg)*))
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ******** COMMUNICATION VALUES ********
|
||||||
|
pub fn log_cv_internal(
|
||||||
|
prefix: &'static str,
|
||||||
|
cv: &CommunicationValue,
|
||||||
|
print_type: Option<PrintType>,
|
||||||
|
) {
|
||||||
|
let formatted = format_cv(cv);
|
||||||
|
|
||||||
|
log_internal(
|
||||||
|
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