Skip to main content

reovim_kernel/printk/
macros.rs

1//! Logging macros for kernel messages.
2//!
3//! Linux equivalent: `pr_err()`, `pr_warn()`, `pr_info()`, `pr_debug()` in `include/linux/printk.h`
4//!
5//! These macros provide a convenient way to log messages at different severity levels.
6//! They check the log level before formatting to avoid allocation overhead when
7//! logging is disabled.
8
9/// Logs a message at the Error level.
10///
11/// Use for critical errors that require immediate attention and may prevent
12/// normal operation.
13///
14/// # Example
15///
16/// ```
17/// use reovim_kernel::api::v1::pr_err;
18///
19/// let buffer_id = 42;
20/// pr_err!("failed to save buffer {}", buffer_id);
21/// pr_err!("critical error occurred");
22/// ```
23#[macro_export]
24macro_rules! pr_err {
25    ($($arg:tt)*) => {
26        if $crate::api::v1::logger().enabled($crate::api::v1::Level::Error) {
27            $crate::api::v1::__log(
28                $crate::api::v1::Level::Error,
29                module_path!(),
30                file!(),
31                line!(),
32                format_args!($($arg)*)
33            )
34        }
35    };
36}
37
38/// Logs a message at the Warn level.
39///
40/// Use for warning conditions that don't prevent operation but indicate
41/// potential problems.
42///
43/// # Example
44///
45/// ```
46/// use reovim_kernel::api::v1::pr_warn;
47///
48/// let path = "/tmp/file.txt";
49/// pr_warn!("file {} not found, using default", path);
50/// pr_warn!("deprecated configuration option used");
51/// ```
52#[macro_export]
53macro_rules! pr_warn {
54    ($($arg:tt)*) => {
55        if $crate::api::v1::logger().enabled($crate::api::v1::Level::Warn) {
56            $crate::api::v1::__log(
57                $crate::api::v1::Level::Warn,
58                module_path!(),
59                file!(),
60                line!(),
61                format_args!($($arg)*)
62            )
63        }
64    };
65}
66
67/// Logs a message at the Info level.
68///
69/// Use for general informational messages about normal operation.
70///
71/// # Example
72///
73/// ```
74/// use reovim_kernel::api::v1::pr_info;
75///
76/// pr_info!("editor started");
77/// pr_info!("opened {} buffers", 3);
78/// ```
79#[macro_export]
80macro_rules! pr_info {
81    ($($arg:tt)*) => {
82        if $crate::api::v1::logger().enabled($crate::api::v1::Level::Info) {
83            $crate::api::v1::__log(
84                $crate::api::v1::Level::Info,
85                module_path!(),
86                file!(),
87                line!(),
88                format_args!($($arg)*)
89            )
90        }
91    };
92}
93
94/// Logs a message at the Debug level.
95///
96/// Use for detailed diagnostic information useful during development.
97/// These messages are typically disabled in release builds.
98///
99/// # Example
100///
101/// ```
102/// use reovim_kernel::api::v1::pr_debug;
103///
104/// let cursor_pos = (10, 5);
105/// pr_debug!("cursor moved to {:?}", cursor_pos);
106/// pr_debug!("entering function");
107/// ```
108#[macro_export]
109macro_rules! pr_debug {
110    ($($arg:tt)*) => {
111        if $crate::api::v1::logger().enabled($crate::api::v1::Level::Debug) {
112            $crate::api::v1::__log(
113                $crate::api::v1::Level::Debug,
114                module_path!(),
115                file!(),
116                line!(),
117                format_args!($($arg)*)
118            )
119        }
120    };
121}
122
123/// Logs a message at the Trace level.
124///
125/// Use for very detailed tracing of execution flow. This is the most
126/// verbose level and should be used sparingly.
127///
128/// # Example
129///
130/// ```
131/// use reovim_kernel::api::v1::pr_trace;
132///
133/// pr_trace!("processing event");
134/// pr_trace!("loop iteration {}", 42);
135/// ```
136#[macro_export]
137macro_rules! pr_trace {
138    ($($arg:tt)*) => {
139        if $crate::api::v1::logger().enabled($crate::api::v1::Level::Trace) {
140            $crate::api::v1::__log(
141                $crate::api::v1::Level::Trace,
142                module_path!(),
143                file!(),
144                line!(),
145                format_args!($($arg)*)
146            )
147        }
148    };
149}