Skip to content

Commit

Permalink
logging: Send commander logs to user
Browse files Browse the repository at this point in the history
Also send commander log to user session.

Signed-off-by: Gris Ge <[email protected]>
  • Loading branch information
cathay4t committed Aug 28, 2024
1 parent f8f307f commit 97ec700
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 44 deletions.
53 changes: 47 additions & 6 deletions src/daemon/commander/commander_thread.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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,
Expand All @@ -66,8 +75,20 @@ async fn process_workflow_queue(
commander_to_switch: &mut Sender<NipartEvent>,
) -> 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}");
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<NipartEvent>,
) {
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}");
}
}
37 changes: 17 additions & 20 deletions src/lib/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
}
)
}
Expand Down
16 changes: 15 additions & 1 deletion src/lib/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
)
}
}
23 changes: 6 additions & 17 deletions src/lib/plugin_native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}
}
}
Expand Down

0 comments on commit 97ec700

Please sign in to comment.