prettylogger/
glob.rs

1use std::sync::{
2    LazyLock,
3    RwLock
4};
5
6use crate::Logger;
7
8/// Global `Logger` struct that can be used with the `debug!`, `info!`, `warn!`,
9/// `err!`, and `fatal!` macros.
10///
11/// > **Note**
12/// > Do not use any log macros when you are writing to the global logger, it
13/// > will cause your thread to lock.
14///
15/// This will block the thread:
16/// ```no_run
17/// # use prettylogger::{info, glob::LOGGER};
18/// // Get write access to the logger
19/// let mut logger = LOGGER.write().unwrap();
20///
21/// // Trying to use logging macro blocks the thread
22/// info!("This will never be shown!");
23/// ````
24///
25/// # Examples
26///
27/// Using global logging:
28/// ```
29/// # use prettylogger::{debug, info, warn, err, fatal};
30/// debug!("This is a debug message!");
31/// info!("This is an info message!");
32/// warn!("This is a warning!");
33/// err!("This is an error!");
34/// fatal!("This is a fatal error!");
35/// ```
36///
37/// Configuring the global logger:
38/// ```
39/// # use prettylogger::{
40/// #     config::Verbosity,
41/// #     glob::LOGGER,
42/// #     debug
43/// # };
44/// // Configure logger
45/// let mut logger = LOGGER.write().unwrap();
46/// logger.set_verbosity(Verbosity::All);
47///
48/// // Drop the logger so a read lock can be acquired
49/// drop(logger);
50///
51/// // Then print a message
52/// debug!("This should be shown!");
53/// ```
54pub static LOGGER: LazyLock<RwLock<Logger>>
55    = LazyLock::new(|| RwLock::new(Logger::default()));
56
57
58/// Prints a debug message using the global `Logger` instance.
59///
60/// Not to be confused with the `dbg!` macro.
61///
62/// > **Warning!**
63/// > This macro will block if any thread holds write access to the global
64/// > logger.
65///
66/// # Panics
67/// Panics if the global logger's lock is poisoned.
68///
69/// # Examples
70///
71/// Using the `debug!` macro:
72/// ```
73/// use prettylogger::debug;
74/// let name = String::from("world");
75/// debug!("Hello, {name}!");
76/// ```
77#[macro_export]
78macro_rules! debug {
79    ($($t:tt)*) => {{
80        use $crate::glob::LOGGER;
81        LOGGER
82            .read()
83            .unwrap()
84            .debug(&format!($($t)*));
85    }};
86}
87
88/// Prints an info message using the global `Logger` instance.
89///
90/// > **Warning!**
91/// > This macro will block if any thread holds write access to the global
92/// > logger.
93///
94/// # Panics
95/// Panics if the global logger's lock is poisoned.
96///
97/// # Examples
98///
99/// Using the `info!` macro:
100/// ```
101/// use prettylogger::info;
102/// let name = String::from("world");
103/// info!("Hello, {name}!");
104/// ```
105#[macro_export]
106macro_rules! info {
107    ($($t:tt)*) => {{
108        use $crate::glob::LOGGER;
109        LOGGER
110            .read()
111            .unwrap()
112            .info(&format!($($t)*));
113    }};
114}
115
116/// Prints a warning using the global `Logger` instance.
117///
118/// > **Warning!**
119/// > This macro will block if any thread holds write access to the global
120/// > logger.
121///
122/// # Panics
123/// Panics if the global logger's lock is poisoned.
124///
125/// # Examples
126///
127/// Using the `warn!` macro:
128/// ```
129/// use prettylogger::warn;
130/// let name = String::from("world");
131/// warn!("Hello, {name}!");
132/// ```
133#[macro_export]
134macro_rules! warn {
135    ($($t:tt)*) => {{
136        use $crate::glob::LOGGER;
137        LOGGER
138            .read()
139            .unwrap()
140            .warning(&format!($($t)*));
141    }};
142}
143
144/// Prints an error using the global `Logger` instance.
145///
146/// > **Warning!**
147/// > This macro will block if any thread holds write access to the global
148/// > logger.
149///
150/// # Panics
151/// Panics if the global logger's lock is poisoned.
152///
153/// # Examples
154///
155/// Using the `err!` macro:
156/// ```
157/// use prettylogger::err;
158/// let name = String::from("world");
159/// err!("Hello, {name}!");
160/// ```
161#[macro_export]
162macro_rules! err {
163    ($($t:tt)*) => {{
164        use $crate::glob::LOGGER;
165        LOGGER
166            .read()
167            .unwrap()
168            .error(&format!($($t)*));
169    }};
170}
171
172/// Prints a fatal error using the global `Logger` instance.
173///
174/// > **Warning!**
175/// > This macro will block if any thread holds write access to the global
176/// > logger.
177///
178/// # Panics
179/// Panics if the global logger's lock is poisoned.
180///
181/// # Examples
182///
183/// Using the `fatal!` macro:
184/// ```
185/// use prettylogger::fatal;
186/// let name = String::from("world");
187/// fatal!("Hello, {name}!");
188/// ```
189#[macro_export]
190macro_rules! fatal {
191    ($($t:tt)*) => {{
192        use $crate::glob::LOGGER;
193        LOGGER
194            .read()
195            .unwrap()
196            .fatal(&format!($($t)*));
197    }};
198}