1// Some feature combinations result in some of these macros never being used.
2// Which is fine. Just squash the warnings.
3#![allow(dead_code, unused_macros)]
4
5macro_rules! log {
6 ($($tt:tt)*) => {
7 #[cfg(feature = "logging")]
8 {
9 $($tt)*
10 }
11 }
12}
13
14macro_rules! error {
15 ($($tt:tt)*) => { log!(log::error!($($tt)*)) }
16}
17
18macro_rules! warn {
19 ($($tt:tt)*) => { log!(log::warn!($($tt)*)) }
20}
21
22macro_rules! info {
23 ($($tt:tt)*) => { log!(log::info!($($tt)*)) }
24}
25
26macro_rules! debug {
27 ($($tt:tt)*) => { log!(log::debug!($($tt)*)) }
28}
29
30macro_rules! trace {
31 ($($tt:tt)*) => { log!(log::trace!($($tt)*)) }
32}
33
34/// A copy of std's `dbg!` macro that doesn't do pretty printing.
35///
36/// This is nice because we usually want more compact output in this crate.
37/// Also, because we don't import std's prelude, we have to use `std::dbg!`.
38/// This macro definition makes it available as `dbg!`.
39#[cfg(feature = "std")]
40macro_rules! dbg {
41 () => {
42 std::eprintln!(
43 "[{}:{}:{}]",
44 $crate::file!(),
45 $crate::line!(),
46 $crate::column!(),
47 )
48 };
49 ($val:expr $(,)?) => {
50 match $val {
51 tmp => {
52 std::eprintln!(
53 "[{}:{}:{}] {} = {:?}",
54 std::file!(),
55 std::line!(),
56 std::column!(),
57 std::stringify!($val),
58 &tmp,
59 );
60 tmp
61 }
62 }
63 };
64 ($($val:expr),+ $(,)?) => {
65 ($(dbg!($val)),+,)
66 };
67}
68
69/// The simplest possible logger that logs to stderr.
70///
71/// This logger does no filtering. Instead, it relies on the `log` crates
72/// filtering via its global max_level setting.
73///
74/// This provides a super simple logger that works with the `log` crate.
75/// We don't need anything fancy; just basic log levels and the ability to
76/// print to stderr. We therefore avoid bringing in extra dependencies just
77/// for this functionality.
78#[cfg(all(test))]
79#[derive(Debug)]
80pub(crate) struct Logger(());
81
82#[cfg(all(test, feature = "std", feature = "logging"))]
83const LOGGER: &'static Logger = &Logger(());
84
85#[cfg(all(test))]
86impl Logger {
87 /// Create a new logger that logs to stderr and initialize it as the
88 /// global logger. If there was a problem setting the logger, then an
89 /// error is returned.
90 pub(crate) fn init() -> Result<(), crate::Error> {
91 #[cfg(all(feature = "std", feature = "logging"))]
92 {
93 log::set_logger(LOGGER).map_err(crate::Error::adhoc)?;
94 log::set_max_level(log::LevelFilter::Trace);
95 Ok(())
96 }
97 #[cfg(not(all(feature = "std", feature = "logging")))]
98 {
99 Ok(())
100 }
101 }
102}
103
104#[cfg(all(test, feature = "std", feature = "logging"))]
105impl log::Log for Logger {
106 fn enabled(&self, _: &log::Metadata<'_>) -> bool {
107 // We set the log level via log::set_max_level, so we don't need to
108 // implement filtering here.
109 true
110 }
111
112 fn log(&self, record: &log::Record<'_>) {
113 match (record.file(), record.line()) {
114 (Some(file), Some(line)) => {
115 std::eprintln!(
116 "{}|{}|{}:{}: {}",
117 record.level(),
118 record.target(),
119 file,
120 line,
121 record.args()
122 );
123 }
124 (Some(file), None) => {
125 std::eprintln!(
126 "{}|{}|{}: {}",
127 record.level(),
128 record.target(),
129 file,
130 record.args()
131 );
132 }
133 _ => {
134 std::eprintln!(
135 "{}|{}: {}",
136 record.level(),
137 record.target(),
138 record.args()
139 );
140 }
141 }
142 }
143
144 fn flush(&self) {
145 // We use eprintln! which is flushed on every call.
146 }
147}
148