1use diagnostics_log::{PublishError, Publisher, PublisherOptions};
6use fidl_fuchsia_logger as flogger;
7use thiserror::Error;
8
9#[derive(Debug, Error)]
10pub enum ScopedLoggerError {
11 #[error("could not create publisher")]
12 PublishError(#[source] PublishError),
13}
14
15pub struct ScopedLogger {
16 publisher: Publisher,
17}
18
19impl ScopedLogger {
20 pub fn create(logsink: flogger::LogSinkProxy) -> Result<Self, ScopedLoggerError> {
21 let publisher = Publisher::new(
22 PublisherOptions::default().wait_for_initial_interest(false).use_log_sink(logsink),
23 )
24 .map_err(ScopedLoggerError::PublishError)?;
25 Ok(Self { publisher })
26 }
27}
28
29impl log::Log for ScopedLogger {
30 #[inline]
31 fn enabled(&self, metadata: &log::Metadata<'_>) -> bool {
32 self.publisher.enabled(metadata)
33 }
34
35 #[inline]
36 fn log(&self, record: &log::Record<'_>) {
37 self.publisher.log(record);
38 }
39
40 #[inline]
41 fn flush(&self) {
42 self.publisher.flush();
43 }
44}