Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rerun --strict: crash if any warning or error is logged #1812

Merged
merged 2 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/re_log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ pub use {
setup::*,
};

/// Re-exports of other crates.
pub mod external {
pub use log;
}

/// Never log anything less serious than a `WARN` from these crates.
const CRATES_AT_WARN_LEVEL: [&str; 3] = [
// wgpu crates spam a lot on info level, which is really annoying
Expand Down
29 changes: 23 additions & 6 deletions crates/rerun/src/crash_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn install_panic_hook(_build_info: BuildInfo) {
let previous_panic_hook = std::panic::take_hook();

std::panic::set_hook(Box::new(move |panic_info: &std::panic::PanicInfo<'_>| {
let callstack = callstack_from("panicking::panic_fmt\n");
let callstack = callstack_from(&["panicking::panic_fmt\n"]);

let file_line = panic_info.location().map(|location| {
let file = anonymize_source_file_path(&std::path::PathBuf::from(location.file()));
Expand Down Expand Up @@ -210,21 +210,38 @@ fn install_signal_handler(build_info: BuildInfo) {
}

fn callstack() -> String {
callstack_from("install_signal_handler::signal_handler\n")
callstack_from(&["install_signal_handler::signal_handler\n"])
}
}

fn callstack_from(start_pattern: &str) -> String {
/// Get a nicely formatted callstack.
///
/// You can give this function a list of substrings to look for, e.g. names of functions.
/// If any of these substrings matches, anything before that is removed from the callstack.
/// For example:
///
/// ```
/// fn print_callstack() {
/// eprintln!("{}", callstack_from(&["print_callstack"]));
/// }
/// ```
pub fn callstack_from(start_patterns: &[&str]) -> String {
let backtrace = backtrace::Backtrace::new();
let stack = backtrace_to_string(&backtrace);

// Trim it a bit:
let mut stack = stack.as_str();

let start_patterns = start_patterns
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is it called start_pattern if it can appear anywhere in the string? (was already the case)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything before it is trimmed away, so it becomes the start of the returned stacktrace

.iter()
.chain(std::iter::once(&"callstack_from"));

// Trim the top (closest to the panic handler) to cut out some noise:
if let Some(offset) = stack.find(start_pattern) {
let prev_newline = stack[..offset].rfind('\n').map_or(0, |newline| newline + 1);
stack = &stack[prev_newline..];
for start_pattern in start_patterns {
if let Some(offset) = stack.find(start_pattern) {
let prev_newline = stack[..offset].rfind('\n').map_or(0, |newline| newline + 1);
stack = &stack[prev_newline..];
}
}

// Trim the bottom to cut out code that sets up the callstack:
Expand Down
41 changes: 41 additions & 0 deletions crates/rerun/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ struct Args {
#[clap(long)]
profile: bool,

/// Exit with a non-zero exit code if any warning or error is logged. Useful for tests.
#[clap(long)]
strict: bool,

/// An upper limit on how much memory the Rerun Viewer should use.
///
/// When this limit is used, Rerun will purge the oldest data.
Expand Down Expand Up @@ -187,6 +191,11 @@ where
return Ok(0);
}

if args.strict {
re_log::add_boxed_logger(Box::new(StrictLogger {})).expect("Failed to enter --strict mode");
re_log::info!("--strict mode: any warning or error will cause Rerun to panic.");
}

let res = if let Some(commands) = &args.commands {
match commands {
#[cfg(all(feature = "analytics"))]
Expand Down Expand Up @@ -539,3 +548,35 @@ pub fn setup_ctrl_c_handler() -> (tokio::sync::broadcast::Receiver<()>, Arc<Atom
.expect("Error setting Ctrl-C handler");
(receiver, shutdown_return)
}

// ----------------------------------------------------------------------------

use re_log::external::log;

struct StrictLogger {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this file here is large enough, StrictLogger could have gone to a different module

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd keep it here since it directly reference argument names (--strict)


impl log::Log for StrictLogger {
fn enabled(&self, metadata: &log::Metadata<'_>) -> bool {
match metadata.level() {
log::Level::Error | log::Level::Warn => true,
log::Level::Info | log::Level::Debug | log::Level::Trace => false,
}
}

fn log(&self, record: &log::Record<'_>) {
let level = match record.level() {
log::Level::Error => "error",
log::Level::Warn => "warning",
log::Level::Info | log::Level::Debug | log::Level::Trace => return,
};

eprintln!("{level} logged in --strict mode: {}", record.args());
eprintln!(
"{}",
crate::crash_handler::callstack_from(&["log::__private_api_log"])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where does this string come from? Does it need to be updated if logging crate is updated?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It comes from the callstack, and yes, it will need to.

The full callstack leading up to StructLogger is:

   1: rerun::crash_handler::callstack_from
             at rerun/src/crash_handler.rs:229:21
   2: <rerun::run::StrictLogger as log::Log>::log
             at rerun/src/run.rs:574:25
   3: <&T as log::Log>::log
             at log-0.4.17/src/lib.rs:1318:9
      <re_log::multi_logger::MultiLogger as log::Log>::log
             at re_log/src/multi_logger.rs:60:13
   4: log::__private_api_log
             at log-0.4.17/src/lib.rs:1597:5

and I want to trim away as much irrelevant stuff as possible

);
std::process::exit(1);
}

fn flush(&self) {}
}