@@ -14,8 +14,25 @@ use tracing_subscriber::{
14
14
EnvFilter , Layer ,
15
15
} ;
16
16
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 ) {
18
30
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
+
19
36
log_panics:: init ( ) ;
20
37
}
21
38
@@ -244,24 +261,36 @@ pub struct TracingConfiguration {
244
261
245
262
#[ matrix_sdk_ffi_macros:: export]
246
263
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 ) ;
262
265
263
266
tracing_subscriber:: registry ( )
264
267
. with ( EnvFilter :: new ( & config. filter ) )
265
268
. with ( text_layers ( config) )
266
269
. init ( ) ;
267
270
}
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