Skip to content

Commit

Permalink
desktop: Allow routing logging to Rerun. Needs a filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
kpreid committed Jan 7, 2024
1 parent 1ed3a95 commit 9e5e9db
Showing 1 changed file with 62 additions and 10 deletions.
72 changes: 62 additions & 10 deletions all-is-cubes-desktop/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ pub fn install(
let &LoggingArgs {
verbose,
simplify_log_format,
ref rerun,
rerun: ref kinds,
} = options;
let kinds: HashSet<RerunDataKind> = HashSet::from_iter(kinds.iter().cloned());

let (max_level, stderr_logger): (log::LevelFilter, Option<simplelog::TermLogger>) =
if options.verbose || !suppress_unless_explicit {
Expand Down Expand Up @@ -76,7 +77,7 @@ pub fn install(
// If we're going to construct a Rerun recording stream, because the user passed at least
// one `--rerun=` arg, do so now.
#[cfg(feature = "rerun")]
let rerun_destination = if !rerun.is_empty() {
let (rerun_destination_general, rerun_destination_logging) = if !kinds.is_empty() {
let stream = re_sdk::RecordingStreamBuilder::new("all-is-cubes")
.default_enabled(true)
.connect()
Expand All @@ -91,44 +92,94 @@ pub fn install(
// Log timeless configuration
destination.log_initialization();

destination
// Hook up [`log`] logging if requested.
let dl = if kinds.contains(&RerunDataKind::Log) {
destination.child(&rg::entity_path!["log"])
} else {
rg::Destination::default()
};

(destination, dl)
} else {
rg::Destination::default()
(rg::Destination::default(), rg::Destination::default())
};

// Install the logger with our wrapper around it.
log::set_boxed_logger(Box::new(AicLogger { stderr_logger }))
.context("failed to initialize logging")?;
let our_combined_logger = AicLogger {
stderr_logger,
#[cfg(feature = "rerun")]
rerun_destination: rerun_destination_logging,
};

// Install the logger.
log::set_boxed_logger(Box::new(our_combined_logger)).context("failed to initialize logging")?;
log::set_max_level(max_level);

Ok(LateLogging {
kinds: HashSet::from_iter(rerun.iter().cloned()),
kinds,
#[cfg(feature = "rerun")]
rerun_destination,
rerun_destination: rerun_destination_general,
})
}

/// [`log::Log`] implementation that [`install()`] registers globally.
struct AicLogger {
stderr_logger: Option<simplelog::TermLogger>,
#[cfg(feature = "rerun")]
rerun_destination: rg::Destination,
}

impl log::Log for AicLogger {
fn enabled(&self, metadata: &log::Metadata<'_>) -> bool {
#[cfg(feature = "rerun")]
let rr = self.rerun_destination.is_enabled();
#[cfg(not(feature = "rerun"))]
let rr = false;

self.stderr_logger
.as_ref()
.is_some_and(|l| l.enabled(metadata))
|| rr
}

fn log(&self, record: &log::Record<'_>) {
if let Some(stderr_logger) = &self.stderr_logger {
suspend_indicatif_in(|| stderr_logger.log(record))
suspend_indicatif_in(|| stderr_logger.log(record));
}
#[cfg(feature = "rerun")]
if self.rerun_destination.is_enabled() {
use rg::components::TextLogLevel;

// TODO: Need to implement our own log filtering here since simplelog doesn't let us
// borrow the filter rules.

self.rerun_destination.log(
&rg::entity_path![],
&rg::archetypes::TextLog {
text: record.args().to_string().into(),
level: Some(TextLogLevel(
match record.level() {
log::Level::Error => TextLogLevel::ERROR,
log::Level::Warn => TextLogLevel::WARN,
log::Level::Info => TextLogLevel::INFO,
log::Level::Debug => TextLogLevel::DEBUG,
log::Level::Trace => TextLogLevel::TRACE,
}
.into(),
)),
color: None,
},
);
}
}

fn flush(&self) {
if let Some(stderr_logger) = &self.stderr_logger {
suspend_indicatif_in(|| stderr_logger.flush())
}
#[cfg(feature = "rerun")]
if self.rerun_destination.is_enabled() {
self.rerun_destination.stream.flush_async();
}
}
}

Expand Down Expand Up @@ -160,6 +211,7 @@ pub fn common_progress_style() -> indicatif::ProgressStyle {
/// Types of data that command line options can request be written to Rerun.
#[derive(Debug, Clone, Eq, Hash, PartialEq, clap::ValueEnum)]
pub(crate) enum RerunDataKind {
Log,
World,
RenderPerf,
RenderImage,
Expand Down

0 comments on commit 9e5e9db

Please sign in to comment.