From b2bd381ebc9ce0e92667405be7acfc61764998d0 Mon Sep 17 00:00:00 2001 From: Ivan Kud Date: Thu, 18 Apr 2024 09:16:24 +0200 Subject: [PATCH] improved log messages (#139) --- Cargo.toml | 2 +- python/zmq/zmq_reqrep_native.py | 4 ++++ savant_core/src/transport/zeromq/reader.rs | 26 +++++++++++++++++----- savant_core/src/transport/zeromq/writer.rs | 6 ++++- savant_core/src/utils.rs | 8 +++++++ 5 files changed, 38 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 75c7bff5..29d72813 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ savant_core = { path = "savant_core" } savant_core_py = { path = "savant_core_py" } [workspace.package] -version = "0.2.25" +version = "0.2.26" edition = "2021" authors = ["Ivan Kudriavtsev "] description = "Savant Rust core functions library" diff --git a/python/zmq/zmq_reqrep_native.py b/python/zmq/zmq_reqrep_native.py index b9eb65ba..a709e0f7 100644 --- a/python/zmq/zmq_reqrep_native.py +++ b/python/zmq/zmq_reqrep_native.py @@ -4,6 +4,10 @@ from savant_rs.utils import gen_frame from savant_rs.utils.serialization import Message from savant_rs.zmq import WriterConfigBuilder, ReaderConfigBuilder, BlockingWriter, BlockingReader +from savant_rs.logging import set_log_level +from savant_rs.logging import LogLevel + +set_log_level(LogLevel.Info) socket_name = "tcp://127.0.0.1:3333" diff --git a/savant_core/src/transport/zeromq/reader.rs b/savant_core/src/transport/zeromq/reader.rs index 7daea8b7..9bd20785 100644 --- a/savant_core/src/transport/zeromq/reader.rs +++ b/savant_core/src/transport/zeromq/reader.rs @@ -1,5 +1,6 @@ use anyhow::bail; use log::{debug, error, info, warn}; +use std::str::from_utf8; use std::time::Duration; use zmq::Context; @@ -8,6 +9,7 @@ use crate::transport::zeromq::{ create_ipc_dirs, set_ipc_permissions, MockSocketResponder, ReaderConfig, ReaderSocketType, RoutingIdFilter, Socket, SocketProvider, CONFIRMATION_MESSAGE, ZMQ_LINGER, }; +use crate::utils::bytes_to_hex_string; use mini_moka::sync::{Cache, CacheBuilder}; pub struct Reader> { @@ -143,10 +145,22 @@ impl + Default> Reader { } pub fn blacklist_source(&mut self, source: &[u8]) { + info!( + target: "savant_rs::zeromq::reader", + "Blacklisting source '{}' for endpoint '{}'", + from_utf8(source).unwrap_or(&bytes_to_hex_string(source)), + self.config.endpoint() + ); self.source_blacklist_cache.insert(source.to_vec(), ()); } pub fn is_blacklisted(&self, source: &[u8]) -> bool { + debug!( + target: "savant_rs::zeromq::reader", + "Checking if source '{}' is blacklisted for endpoint '{}'", + from_utf8(source).unwrap_or(&bytes_to_hex_string(source)), + self.config.endpoint() + ); self.source_blacklist_cache.contains_key(&source.to_vec()) } @@ -222,7 +236,7 @@ impl + Default> Reader { debug!( target: "savant_rs::zeromq::reader", "Received message from blacklisted source {:?} from ZeroMQ socket for endpoint {}", - topic, + from_utf8(topic).unwrap_or(&bytes_to_hex_string(topic)), self.config.endpoint() ); @@ -260,10 +274,10 @@ impl + Default> Reader { if !self.config.topic_prefix_spec().matches(topic) { debug!( target: "savant_rs::zeromq::reader", - "Received message with invalid topic from ZeroMQ socket for endpoint {}. Expected topic to match spec {:?}, but got {:?}", + "Received message with invalid topic from ZeroMQ socket for endpoint {}. Expected topic to match spec {:?}, but got {}", self.config.endpoint(), self.config.topic_prefix_spec(), - topic + from_utf8(topic).unwrap_or(&bytes_to_hex_string(topic)) ); if self.config.socket_type() == &ReaderSocketType::Rep { @@ -290,10 +304,10 @@ impl + Default> Reader { } else { debug!( target: "savant_rs::zeromq::reader", - "Received message with invalid routing ID from ZeroMQ socket for endpoint {}. Got topic = {:?}, routing_id = {:?}", + "Received message with invalid routing ID from ZeroMQ socket for endpoint {}. Got topic = {}, routing_id = {}", self.config.endpoint(), - topic, - routing_id + from_utf8(topic).unwrap_or(&bytes_to_hex_string(topic)), + routing_id.map(|r| bytes_to_hex_string(r)).unwrap_or(String::new()) ); Ok(ReaderResult::routing_id_mismatch(topic, &routing_id)) diff --git a/savant_core/src/transport/zeromq/writer.rs b/savant_core/src/transport/zeromq/writer.rs index 5d4736e1..d4a570bb 100644 --- a/savant_core/src/transport/zeromq/writer.rs +++ b/savant_core/src/transport/zeromq/writer.rs @@ -5,8 +5,10 @@ use crate::transport::zeromq::{ create_ipc_dirs, set_ipc_permissions, MockSocketResponder, Socket, SocketProvider, WriterConfig, WriterSocketType, CONFIRMATION_MESSAGE, ZMQ_LINGER, }; +use crate::utils::bytes_to_hex_string; use anyhow::bail; use log::{debug, info, warn}; +use std::str::from_utf8; pub struct Writer> { context: Option, @@ -151,7 +153,9 @@ impl + Default> Writer { .collect::>(); debug!( target: "savant_rs::zeromq::writer", - "Sending message to ZeroMQ socket: {:?} {:?}", topic, m); + "Sending message to ZeroMQ socket: {} {:?}", + from_utf8(topic).unwrap_or(&bytes_to_hex_string(topic)), + m); let mut send_retries = *self.config.send_retries(); while send_retries >= 0 { let res = socket.send_multipart(&parts, 0); diff --git a/savant_core/src/utils.rs b/savant_core/src/utils.rs index cf2a8e0b..e6e9dff8 100644 --- a/savant_core/src/utils.rs +++ b/savant_core/src/utils.rs @@ -1,2 +1,10 @@ pub mod default_once; pub mod iter; +use std::fmt::Write; + +pub fn bytes_to_hex_string(bytes: &[u8]) -> String { + bytes.iter().fold(String::new(), |mut output, b| { + let _ = write!(output, "{b:02X}"); + output + }) +}