logs to client_api

This commit is contained in:
John Smith 2021-12-10 22:04:38 -05:00
parent 7e8eb0c57d
commit 43fd315932
6 changed files with 48 additions and 37 deletions

View File

@ -43,6 +43,16 @@ impl veilid_client::Server for VeilidClientImpl {
Promise::ok(()) 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 { struct ClientApiConnectionInner {

View File

@ -277,6 +277,10 @@ debug - send a debugging command to the Veilid server
self.inner_mut().ui.set_attachment_state(state); 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 // called by client_api_connection
// calls into ui // calls into ui
//////////////////////////////////////////// ////////////////////////////////////////////

View File

@ -166,7 +166,7 @@ impl veilid_server::Server for VeilidServerImpl {
params: veilid_server::DebugParams, params: veilid_server::DebugParams,
mut results: veilid_server::DebugResults, mut results: veilid_server::DebugResults,
) -> Promise<(), ::capnp::Error> { ) -> Promise<(), ::capnp::Error> {
trace!("VeilidServerImpl::attach"); trace!("VeilidServerImpl::debug");
let veilid_api = self.veilid_api.clone(); let veilid_api = self.veilid_api.clone();
let what = pry!(pry!(params.get()).get_what()).to_owned(); let what = pry!(pry!(params.get()).get_what()).to_owned();
@ -298,7 +298,7 @@ impl ClientApi {
let regs = &mut registration_map.borrow_mut().registrations; let regs = &mut registration_map.borrow_mut().registrations;
for (&id, mut registration) in regs.iter_mut() { for (&id, mut registration) in regs.iter_mut() {
if registration.requests_in_flight > 5 { if registration.requests_in_flight > 5 {
debug!( println!(
"too many requests in flight: {}", "too many requests in flight: {}",
registration.requests_in_flight registration.requests_in_flight
); );
@ -317,7 +317,7 @@ impl ClientApi {
} }
} }
Err(e) => { Err(e) => {
debug!("Got error: {:?}. Dropping registation.", e); println!("Got error: {:?}. Dropping registation.", e);
registration_map2.borrow_mut().registrations.remove(&id); registration_map2.borrow_mut().registrations.remove(&id);
} }
})); }));

View File

