label_logger/
macros.rs

1//! The replacement macro that uses core logger functions.
2
3/// Prints a message with no or the provided label
4///
5/// ```
6/// # use label_logger::log;
7/// log!("Hello, world!");
8/// ```
9#[macro_export]
10macro_rules! log {
11	(label: $lbl:expr, $($arg:tt)+) => {
12		$crate::println_label($lbl, std::format!($($arg)+))
13	};
14	($($arg:tt)+) => {
15		$crate::println_label($crate::OutputLabel::default(), std::format!($($arg)*))
16	};
17}
18
19/// Prints a message with the error label (prints to `stderr`)
20///
21/// ```
22/// # use label_logger::error;
23/// error!("An error occurred while deactivating the nuclear core");
24/// // "Boom" will be displayed in red as a label
25/// error!(label: "Boom", "it is too late");
26/// ```
27#[macro_export]
28macro_rules! error {
29	(label: $lbl:tt, $($arg:tt)+) => {
30		$crate::println_label($crate::OutputLabel::Error($lbl), std::format!($($arg)+))
31	};
32	($($arg:tt)+) => {
33		$crate::println_label($crate::OutputLabel::Error("Error"), std::format!($($arg)+))
34	};
35}
36
37/// Prints a message with the warning label
38///
39/// ```
40/// # use label_logger::warn;
41/// warn!("This is fine, there are only 2849 warnings, but no errors");
42/// // "Tic Tac" will be displayed in yellow as a label
43/// warn!(label: "Tic Tac", "run forest, run!");
44/// ```
45#[macro_export]
46macro_rules! warn {
47	(label: $lbl:expr, $($arg:tt)+) => {
48		$crate::println_label($crate::OutputLabel::Warning($lbl), std::format!($($arg)+))
49	};
50	($($arg:tt)*) => {
51		$crate::println_label($crate::OutputLabel::Warning("Warn"), std::format!($($arg)*))
52	};
53}
54
55/// Prints a message with the info label and the provided text
56///
57/// ```
58/// # use label_logger::info;
59/// info!("Cleaning up the mess that was made");
60/// // "Waiting" will be displayed in cyan (light blue) as a label
61/// info!(label: "Waiting", "for OAuth2 callback");
62/// ```
63#[macro_export]
64macro_rules! info {
65	(label: $lbl:expr, $($arg:tt)+) => {
66		$crate::println_label($crate::OutputLabel::Info($lbl), std::format!($($arg)+))
67	};
68	($($arg:tt)+) => {
69		$crate::println_label($crate::OutputLabel::Info("Info"), std::format!($($arg)+))
70	};
71}
72
73/// Prints a message with the success label and the provided text
74///
75/// ```
76/// # use label_logger::success;
77/// success!("temporary file successfully deleted");
78/// // "Waouh" will be displayed in green as a label
79/// success!(label: "Waouh", "you successfully did not went on StackOverflow for 5min");
80/// ```
81#[macro_export]
82macro_rules! success {
83	(label: $lbl:expr, $($arg:tt)+) => {
84		$crate::println_label($crate::OutputLabel::Success($lbl), std::format!($($arg)+))
85	};
86	($($arg:tt)+) => {
87		$crate::println_label($crate::OutputLabel::Success("Success"), std::format!($($arg)+))
88	};
89}
90
91/// Formats your message with the specified output label
92///
93/// ```
94/// # use label_logger::{format_label, OutputLabel};
95/// let _msg = format_label!(
96///     label: OutputLabel::Info("Building"),
97///     "one crate at a time"
98/// );
99/// ```
100///
101/// # Example
102///
103/// It can be useful when you need to have a correctly formatted message but you don't want to print it directly to the terminal.
104///
105// indicatif is only available on `indicatif` feature
106/// ```ignore
107/// use label_logger::{format_label, OutputLabel};
108/// use indicatif::ProgressBar;
109///
110/// let mut bar = ProgressBar::new(100);
111/// let msg = format_label!(
112///    label: OutputLabel::Info("Building"),
113///   "one crate at a time"
114/// );
115///
116/// bar.set_message(msg);
117/// ```
118#[macro_export]
119macro_rules! format_label {
120	(label: $label:expr, $($arg:tt)+) => {
121		$crate::pretty_output($label, std::format!($($arg)+))
122	};
123	($($arg:tt)+) => {
124		$crate::pretty_output($crate::OutputLabel::default(), std::format!($($arg)+))
125	};
126}
127
128#[cfg(test)]
129mod tests {
130	use crate::OutputLabel;
131
132	#[test]
133	fn log_macro_expand() {
134		log!("Hello, world!");
135
136		log!(label: OutputLabel::Error("Error"), "Hello");
137	}
138
139	#[test]
140	fn error_macro_expand() {
141		error!("Hello, error!");
142
143		error!(label: "Bip Bip", "alert, everything is broken");
144	}
145
146	#[test]
147	fn warn_macro_expand() {
148		warn!("Hello, warn!");
149
150		warn!(label: "Wow", "there is a bug here!");
151	}
152
153	#[test]
154	fn info_macro_expand() {
155		info!("Hello, info!");
156
157		info!(label: "Ping", "new message");
158	}
159
160	#[test]
161	fn success_macro_expand() {
162		success!("Hello, success!");
163
164		success!(label: "Nice", "you succeed!");
165	}
166
167	#[test]
168	fn format_label_macro_expand() {
169		format_label!(label: OutputLabel::Info("Hey"), "Hello, world!");
170	}
171}