fuchsia_syslog_listener/
lib.rs#![deny(missing_docs)]
use anyhow::{Context as _, Error};
use fidl_fuchsia_diagnostics::LogInterestSelector;
use fidl_fuchsia_logger::{
LogFilterOptions, LogListenerSafeRequest, LogListenerSafeRequestStream, LogMarker, LogMessage,
LogProxy,
};
use fuchsia_component::client::connect_to_protocol;
use futures::channel::mpsc;
use futures::TryStreamExt;
pub trait LogProcessor {
fn log(&mut self, message: LogMessage);
fn done(&mut self);
}
async fn log_listener(
mut processor: impl LogProcessor,
mut stream: LogListenerSafeRequestStream,
) -> Result<(), fidl::Error> {
while let Some(request) = stream.try_next().await? {
match request {
LogListenerSafeRequest::Log { log, responder } => {
processor.log(log);
responder.send().ok();
}
LogListenerSafeRequest::LogMany { log, responder } => {
for msg in log {
processor.log(msg);
}
responder.send().ok();
}
LogListenerSafeRequest::Done { control_handle: _ } => {
processor.done();
return Ok(());
}
}
}
Ok(())
}
pub async fn run_log_listener_with_proxy<'a>(
logger: &LogProxy,
processor: impl LogProcessor + 'a,
options: Option<&'a LogFilterOptions>,
dump_logs: bool,
selectors: Option<&'a [LogInterestSelector]>,
) -> Result<(), Error> {
let (listener_ptr, listener_stream) = fidl::endpoints::create_request_stream();
if dump_logs {
logger.dump_logs_safe(listener_ptr, options).context("failed to register log dumper")?;
} else {
match selectors {
Some(s) => logger
.listen_safe_with_selectors(listener_ptr, options, s)
.context("failed to register listener with selectors")?,
None => {
logger.listen_safe(listener_ptr, options).context("failed to register listener")?
}
};
}
log_listener(processor, listener_stream).await?;
Ok(())
}
pub async fn run_log_listener<'a>(
processor: impl LogProcessor + 'a,
options: Option<&'a LogFilterOptions>,
dump_logs: bool,
selectors: Option<&'a [LogInterestSelector]>,
) -> Result<(), Error> {
let logger = connect_to_protocol::<LogMarker>()?;
run_log_listener_with_proxy(&logger, processor, options, dump_logs, selectors).await
}
impl LogProcessor for mpsc::UnboundedSender<LogMessage> {
fn log(&mut self, message: LogMessage) {
self.unbounded_send(message).ok();
}
fn done(&mut self) {
self.close_channel();
}
}