@ -14,7 +14,7 @@ pub struct ClientLogChannel {
impl ClientLogChannel { impl ClientLogChannel {
pub fn new() -> Self { pub fn new() -> Self {
let (sender, receiver) = bounded(1); let (sender, receiver) = bounded(1024);
Self { Self {
inner: Arc::new(ClientLogChannelInner { sender, receiver }), inner: Arc::new(ClientLogChannelInner { sender, receiver }),
} }
@ -27,11 +27,8 @@ impl ClientLogChannel {
impl std::io::Write for ClientLogChannel { impl std::io::Write for ClientLogChannel {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
if let Err(e) = self let bufstr = String::from_utf8_lossy(buf).to_string();
.inner if let Err(e) = self.inner.sender.try_send(bufstr) {
.sender
.try_send(String::from_utf8_lossy(buf).to_string())
{
match e { match e {
TrySendError::Full(_) => Err(std::io::Error::from(std::io::ErrorKind::WouldBlock)), TrySendError::Full(_) => Err(std::io::Error::from(std::io::ErrorKind::WouldBlock)),
TrySendError::Closed(_) => { TrySendError::Closed(_) => {

View File

@ -30,7 +30,7 @@ logging:
append: true append: true
level: "info" level: "info"
client: client:
enable: false enabled: true
level: "info" level: "info"
testing: testing:
subnode_index: 0 subnode_index: 0

View File

@ -4,16 +4,13 @@ use crate::client_log_channel::*;
use crate::settings; use crate::settings;
use async_std::channel::{bounded, Receiver, Sender}; use async_std::channel::{bounded, Receiver, Sender};
use clap::{App, Arg}; use clap::{App, Arg};
use futures::*;
use lazy_static::*; use lazy_static::*;
use log::*; use log::*;
use parking_lot::Mutex; use parking_lot::Mutex;
use simplelog::*; use simplelog::*;
use std::cell::RefCell;
use std::ffi::OsStr; use std::ffi::OsStr;
use std::fs::OpenOptions; use std::fs::OpenOptions;
use std::path::Path; use std::path::Path;
use std::rc::Rc;
use std::str::FromStr; use std::str::FromStr;
use std::sync::Arc; use std::sync::Arc;
use veilid_core::xx::SingleShotEventual; use veilid_core::xx::SingleShotEventual;
@ -236,7 +233,7 @@ pub async fn main() -> Result<(), String> {
logs.push(WriteLogger::new( logs.push(WriteLogger::new(
settings::convert_loglevel(settingsr.logging.file.level), settings::convert_loglevel(settingsr.logging.file.level),
cb.build(), cb.build(),
clog, std::io::LineWriter::with_capacity(65536, clog),
)) ))
} }
CombinedLogger::init(logs).map_err(|e| format!("failed to init logs: {}", e))?; 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))?; .map_err(|e| format!("VeilidCore startup failed: {}", e))?;
// Start client api if one is requested // 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()); let some_capi = client_api::ClientApi::new(veilid_api.clone());
some_capi some_capi
.clone() .clone()
@ -280,36 +277,37 @@ pub async fn main() -> Result<(), String> {
Some(some_capi) Some(some_capi)
} else { } else {
None None
})); };
// Drop rwlock on settings // Drop rwlock on settings
let auto_attach = settingsr.auto_attach; let auto_attach = settingsr.auto_attach;
drop(settingsr); drop(settingsr);
// Handle state changes on main thread for capnproto rpc // Handle state changes on main thread for capnproto rpc
let capi2 = capi.clone(); let capi_jh = capi.clone().map(|capi| {
let capi_jh = async_std::task::spawn_local(async move { async_std::task::spawn_local(async move {
trace!("state change processing started"); trace!("state change processing started");
while let Ok(change) = receiver.recv().await { while let Ok(change) = receiver.recv().await {
if let Some(c) = capi2.borrow().as_ref().cloned() { capi.clone().handle_state_change(change);
c.handle_state_change(change);
}
} }
trace!("state change processing stopped"); trace!("state change processing stopped");
})
}); });
// Handle log messages on main thread for capnproto rpc // Handle log messages on main thread for capnproto rpc
let capi2 = capi.clone(); let capi_jh2 = capi
let capi_jh2 = client_log_channel.map(|client_log_channel| { .clone()
.map(|capi| {
client_log_channel.map(|client_log_channel| {
async_std::task::spawn_local(async move { async_std::task::spawn_local(async move {
trace!("client logging started"); trace!("client logging started");
while let Ok(message) = client_log_channel.recv().await { while let Ok(message) = client_log_channel.recv().await {
if let Some(c) = capi2.borrow().as_ref().cloned() { capi.clone().handle_client_log(message);
c.handle_client_log(message);
}
} }
trace!("client logging stopped") trace!("client logging stopped")
}) })
}); })
})
.flatten();
// Auto-attach if desired // Auto-attach if desired
if auto_attach { if auto_attach {
@ -330,7 +328,7 @@ pub async fn main() -> Result<(), String> {
} }
// Stop the client api if we have one // 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; c.stop().await;
} }
@ -338,7 +336,9 @@ pub async fn main() -> Result<(), String> {
veilid_api.shutdown().await; veilid_api.shutdown().await;
// Wait for client api handlers to exit // Wait for client api handlers to exit
if let Some(capi_jh) = capi_jh {
capi_jh.await; capi_jh.await;
}
if let Some(capi_jh2) = capi_jh2 { if let Some(capi_jh2) = capi_jh2 {
capi_jh2.await; capi_jh2.await;
} }