Skip to content

Commit

Permalink
Add rerun --strict: crash if any warning or error is logged (#1812)
Browse files Browse the repository at this point in the history
* Add `rerun --strict`: crash if any warning or error is logged

Part of #1483

* Can't doc-test private functions
  • Loading branch information
emilk authored Apr 11, 2023
1 parent d852534 commit 7d4514e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 6 deletions.
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 @@ -215,21 +215,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:
///
/// ```ignore
/// 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
.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 {}

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"])
);
std::process::exit(1);
}

fn flush(&self) {}
}

1 comment on commit 7d4514e

@github-actions
Copy link

Choose a reason for hiding this comment

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

Rust Benchmark

Benchmark suite Current: 7d4514e Previous: d852534 Ratio
datastore/num_rows=1000/num_instances=1000/packed=false/insert/default 10655704 ns/iter (± 500613) 9986008 ns/iter (± 786558) 1.07
datastore/num_rows=1000/num_instances=1000/packed=false/latest_at/default 1839 ns/iter (± 21) 1810 ns/iter (± 26) 1.02
datastore/num_rows=1000/num_instances=1000/packed=false/latest_at_missing/primary/default 280 ns/iter (± 0) 277 ns/iter (± 4) 1.01
datastore/num_rows=1000/num_instances=1000/packed=false/latest_at_missing/secondaries/default 434 ns/iter (± 0) 428 ns/iter (± 5) 1.01
datastore/num_rows=1000/num_instances=1000/packed=false/range/default 10819118 ns/iter (± 1050608) 10083824 ns/iter (± 431121) 1.07
mono_points_arrow/generate_message_bundles 46382388 ns/iter (± 1940474) 44662814 ns/iter (± 1366035) 1.04
mono_points_arrow/generate_messages 169558340 ns/iter (± 1538629) 165151412 ns/iter (± 1623002) 1.03
mono_points_arrow/encode_log_msg 193458262 ns/iter (± 1130953) 190444310 ns/iter (± 1824307) 1.02
mono_points_arrow/encode_total 409625227 ns/iter (± 2931999) 406511948 ns/iter (± 2826957) 1.01
mono_points_arrow/decode_log_msg 248309845 ns/iter (± 1431760) 249669700 ns/iter (± 1955503) 0.99
mono_points_arrow/decode_message_bundles 83077512 ns/iter (± 937432) 79645762 ns/iter (± 1588996) 1.04
mono_points_arrow/decode_total 337306060 ns/iter (± 2775485) 331555134 ns/iter (± 2810134) 1.02
mono_points_arrow_batched/generate_message_bundles 40333025 ns/iter (± 2101631) 35949883 ns/iter (± 1764135) 1.12
mono_points_arrow_batched/generate_messages 8420011 ns/iter (± 833650) 6823526 ns/iter (± 415150) 1.23
mono_points_arrow_batched/encode_log_msg 1459604 ns/iter (± 7542) 1437597 ns/iter (± 16752) 1.02
mono_points_arrow_batched/encode_total 52606002 ns/iter (± 1953837) 48628004 ns/iter (± 1902045) 1.08
mono_points_arrow_batched/decode_log_msg 858989 ns/iter (± 2897) 847900 ns/iter (± 7261) 1.01
mono_points_arrow_batched/decode_message_bundles 12068697 ns/iter (± 689960) 11275359 ns/iter (± 517095) 1.07
mono_points_arrow_batched/decode_total 13643801 ns/iter (± 612416) 12409735 ns/iter (± 539880) 1.10
batch_points_arrow/generate_message_bundles 333570 ns/iter (± 838) 323589 ns/iter (± 4333) 1.03
batch_points_arrow/generate_messages 6352 ns/iter (± 15) 6148 ns/iter (± 93) 1.03
batch_points_arrow/encode_log_msg 384658 ns/iter (± 3508) 379603 ns/iter (± 3945) 1.01
batch_points_arrow/encode_total 736022 ns/iter (± 3775) 721784 ns/iter (± 7601) 1.02
batch_points_arrow/decode_log_msg 337204 ns/iter (± 1221) 332936 ns/iter (± 2915) 1.01
batch_points_arrow/decode_message_bundles 2295 ns/iter (± 17) 2239 ns/iter (± 34) 1.03
batch_points_arrow/decode_total 350135 ns/iter (± 1114) 341734 ns/iter (± 3012) 1.02
arrow_mono_points/insert 7350272140 ns/iter (± 25118734) 7311032580 ns/iter (± 28599593) 1.01
arrow_mono_points/query 1780072 ns/iter (± 7323) 1751863 ns/iter (± 29209) 1.02
arrow_batch_points/insert 3214273 ns/iter (± 8280) 3212950 ns/iter (± 20038) 1.00
arrow_batch_points/query 16367 ns/iter (± 58) 16379 ns/iter (± 50) 1.00
arrow_batch_vecs/insert 44355 ns/iter (± 99) 44838 ns/iter (± 269) 0.99
arrow_batch_vecs/query 388792 ns/iter (± 478) 389195 ns/iter (± 1221) 1.00
tuid/Tuid::random 34 ns/iter (± 0) 34 ns/iter (± 0) 1

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.