Skip to content

Commit 83fb579

Browse files
committed
fix(ffi): only add panic=error to the log filter, instead of globally enabling error
1 parent a16e9ff commit 83fb579

File tree

1 file changed

+45
-16
lines changed

1 file changed

+45
-16
lines changed

bindings/matrix-sdk-ffi/src/platform.rs

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,25 @@ use tracing_subscriber::{
1414
EnvFilter, Layer,
1515
};
1616

17-
pub fn log_panics() {
17+
/// Add panic=error to a filter line, if it's missing from it.
18+
///
19+
/// Doesn't do anything if the directive is already present.
20+
fn add_panic_to_filter(filter: &mut String) {
21+
if filter.split(',').all(|pair| pair.split('=').next().is_none_or(|lhs| lhs != "panic")) {
22+
if !filter.is_empty() {
23+
filter.push(',');
24+
}
25+
filter.push_str("panic=error");
26+
}
27+
}
28+
29+
pub fn log_panics(filter: &mut String) {
1830
std::env::set_var("RUST_BACKTRACE", "1");
31+
32+
// Make sure that panics will be properly logged. On 2025-01-08, `log_panics`
33+
// uses the `panic` target, at the error log level.
34+
add_panic_to_filter(filter);
35+
1936
log_panics::init();
2037
}
2138

@@ -244,24 +261,36 @@ pub struct TracingConfiguration {
244261

245262
#[matrix_sdk_ffi_macros::export]
246263
pub fn setup_tracing(mut config: TracingConfiguration) {
247-
log_panics();
248-
249-
// If there's no global directive, and a non-empty set of module/crate
250-
// directives, then logs from modules and crates *not* appearing in the list
251-
// of directives will be silently swallowed. This is bad, especially at the
252-
// `error` level, for which we always want to see logs (notably because the
253-
// panic logger uses the error level and a target that may not be specified in
254-
// the list of user-provided directives).
255-
//
256-
// As a result, always make sure there's a global directive. If it's not there,
257-
// we insert it at the beginning of the filter line.
258-
259-
if !config.filter.split(',').any(|pair| !pair.contains('=')) {
260-
config.filter = "error,".to_owned() + config.filter.as_str();
261-
}
264+
log_panics(&mut config.filter);
262265

263266
tracing_subscriber::registry()
264267
.with(EnvFilter::new(&config.filter))
265268
.with(text_layers(config))
266269
.init();
267270
}
271+
272+
#[cfg(test)]
273+
mod tests {
274+
use super::add_panic_to_filter;
275+
276+
#[test]
277+
fn test_add_panic_when_not_provided_empty() {
278+
let mut filter = String::from("");
279+
add_panic_to_filter(&mut filter);
280+
assert_eq!(filter, "panic=error");
281+
}
282+
283+
#[test]
284+
fn test_add_panic_when_not_provided_non_empty() {
285+
let mut filter = String::from("a=b,c=d");
286+
add_panic_to_filter(&mut filter);
287+
assert_eq!(filter, "a=b,c=d,panic=error");
288+
}
289+
290+
#[test]
291+
fn test_do_nothing_when_provided() {
292+
let mut filter = String::from("a=b,panic=error,c=d");
293+
add_panic_to_filter(&mut filter);
294+
assert_eq!(filter, "a=b,panic=error,c=d");
295+
}
296+
}

0 commit comments

Comments
 (0)