iceoryx2_bb_log/
log.rs

1// Copyright (c) 2023 Contributors to the Eclipse Foundation
2//
3// See the NOTICE file(s) distributed with this work for additional
4// information regarding copyright ownership.
5//
6// This program and the accompanying materials are made available under the
7// terms of the Apache Software License 2.0 which is available at
8// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
9// which is available at https://opensource.org/licenses/MIT.
10//
11// SPDX-License-Identifier: Apache-2.0 OR MIT
12
13//! Contains all macros to log messages.
14
15/// Logs a trace message.
16///
17/// ```
18/// use iceoryx2_bb_log::trace;
19///
20/// #[derive(Debug)]
21/// struct MyDataType {}
22///
23/// impl MyDataType {
24///     fn something_that_fails(&self) -> Result<(), ()> {
25///         Err(())
26///     }
27///
28///     fn doIt(&self) {
29///         trace!("Only a message");
30///         trace!(from self, "Message which adds the object as its origin");
31///         trace!(from "Somewhere over the Rainbow", "Message with custom origin");
32///
33///         trace!(from self, when self.something_that_fails(),
34///             "Print only when result.is_err()")
35///     }
36/// }
37/// ```
38#[macro_export(local_inner_macros)]
39macro_rules! trace {
40    ($($e:expr),*) => {
41        $crate::__internal_print_log_msg($crate::LogLevel::Trace, std::format_args!(""), std::format_args!($($e),*))
42    };
43    (from $o:expr, $($e:expr),*) => {
44        $crate::__internal_print_log_msg($crate::LogLevel::Trace, std::format_args!("{:?}", $o), std::format_args!($($e),*))
45    };
46    (from $o:expr, when $call:expr, $($e:expr),*) => {
47        {
48            let result = $call;
49            if result.is_err() {
50                $crate::__internal_print_log_msg($crate::LogLevel::Trace, std::format_args!("{:?}", $o), std::format_args!($($e),*))
51            }
52        }
53    }
54}
55
56/// Logs a debug message.
57///
58/// ```
59/// use iceoryx2_bb_log::debug;
60///
61/// #[derive(Debug)]
62/// struct MyDataType {}
63///
64/// impl MyDataType {
65///     fn something_that_fails(&self) -> Result<(), ()> {
66///         Err(())
67///     }
68///
69///     fn doIt(&self) {
70///         debug!("Only a message");
71///         debug!(from self, "Message which adds the object as its origin");
72///         debug!(from "Somewhere over the Rainbow", "Message with custom origin");
73///
74///         debug!(from self, when self.something_that_fails(),
75///             "Print only when result.is_err()")
76///     }
77/// }
78/// ```
79#[macro_export(local_inner_macros)]
80macro_rules! debug {
81    ($($e:expr),*) => {
82        $crate::__internal_print_log_msg($crate::LogLevel::Debug, std::format_args!(""), std::format_args!($($e),*))
83    };
84    (from $o:expr, $($e:expr),*) => {
85        $crate::__internal_print_log_msg($crate::LogLevel::Debug, std::format_args!("{:?}", $o), std::format_args!($($e),*))
86    };
87    (from $o:expr, when $call:expr, $($e:expr),*) => {
88        {
89            let result = $call;
90            if result.is_err() {
91                $crate::__internal_print_log_msg($crate::LogLevel::Debug, std::format_args!("{:?}", $o), std::format_args!($($e),*))
92            }
93        }
94    }
95}
96
97/// Logs a info message.
98///
99/// ```
100/// use iceoryx2_bb_log::info;
101///
102/// #[derive(Debug)]
103/// struct MyDataType {}
104///
105/// impl MyDataType {
106///     fn something_that_fails(&self) -> Result<(), ()> {
107///         Err(())
108///     }
109///
110///     fn doIt(&self) {
111///         info!("Only a message");
112///         info!(from self, "Message which adds the object as its origin");
113///         info!(from "Somewhere over the Rainbow", "Message with custom origin");
114///
115///         info!(from self, when self.something_that_fails(),
116///             "Print only when result.is_err()")
117///     }
118/// }
119/// ```
120#[macro_export(local_inner_macros)]
121macro_rules! info {
122    ($($e:expr),*) => {
123        $crate::__internal_print_log_msg($crate::LogLevel::Info, std::format_args!(""), std::format_args!($($e),*))
124    };
125    (from $o:expr, $($e:expr),*) => {
126        $crate::__internal_print_log_msg($crate::LogLevel::Info, std::format_args!("{:?}", $o), std::format_args!($($e),*))
127    };
128    (from $o:expr, when $call:expr, $($e:expr),*) => {
129        {
130            let result = $call;
131            if result.is_err() {
132                $crate::__internal_print_log_msg($crate::LogLevel::Info, std::format_args!("{:?}", $o), std::format_args!($($e),*))
133            }
134        }
135    }
136}
137
138/// Logs a warn message.
139///
140/// ```
141/// use iceoryx2_bb_log::warn;
142///
143/// #[derive(Debug)]
144/// struct MyDataType {}
145///
146/// impl MyDataType {
147///     fn something_that_fails(&self) -> Result<(), ()> {
148///         Err(())
149///     }
150///
151///     fn doIt(&self) {
152///         warn!("Only a message");
153///         warn!(from self, "Message which adds the object as its origin");
154///         warn!(from "Somewhere over the Rainbow", "Message with custom origin");
155///
156///         warn!(from self, when self.something_that_fails(),
157///             "Print only when result.is_err()")
158///     }
159/// }
160/// ```
161#[macro_export(local_inner_macros)]
162macro_rules! warn {
163    ($($e:expr),*) => {
164        $crate::__internal_print_log_msg($crate::LogLevel::Warn, std::format_args!(""), std::format_args!($($e),*))
165    };
166    (from $o:expr, $($e:expr),*) => {
167        $crate::__internal_print_log_msg($crate::LogLevel::Warn, std::format_args!("{:?}", $o), std::format_args!($($e),*))
168    };
169    (from $o:expr, when $call:expr, $($e:expr),*) => {
170        {
171            let result = $call;
172            if result.is_err() {
173                $crate::__internal_print_log_msg($crate::LogLevel::Warn, std::format_args!("{:?}", $o), std::format_args!($($e),*))
174            }
175        }
176    }
177}
178
179/// Logs an error message.
180///
181/// ```
182/// use iceoryx2_bb_log::error;
183///
184/// #[derive(Debug)]
185/// struct MyDataType {}
186///
187/// impl MyDataType {
188///     fn something_that_fails(&self) -> Result<(), ()> {
189///         Err(())
190///     }
191///
192///     fn doIt(&self) {
193///         error!("Only a message");
194///         error!(from self, "Message which adds the object as its origin");
195///         error!(from "Somewhere over the Rainbow", "Message with custom origin");
196///
197///         error!(from self, when self.something_that_fails(),
198///             "Print only when result.is_err()")
199///     }
200/// }
201/// ```
202#[macro_export(local_inner_macros)]
203macro_rules! error {
204    ($($e:expr),*) => {
205        $crate::__internal_print_log_msg($crate::LogLevel::Error, std::format_args!(""), std::format_args!($($e),*))
206    };
207    (from $o:expr, $($e:expr),*) => {
208        $crate::__internal_print_log_msg($crate::LogLevel::Error, std::format_args!("{:?}", $o), std::format_args!($($e),*))
209    };
210    (from $o:expr, when $call:expr, $($e:expr),*) => {
211        {
212            let result = $call;
213            if result.is_err() {
214                $crate::__internal_print_log_msg($crate::LogLevel::Error, std::format_args!("{:?}", $o), std::format_args!($($e),*))
215            }
216        }
217    }
218}
219
220/// Logs a fatal error message and calls panic.
221///
222/// ```
223/// use iceoryx2_bb_log::fatal_panic;
224///
225/// #[derive(Debug)]
226/// struct MyDataType {}
227///
228/// impl MyDataType {
229///     fn something_that_fails(&self) -> Result<(), ()> {
230///         Err(())
231///     }
232///
233///     fn doIt(&self) {
234///         fatal_panic!("Only a message");
235///         fatal_panic!(from self, "Message which adds the object as its origin");
236///         fatal_panic!(from "Somewhere over the Rainbow", "Message with custom origin");
237///
238///         fatal_panic!(from self, when self.something_that_fails(),
239///             "Print only when result.is_err()")
240///     }
241/// }
242/// ```
243#[macro_export(local_inner_macros)]
244macro_rules! fatal_panic {
245    ($($e:expr),*) => {
246        {
247            $crate::__internal_print_log_msg($crate::LogLevel::Fatal, std::format_args!(""), std::format_args!($($e),*));
248            std::panic!($($e),*);
249        }
250    };
251    (from $o:expr, $($e:expr),*) => {
252        {
253            $crate::__internal_print_log_msg($crate::LogLevel::Fatal, std::format_args!("{:?}", $o), std::format_args!($($e),*));
254            std::panic!("From: {:?} ::: {}", $o, std::format_args!($($e),*));
255        }
256    };
257    (from $o:expr, when $call:expr, $($e:expr),*) => {
258        {
259            let result = $call;
260            if result.is_err() {
261                $crate::__internal_print_log_msg($crate::LogLevel::Fatal, std::format_args!("{:?}", $o), std::format_args!($($e),*));
262                std::panic!("From: {:?} ::: {}", $o, std::format_args!($($e),*));
263            }
264            result.ok().unwrap()
265        }
266    }
267}