From 43fd31593202e89189a972de1f360afe8fb24f43 Mon Sep 17 00:00:00 2001 From: John Smith Date: Fri, 10 Dec 2021 22:04:38 -0500 Subject: [PATCH] logs to client_api --- veilid-cli/src/client_api_connection.rs | 10 +++++ veilid-cli/src/command_processor.rs | 4 ++ veilid-server/src/client_api.rs | 6 +-- veilid-server/src/client_log_channel.rs | 9 ++--- veilid-server/src/settings.rs | 2 +- veilid-server/src/unix.rs | 54 ++++++++++++------------- 6 files changed, 48 insertions(+), 37 deletions(-) diff --git a/veilid-cli/src/client_api_connection.rs b/veilid-cli/src/client_api_connection.rs index 363150a4..cfcd8f43 100644 --- a/veilid-cli/src/client_api_connection.rs +++ b/veilid-cli/src/client_api_connection.rs @@ -43,6 +43,16 @@ impl veilid_client::Server for VeilidClientImpl { Promise::ok(()) } + + fn log_message( + &mut self, + params: veilid_client::LogMessageParams, + _results: veilid_client::LogMessageResults, + ) -> Promise<(), ::capnp::Error> { + let message = pry!(pry!(params.get()).get_message()); + self.comproc.add_log_message(message); + Promise::ok(()) + } } struct ClientApiConnectionInner { diff --git a/veilid-cli/src/command_processor.rs b/veilid-cli/src/command_processor.rs index 8ade71f2..cbdb25a0 100644 --- a/veilid-cli/src/command_processor.rs +++ b/veilid-cli/src/command_processor.rs @@ -277,6 +277,10 @@ debug - send a debugging command to the Veilid server self.inner_mut().ui.set_attachment_state(state); } + pub fn add_log_message(&mut self, message: &str) { + self.inner().ui.add_node_event(message); + } + // called by client_api_connection // calls into ui //////////////////////////////////////////// diff --git a/veilid-server/src/client_api.rs b/veilid-server/src/client_api.rs index 3d6b695a..eba9c589 100644 --- a/veilid-server/src/client_api.rs +++ b/veilid-server/src/client_api.rs @@ -166,7 +166,7 @@ impl veilid_server::Server for VeilidServerImpl { params: veilid_server::DebugParams, mut results: veilid_server::DebugResults, ) -> Promise<(), ::capnp::Error> { - trace!("VeilidServerImpl::attach"); + trace!("VeilidServerImpl::debug"); let veilid_api = self.veilid_api.clone(); let what = pry!(pry!(params.get()).get_what()).to_owned(); @@ -298,7 +298,7 @@ impl ClientApi { let regs = &mut registration_map.borrow_mut().registrations; for (&id, mut registration) in regs.iter_mut() { if registration.requests_in_flight > 5 { - debug!( + println!( "too many requests in flight: {}", registration.requests_in_flight ); @@ -317,7 +317,7 @@ impl ClientApi { } } Err(e) => { - debug!("Got error: {:?}. Dropping registation.", e); + println!("Got error: {:?}. Dropping registation.", e); registration_map2.borrow_mut().registrations.remove(&id); } })); diff --git a/veilid-server/src/client_log_channel.rs b/veilid-server/src/client_log_channel.rs index 5e370e10..8fc84da9 100644 --- a/veilid-server/src/client_log_channel.rs +++ b/veilid-server/src/client_log_channel.rs @@ -14,7 +14,7 @@ pub struct ClientLogChannel { impl ClientLogChannel { pub fn new() -> Self { - let (sender, receiver) = bounded(1); + let (sender, receiver) = bounded(1024); Self { inner: Arc::new(ClientLogChannelInner { sender, receiver }), } @@ -27,11 +27,8 @@ impl ClientLogChannel { impl std::io::Write for ClientLogChannel { fn write(&mut self, buf: &[u8]) -> std::io::Result { - if let Err(e) = self - .inner - .sender - .try_send(String::from_utf8_lossy(buf).to_string()) - { + let bufstr = String::from_utf8_lossy(buf).to_string(); + if let Err(e) = self.inner.sender.try_send(bufstr) { match e { TrySendError::Full(_) => Err(std::io::Error::from(std::io::ErrorKind::WouldBlock)), TrySendError::Closed(_) => { diff --git a/veilid-server/src/settings.rs b/veilid-server/src/settings.rs index 3d4bbc22..bebdcb2a 100644 --- a/veilid-server/src/settings.rs +++ b/veilid-server/src/settings.rs @@ -30,7 +30,7 @@ logging: append: true level: "info" client: - enable: false + enabled: true level: "info" testing: subnode_index: 0 diff --git a/veilid-server/src/unix.rs b/veilid-server/src/unix.rs index c23a93dd..8c2761b4 100644 --- a/veilid-server/src/unix.rs +++ b/veilid-server/src/unix.rs @@ -4,16 +4,13 @@ use crate::client_log_channel::*; use crate::settings; use async_std::channel::{bounded, Receiver, Sender}; use clap::{App, Arg}; -use futures::*; use lazy_static::*; use log::*; use parking_lot::Mutex; use simplelog::*; -use std::cell::RefCell; use std::ffi::OsStr; use std::fs::OpenOptions; use std::path::Path; -use std::rc::Rc; use std::str::FromStr; use std::sync::Arc; use veilid_core::xx::SingleShotEventual; @@ -236,7 +233,7 @@ pub async fn main() -> Result<(), String> { logs.push(WriteLogger::new( settings::convert_loglevel(settingsr.logging.file.level), cb.build(), - clog, + std::io::LineWriter::with_capacity(65536, clog), )) } CombinedLogger::init(logs).map_err(|e| format!("failed to init logs: {}", e))?; @@ -272,7 +269,7 @@ pub async fn main() -> Result<(), String> { .map_err(|e| format!("VeilidCore startup failed: {}", e))?; // Start client api if one is requested - let capi = Rc::new(RefCell::new(if settingsr.client_api.enabled { + let mut capi = if settingsr.client_api.enabled { let some_capi = client_api::ClientApi::new(veilid_api.clone()); some_capi .clone() @@ -280,36 +277,37 @@ pub async fn main() -> Result<(), String> { Some(some_capi) } else { None - })); + }; // Drop rwlock on settings let auto_attach = settingsr.auto_attach; drop(settingsr); // Handle state changes on main thread for capnproto rpc - let capi2 = capi.clone(); - let capi_jh = async_std::task::spawn_local(async move { - trace!("state change processing started"); - while let Ok(change) = receiver.recv().await { - if let Some(c) = capi2.borrow().as_ref().cloned() { - c.handle_state_change(change); - } - } - trace!("state change processing stopped"); - }); - // Handle log messages on main thread for capnproto rpc - let capi2 = capi.clone(); - let capi_jh2 = client_log_channel.map(|client_log_channel| { + let capi_jh = capi.clone().map(|capi| { async_std::task::spawn_local(async move { - trace!("client logging started"); - while let Ok(message) = client_log_channel.recv().await { - if let Some(c) = capi2.borrow().as_ref().cloned() { - c.handle_client_log(message); - } + trace!("state change processing started"); + while let Ok(change) = receiver.recv().await { + capi.clone().handle_state_change(change); } - trace!("client logging stopped") + trace!("state change processing stopped"); }) }); + // Handle log messages on main thread for capnproto rpc + let capi_jh2 = capi + .clone() + .map(|capi| { + client_log_channel.map(|client_log_channel| { + async_std::task::spawn_local(async move { + trace!("client logging started"); + while let Ok(message) = client_log_channel.recv().await { + capi.clone().handle_client_log(message); + } + trace!("client logging stopped") + }) + }) + }) + .flatten(); // Auto-attach if desired if auto_attach { @@ -330,7 +328,7 @@ pub async fn main() -> Result<(), String> { } // Stop the client api if we have one - if let Some(c) = capi.borrow_mut().as_mut().cloned() { + if let Some(c) = capi.as_mut().cloned() { c.stop().await; } @@ -338,7 +336,9 @@ pub async fn main() -> Result<(), String> { veilid_api.shutdown().await; // Wait for client api handlers to exit - capi_jh.await; + if let Some(capi_jh) = capi_jh { + capi_jh.await; + } if let Some(capi_jh2) = capi_jh2 { capi_jh2.await; }