witchcraft_log/
macros.rs

1// Copyright 2019 Palantir Technologies, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/// Logs a message at the specified level.
16#[macro_export]
17macro_rules! log {
18    ($lvl:expr, $msg:expr) => {{
19        let level = $lvl;
20        if level <= $crate::max_level() {
21            $crate::private::log_minimal(
22                level,
23                &(module_path!(), file!(), line!(), $msg),
24            );
25        }
26    }};
27    (
28        $lvl:expr,
29        $msg:expr
30        $(, safe: { $($safe_key:ident: $safe_value:expr),* $(,)? })?
31        $(, unsafe: { $($unsafe_key:ident: $unsafe_value:expr),* $(,)? })?
32        $(, error: $error:expr)?
33        $(,)?
34    ) => {{
35        let level = $lvl;
36        if level <= $crate::max_level() {
37            $crate::private::log(
38                level,
39                &(module_path!(), file!(), line!(), $msg),
40                &[$($((stringify!($safe_key), &$safe_value)),*)*],
41                &[$($((stringify!($unsafe_key), &$unsafe_value)),*)*],
42                None $(.or(Some(&$error)))?,
43            );
44        }
45    }};
46}
47
48/// Logs a message at the "fatal" level.
49#[macro_export]
50macro_rules! fatal {
51    ($($v:tt)*) => {
52        $crate::log!($crate::Level::Fatal, $($v)*)
53    }
54}
55
56/// Logs a message at the "error" level.
57#[macro_export]
58macro_rules! error {
59    ($($v:tt)*) => {
60        $crate::log!($crate::Level::Error, $($v)*)
61    }
62}
63
64/// Logs a message at the "warn" level.
65#[macro_export]
66macro_rules! warn {
67    ($($v:tt)*) => {
68        $crate::log!($crate::Level::Warn, $($v)*)
69    }
70}
71
72/// Logs a message at the "info" level.
73#[macro_export]
74macro_rules! info {
75    ($($v:tt)*) => {
76        $crate::log!($crate::Level::Info, $($v)*)
77    }
78}
79
80/// Logs a message at the "debug" level.
81#[macro_export]
82macro_rules! debug {
83    ($($v:tt)*) => {
84        $crate::log!($crate::Level::Debug, $($v)*)
85    }
86}
87
88/// Logs a message at the "trace" level.
89#[macro_export]
90macro_rules! trace {
91    ($($v:tt)*) => {
92        $crate::log!($crate::Level::Trace, $($v)*)
93    }
94}
95
96/// Determines if a message logged at the specified level in the same module would be logged or not.
97#[macro_export]
98macro_rules! enabled {
99    ($lvl:expr) => {{
100        let level = $lvl;
101        level <= $crate::max_level() && $crate::private::enabled(level, module_path!())
102    }};
103}