wgpu_subscriber/lib.rs
1//! Easy to use tracing subscribers tuned to usage in wgpu.
2//!
3//! [`initialize_default_subscriber`] will set everything up
4//! in a default configuration.
5//!
6//! Subscribers:
7//! - [`ChromeTracingLayer`]: Output to chrome tracing format
8//! - [`FmtLayer`]: Formatted output to stderr/stdout.
9
10pub use chrome::*;
11pub use fmt_layer::*;
12use std::path::Path;
13use tracing_subscriber::{layer::SubscriberExt as _, EnvFilter};
14
15mod chrome;
16mod fmt_layer;
17
18/// Set up the "standard" logger.
19///
20/// This is fairly inflexible, but a good default to start with. If you need more customization,
21/// take what this function does and implement it however you need.
22///
23/// If this function is called, you should **not** set up a log-based logger like env_logger
24/// or fern. This will result in duplicate messages.
25///
26/// # Args
27///
28/// - `chrome_tracing_path` if set to `Some`, will create a trace compatible with chrome://tracing
29/// at that location.
30pub fn initialize_default_subscriber(chrome_trace_path: Option<&Path>) {
31 let chrome_tracing_layer_opt =
32 chrome_trace_path.map(|path| ChromeTracingLayer::with_file(path).unwrap());
33
34 // Tracing currently doesn't support type erasure with layer composition
35 if let Some(chrome_tracing_layer) = chrome_tracing_layer_opt {
36 tracing::subscriber::set_global_default(
37 tracing_subscriber::Registry::default()
38 .with(chrome_tracing_layer)
39 .with(FmtLayer::new())
40 .with(EnvFilter::from_default_env()),
41 )
42 .unwrap();
43 } else {
44 tracing::subscriber::set_global_default(
45 tracing_subscriber::Registry::default()
46 .with(FmtLayer::new())
47 .with(EnvFilter::from_default_env()),
48 )
49 .unwrap();
50 }
51
52 tracing_log::LogTracer::init().unwrap();
53}
54
55thread_local! {
56 static CURRENT_THREAD_ID: usize = thread_id::get();
57}