From 97ec70053722006f4f879dfa25193c7e3a050ebb Mon Sep 17 00:00:00 2001 From: Gris Ge Date: Wed, 28 Aug 2024 15:01:20 +0800 Subject: [PATCH] logging: Send commander logs to user Also send commander log to user session. Signed-off-by: Gris Ge --- src/daemon/commander/commander_thread.rs | 53 +++++++++++++++++++++--- src/lib/event.rs | 37 ++++++++--------- src/lib/logging.rs | 16 ++++++- src/lib/plugin_native.rs | 23 +++------- 4 files changed, 85 insertions(+), 44 deletions(-) diff --git a/src/daemon/commander/commander_thread.rs b/src/daemon/commander/commander_thread.rs index 1bfaefd..1395540 100644 --- a/src/daemon/commander/commander_thread.rs +++ b/src/daemon/commander/commander_thread.rs @@ -1,6 +1,9 @@ // SPDX-License-Identifier: Apache-2.0 -use nipart::{NipartError, NipartEvent, NipartPluginEvent, NipartUserEvent}; +use nipart::{ + NipartError, NipartEvent, NipartEventAddress, NipartLogEntry, + NipartLogLevel, NipartPluginEvent, NipartUserEvent, +}; use tokio::sync::mpsc::{Receiver, Sender}; use super::{WorkFlow, WorkFlowQueue}; @@ -47,8 +50,14 @@ async fn commander_thread( &mut workflow_queue, &mut commander_to_switch).await } Some(event) = switch_to_commander.recv() => { - log::debug!("switch_to_commander {event}"); - log::trace!("switch_to_commander {event:?}"); + log_to_user(event.uuid, + NipartLogLevel::Debug, + format!("Received event {event}"), + &commander_to_switch).await; + log_to_user(event.uuid, + NipartLogLevel::Trace, + format!("Received event {event:?}"), + &commander_to_switch).await; process_event( event, &mut workflow_queue, @@ -66,8 +75,20 @@ async fn process_workflow_queue( commander_to_switch: &mut Sender, ) -> Result<(), NipartError> { for event in workflow_queue.process()? { - log::debug!("Sent to switch {event}"); - log::trace!("Sent to switch {event:?}"); + log_to_user( + event.uuid, + NipartLogLevel::Debug, + format!("Send event {event}"), + commander_to_switch, + ) + .await; + log_to_user( + event.uuid, + NipartLogLevel::Trace, + format!("Sent event {event:?}"), + commander_to_switch, + ) + .await; if let Err(e) = commander_to_switch.send(event).await { log::error!("{e}"); } @@ -113,7 +134,14 @@ async fn process_plugin_event( } else { match event.plugin { NipartPluginEvent::GotDhcpLease(lease) => { - log::debug!("Got DHCP {lease:?}"); + log_to_user( + event.uuid, + NipartLogLevel::Debug, + format!("Got DHCP {lease:?}"), + commander_to_switch, + ) + .await; + let (workflow, share_data) = WorkFlow::new_apply_dhcp_lease( event.uuid, *lease, @@ -185,3 +213,16 @@ async fn process_user_event( workflow_queue.add_workflow(workflow, share_data); process_workflow_queue(workflow_queue, commander_to_switch).await } + +async fn log_to_user( + uuid: u128, + level: NipartLogLevel, + message: String, + sender: &Sender, +) { + let event = NipartLogEntry::new(level, message) + .to_event(uuid, NipartEventAddress::Commander); + if let Err(e) = sender.send(event).await { + log::warn!("Failed to send log {e}"); + } +} diff --git a/src/lib/event.rs b/src/lib/event.rs index 68d8a4c..5e8cbda 100644 --- a/src/lib/event.rs +++ b/src/lib/event.rs @@ -70,8 +70,8 @@ impl std::fmt::Display for NipartEvent { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( f, - "event.uuid:{} event.user:{} event.plugin:{} \ - event.src:{} event.dst:{} event.timeout:{}ms{}", + "uuid:{} user:{} plugin:{} \ + src:{} dst:{} timeout:{}ms{}", self.uuid, self.user, self.plugin, @@ -196,24 +196,21 @@ impl std::fmt::Display for NipartUserEvent { f, "{}", match self { - Self::None => "user_event.none", - Self::Quit => "user_event.quit", - Self::Error(_) => "user_event.error", - Self::QueryPluginInfo => "user_event.query_plugin_info", - Self::QueryPluginInfoReply(_) => - "user_event.query_plugin_info_reply", - Self::ChangeLogLevel(_) => "user_event.change_log_level", - Self::QueryLogLevel => "user_event.query_log_level", - Self::QueryLogLevelReply(_) => - "user_event.query_log_level_reply", - Self::QueryNetState(_) => "user_event.query_netstate", - Self::QueryNetStateReply(_) => - "user_event.query_netstate_reply", - Self::ApplyNetState(_, _) => "user_event.apply_netstate", - Self::ApplyNetStateReply => "user_event.apply_netstate_reply", - Self::QueryCommits(_) => "user_event.query_commits", - Self::QueryCommitsReply(_) => "user_event.query_commits_reply", - Self::Log(_) => "user_event.log", + Self::None => "none", + Self::Quit => "quit", + Self::Error(_) => "error", + Self::QueryPluginInfo => "query_plugin_info", + Self::QueryPluginInfoReply(_) => "query_plugin_info_reply", + Self::ChangeLogLevel(_) => "change_log_level", + Self::QueryLogLevel => "query_log_level", + Self::QueryLogLevelReply(_) => "query_log_level_reply", + Self::QueryNetState(_) => "query_netstate", + Self::QueryNetStateReply(_) => "query_netstate_reply", + Self::ApplyNetState(_, _) => "apply_netstate", + Self::ApplyNetStateReply => "apply_netstate_reply", + Self::QueryCommits(_) => "query_commits", + Self::QueryCommitsReply(_) => "query_commits_reply", + Self::Log(_) => "log", } ) } diff --git a/src/lib/logging.rs b/src/lib/logging.rs index 322adaf..4ee8a68 100644 --- a/src/lib/logging.rs +++ b/src/lib/logging.rs @@ -2,7 +2,10 @@ use serde::{Deserialize, Serialize}; -use crate::{ErrorKind, NipartError}; +use crate::{ + ErrorKind, NipartError, NipartEvent, NipartEventAddress, NipartPluginEvent, + NipartUserEvent, +}; #[derive( Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize, @@ -139,4 +142,15 @@ impl NipartLogEntry { } } } + + pub fn to_event(self, uuid: u128, src: NipartEventAddress) -> NipartEvent { + NipartEvent::new_with_uuid( + uuid, + NipartUserEvent::Log(self), + NipartPluginEvent::None, + src, + NipartEventAddress::User, + crate::DEFAULT_TIMEOUT, + ) + } } diff --git a/src/lib/plugin_native.rs b/src/lib/plugin_native.rs index 965fee9..a8f158d 100644 --- a/src/lib/plugin_native.rs +++ b/src/lib/plugin_native.rs @@ -35,23 +35,12 @@ pub trait NipartNativePlugin: Sized + Send + Sync + 'static { return; } - if uuid > 0 { - let event = NipartEvent::new_with_uuid( - uuid, - NipartUserEvent::Log(NipartLogEntry::new(level, msg)), - NipartPluginEvent::None, - NipartEventAddress::Unicast(Self::PLUGIN_NAME.to_string()), - NipartEventAddress::User, - crate::DEFAULT_TIMEOUT, - ); - event.emit_log(); - if let Err(e) = self.sender_to_daemon().send(event).await { - log::error!( - "{level} plugin::{} {uuid}: \ - Failed to send log to daemon, {e}", - Self::PLUGIN_NAME, - ); - } + let event = NipartLogEntry::new(level, msg).to_event( + uuid, + NipartEventAddress::Unicast(Self::PLUGIN_NAME.to_string()), + ); + if let Err(e) = self.sender_to_daemon().send(event).await { + log::warn!("Failed to send log: {e}"); } } }