Skip to main content

tuwunel_core/log/
mod.rs

1//! Configures structured logging and in-memory capture.
2//!
3//! The module builds tracing layers for supported output targets. It also
4//! exposes scoped capture and reload controls to the server.
5
6pub mod capture;
7pub mod color;
8pub mod console;
9pub mod fmt;
10pub mod fmt_span;
11pub mod journald;
12mod reload;
13mod suppress;
14
15use std::sync::Arc;
16
17pub use tracing::{Level, subscriber::Subscriber};
18pub use tracing_core::{Event, Metadata};
19pub use tracing_subscriber::EnvFilter;
20
21pub use self::{
22	capture::Capture,
23	console::{ConsoleFormat, ConsoleWriter, ansi_enabled, is_systemd_mode, is_terminal_mode},
24	reload::{LogLevelReloadHandles, ReloadHandle},
25	suppress::Suppress,
26};
27
28/// Owns the server's shared logging and tracing state.
29///
30/// The singleton groups the configured subscriber, reload handles, and
31/// ephemeral capture registry. `Server` exposes this value to components that
32/// adjust logging.
33pub struct Logging {
34	/// Retains the subscriber configured for the server.
35	///
36	/// Initialization may register it globally when `log_global_default` is
37	/// enabled. Otherwise no global registration occurs, and disabled logging
38	/// can retain a no-op subscriber.
39	pub subscriber: Arc<dyn Subscriber + Send + Sync>,
40
41	/// Named handles for changing subscriber filters at runtime.
42	///
43	/// Administrative commands and scoped guards use these handles without
44	/// knowing the concrete subscriber stack type.
45	pub reload: LogLevelReloadHandles,
46
47	/// Shared registration state for ephemeral tracing captures.
48	///
49	/// Capture guards add and remove callbacks observed by the capture layer.
50	pub capture: Arc<capture::State>,
51}
52
53// Wraps for logging macros. Use these macros rather than extern tracing:: or
54// log:: crates in project code. ::log and ::tracing can still be used if
55// necessary but discouraged. Remember debug_ log macros are also exported to
56// the crate namespace like these.
57
58/// Emits a tracing event at a caller-supplied level.
59///
60/// The macro forwards tracing fields and message tokens to `tracing::event!`.
61/// Project code can use it without importing the tracing crate directly.
62#[macro_export]
63#[collapse_debuginfo(yes)]
64macro_rules! event {
65	( $level:expr_2021, $($x:tt)+ ) => { ::tracing::event!( $level, $($x)+ ) }
66}
67
68/// Emits an error-level tracing event.
69///
70/// The macro forwards all fields and message tokens to `tracing::error!`.
71/// Project code can use it without importing the tracing crate directly.
72#[macro_export]
73macro_rules! error {
74    ( $($x:tt)+ ) => { ::tracing::error!( $($x)+ ) }
75}
76
77/// Emits a warning-level tracing event.
78///
79/// The macro forwards all fields and message tokens to `tracing::warn!`.
80/// Project code can use it without importing the tracing crate directly.
81#[macro_export]
82macro_rules! warn {
83    ( $($x:tt)+ ) => { ::tracing::warn!( $($x)+ ) }
84}
85
86/// Emits an informational tracing event.
87///
88/// The macro forwards all fields and message tokens to `tracing::info!`.
89/// Project code can use it without importing the tracing crate directly.
90#[macro_export]
91macro_rules! info {
92    ( $($x:tt)+ ) => { ::tracing::info!( $($x)+ ) }
93}
94
95/// Emits a debug-level tracing event.
96///
97/// The macro forwards all fields and message tokens to `tracing::debug!`.
98/// Project code can use it without importing the tracing crate directly.
99#[macro_export]
100macro_rules! debug {
101    ( $($x:tt)+ ) => { ::tracing::debug!( $($x)+ ) }
102}
103
104/// Emits a trace-level tracing event.
105///
106/// The macro forwards all fields and message tokens to `tracing::trace!`.
107/// Project code can use it without importing the tracing crate directly.
108#[macro_export]
109macro_rules! trace {
110    ( $($x:tt)+ ) => { ::tracing::trace!( $($x)+ ) }
111}