Debian Codegraph @

vendor/log/src/lib.rs

raw
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! A lightweight logging facade.
12 //!
13 //! The `log` crate provides a single logging API that abstracts over the
14 //! actual logging implementation. Libraries can use the logging API provided
15 //! by this crate, and the consumer of those libraries can choose the logging
16 //! implementation that is most suitable for its use case.
17 //!
18 //! If no logging implementation is selected, the facade falls back to a "noop"
19 //! implementation that ignores all log messages. The overhead in this case
20 //! is very small - just an integer load, comparison and jump.
21 //!
22 //! A log request consists of a _target_, a _level_, and a _body_. A target is a
23 //! string which defaults to the module path of the location of the log request,
24 //! though that default may be overridden. Logger implementations typically use
25 //! the target to filter requests based on some user configuration.
26 //!
27 //! # Usage
28 //!
29 //! The basic use of the log crate is through the five logging macros: [`error!`],
30 //! [`warn!`], [`info!`], [`debug!`] and [`trace!`]
31 //! where `error!` represents the highest-priority log messages
32 //! and `trace!` the lowest. The log messages are filtered by configuring
33 //! the log level to exclude messages with a lower priority.
34 //! Each of these macros accept format strings similarly to [`println!`].
35 //!
36 //!
37 //! [`error!`]: ./macro.error.html
38 //! [`warn!`]: ./macro.warn.html
39 //! [`info!`]: ./macro.info.html
40 //! [`debug!`]: ./macro.debug.html
41 //! [`trace!`]: ./macro.trace.html
42 //! [`println!`]: https://doc.rust-lang.org/stable/std/macro.println.html
43 //!
44 //! Avoid writing expressions with side-effects in log statements. They may not be evaluated.
45 //!
46 //! ## In libraries
47 //!
48 //! Libraries should link only to the `log` crate, and use the provided
49 //! macros to log whatever information will be useful to downstream consumers.
50 //!
51 //! ### Examples
52 //!
53 //! ```
54 //! # #[derive(Debug)] pub struct Yak(String);
55 //! # impl Yak { fn shave(&mut self, _: u32) {} }
56 //! # fn find_a_razor() -> Result<u32, u32> { Ok(1) }
57 //! use log::{info, warn};
58 //!
59 //! pub fn shave_the_yak(yak: &mut Yak) {
60 //! info!(target: "yak_events", "Commencing yak shaving for {yak:?}");
61 //!
62 //! loop {
63 //! match find_a_razor() {
64 //! Ok(razor) => {
65 //! info!("Razor located: {razor}");
66 //! yak.shave(razor);
67 //! break;
68 //! }
69 //! Err(err) => {
70 //! warn!("Unable to locate a razor: {err}, retrying");
71 //! }
72 //! }
73 //! }
74 //! }
75 //! # fn main() {}
76 //! ```
77 //!
78 //! ## In executables
79 //!
80 //! Executables should choose a logging implementation and initialize it early in the
81 //! runtime of the program. Logging implementations will typically include a
82 //! function to do this. Any log messages generated before
83 //! the implementation is initialized will be ignored.
84 //!
85 //! The executable itself may use the `log` crate to log as well.
86 //!
87 //! ### Warning
88 //!
89 //! The logging system may only be initialized once.
90 //!
91 //! ## Structured logging
92 //!
93 //! If you enable the `kv` feature you can associate structured values
94 //! with your log records. If we take the example from before, we can include
95 //! some additional context besides what's in the formatted message:
96 //!
97 //! ```
98 //! # use serde::Serialize;
99 //! # #[derive(Debug, Serialize)] pub struct Yak(String);
100 //! # impl Yak { fn shave(&mut self, _: u32) {} }
101 //! # fn find_a_razor() -> Result<u32, std::io::Error> { Ok(1) }
102 //! # #[cfg(feature = "kv_serde")]
103 //! # fn main() {
104 //! use log::{info, warn};
105 //!
106 //! pub fn shave_the_yak(yak: &mut Yak) {
107 //! info!(target: "yak_events", yak:serde; "Commencing yak shaving");
108 //!
109 //! loop {
110 //! match find_a_razor() {
111 //! Ok(razor) => {
112 //! info!(razor; "Razor located");
113 //! yak.shave(razor);
114 //! break;
115 //! }
116 //! Err(e) => {
117 //! warn!(e:err; "Unable to locate a razor, retrying");
118 //! }
119 //! }
120 //! }
121 //! }
122 //! # }
123 //! # #[cfg(not(feature = "kv_serde"))]
124 //! # fn main() {}
125 //! ```
126 //!
127 //! See the [`kv`] module documentation for more details.
128 //!
129 //! # Available logging implementations
130 //!
131 //! In order to produce log output executables have to use
132 //! a logger implementation compatible with the facade.
133 //! There are many available implementations to choose from,
134 //! here are some of the most popular ones:
135 //!
136 //! * Simple minimal loggers:
137 //! * [env_logger]
138 //! * [colog]
139 //! * [simple_logger]
140 //! * [simplelog]
141 //! * [pretty_env_logger]
142 //! * [stderrlog]
143 //! * [flexi_logger]
144 //! * [call_logger]
145 //! * [std-logger]
146 //! * [structured-logger]
147 //! * [clang_log]
148 //! * [ftail]
149 //! * Complex configurable frameworks:
150 //! * [log4rs]
151 //! * [logforth]
152 //! * [fern]
153 //! * [spdlog-rs]
154 //! * Adaptors for other facilities:
155 //! * [syslog]
156 //! * [slog-stdlog]
157 //! * [systemd-journal-logger]
158 //! * [android_log]
159 //! * [win_dbg_logger]
160 //! * [db_logger]
161 //! * [log-to-defmt]
162 //! * [logcontrol-log]
163 //! * For WebAssembly binaries:
164 //! * [console_log]
165 //! * For dynamic libraries:
166 //! * You may need to construct an FFI-safe wrapper over `log` to initialize in your libraries
167 //! * Utilities:
168 //! * [log_err]
169 //! * [log-reload]
170 //! * [alterable_logger]
171 //!
172 //! # Implementing a Logger
173 //!
174 //! Loggers implement the [`Log`] trait. Here's a very basic example that simply
175 //! logs all messages at the [`Error`][level_link], [`Warn`][level_link] or
176 //! [`Info`][level_link] levels to stdout:
177 //!
178 //! ```
179 //! use log::{Record, Level, Metadata};
180 //!
181 //! struct SimpleLogger;
182 //!
183 //! impl log::Log for SimpleLogger {
184 //! fn enabled(&self, metadata: &Metadata) -> bool {
185 //! metadata.level() <= Level::Info
186 //! }
187 //!
188 //! fn log(&self, record: &Record) {
189 //! if self.enabled(record.metadata()) {
190 //! println!("{} - {}", record.level(), record.args());
191 //! }
192 //! }
193 //!
194 //! fn flush(&self) {}
195 //! }
196 //!
197 //! # fn main() {}
198 //! ```
199 //!
200 //! Loggers are installed by calling the [`set_logger`] function. The maximum
201 //! log level also needs to be adjusted via the [`set_max_level`] function. The
202 //! logging facade uses this as an optimization to improve performance of log
203 //! messages at levels that are disabled. It's important to set it, as it
204 //! defaults to [`Off`][filter_link], so no log messages will ever be captured!
205 //! In the case of our example logger, we'll want to set the maximum log level
206 //! to [`Info`][filter_link], since we ignore any [`Debug`][level_link] or
207 //! [`Trace`][level_link] level log messages. A logging implementation should
208 //! provide a function that wraps a call to [`set_logger`] and
209 //! [`set_max_level`], handling initialization of the logger:
210 //!
211 //! ```
212 //! # use log::{Level, Metadata};
213 //! # struct SimpleLogger;
214 //! # impl log::Log for SimpleLogger {
215 //! # fn enabled(&self, _: &Metadata) -> bool { false }
216 //! # fn log(&self, _: &log::Record) {}
217 //! # fn flush(&self) {}
218 //! # }
219 //! # fn main() {}
220 //! use log::{SetLoggerError, LevelFilter};
221 //!
222 //! static LOGGER: SimpleLogger = SimpleLogger;
223 //!
224 //! pub fn init() -> Result<(), SetLoggerError> {
225 //! log::set_logger(&LOGGER)
226 //! .map(|()| log::set_max_level(LevelFilter::Info))
227 //! }
228 //! ```
229 //!
230 //! Implementations that adjust their configurations at runtime should take care
231 //! to adjust the maximum log level as well.
232 //!
233 //! # Use with `std`
234 //!
235 //! `set_logger` requires you to provide a `&'static Log`, which can be hard to
236 //! obtain if your logger depends on some runtime configuration. The
237 //! `set_boxed_logger` function is available with the `std` Cargo feature. It is
238 //! identical to `set_logger` except that it takes a `Box<Log>` rather than a
239 //! `&'static Log`:
240 //!
241 //! ```
242 //! # use log::{Level, LevelFilter, Log, SetLoggerError, Metadata};
243 //! # struct SimpleLogger;
244 //! # impl log::Log for SimpleLogger {
245 //! # fn enabled(&self, _: &Metadata) -> bool { false }
246 //! # fn log(&self, _: &log::Record) {}
247 //! # fn flush(&self) {}
248 //! # }
249 //! # fn main() {}
250 //! # #[cfg(feature = "std")]
251 //! pub fn init() -> Result<(), SetLoggerError> {
252 //! log::set_boxed_logger(Box::new(SimpleLogger))
253 //! .map(|()| log::set_max_level(LevelFilter::Info))
254 //! }
255 //! ```
256 //!
257 //! # Compile time filters
258 //!
259 //! Log levels can be statically disabled at compile time by enabling one of these Cargo features:
260 //!
261 //! * `max_level_off`
262 //! * `max_level_error`
263 //! * `max_level_warn`
264 //! * `max_level_info`
265 //! * `max_level_debug`
266 //! * `max_level_trace`
267 //!
268 //! Log invocations at disabled levels will be skipped and will not even be present in the
269 //! resulting binary. These features control the value of the `STATIC_MAX_LEVEL` constant. The
270 //! logging macros check this value before logging a message. By default, no levels are disabled.
271 //!
272 //! It is possible to override this level for release builds only with the following features:
273 //!
274 //! * `release_max_level_off`
275 //! * `release_max_level_error`
276 //! * `release_max_level_warn`
277 //! * `release_max_level_info`
278 //! * `release_max_level_debug`
279 //! * `release_max_level_trace`
280 //!
281 //! Libraries should avoid using the max level features because they're global and can't be changed
282 //! once they're set.
283 //!
284 //! For example, a crate can disable trace level logs in debug builds and trace, debug, and info
285 //! level logs in release builds with the following configuration:
286 //!
287 //! ```toml
288 //! [dependencies]
289 //! log = { version = "0.4", features = ["max_level_debug", "release_max_level_warn"] }
290 //! ```
291 //! # Crate Feature Flags
292 //!
293 //! The following crate feature flags are available in addition to the filters. They are
294 //! configured in your `Cargo.toml`.
295 //!
296 //! * `std` allows use of `std` crate instead of the default `core`. Enables using `std::error` and
297 //! `set_boxed_logger` functionality.
298 //! * `serde` enables support for serialization and deserialization of `Level` and `LevelFilter`.
299 //!
300 //! ```toml
301 //! [dependencies]
302 //! log = { version = "0.4", features = ["std", "serde"] }
303 //! ```
304 //!
305 //! # Version compatibility
306 //!
307 //! The 0.3 and 0.4 versions of the `log` crate are almost entirely compatible. Log messages
308 //! made using `log` 0.3 will forward transparently to a logger implementation using `log` 0.4. Log
309 //! messages made using `log` 0.4 will forward to a logger implementation using `log` 0.3, but the
310 //! module path and file name information associated with the message will unfortunately be lost.
311 //!
312 //! [`Log`]: trait.Log.html
313 //! [level_link]: enum.Level.html
314 //! [filter_link]: enum.LevelFilter.html
315 //! [`set_logger`]: fn.set_logger.html
316 //! [`set_max_level`]: fn.set_max_level.html
317 //! [`try_set_logger_raw`]: fn.try_set_logger_raw.html
318 //! [`shutdown_logger_raw`]: fn.shutdown_logger_raw.html
319 //! [env_logger]: https://docs.rs/env_logger/*/env_logger/
320 //! [colog]: https://docs.rs/colog/*/colog/
321 //! [simple_logger]: https://github.com/borntyping/rust-simple_logger
322 //! [simplelog]: https://github.com/drakulix/simplelog.rs
323 //! [pretty_env_logger]: https://docs.rs/pretty_env_logger/*/pretty_env_logger/
324 //! [stderrlog]: https://docs.rs/stderrlog/*/stderrlog/
325 //! [flexi_logger]: https://docs.rs/flexi_logger/*/flexi_logger/
326 //! [call_logger]: https://docs.rs/call_logger/*/call_logger/
327 //! [std-logger]: https://docs.rs/std-logger/*/std_logger/
328 //! [syslog]: https://docs.rs/syslog/*/syslog/
329 //! [slog-stdlog]: https://docs.rs/slog-stdlog/*/slog_stdlog/
330 //! [log4rs]: https://docs.rs/log4rs/*/log4rs/
331 //! [logforth]: https://docs.rs/logforth/*/logforth/
332 //! [fern]: https://docs.rs/fern/*/fern/
333 //! [spdlog-rs]: https://docs.rs/spdlog-rs/*/spdlog/
334 //! [systemd-journal-logger]: https://docs.rs/systemd-journal-logger/*/systemd_journal_logger/
335 //! [android_log]: https://docs.rs/android_log/*/android_log/
336 //! [win_dbg_logger]: https://docs.rs/win_dbg_logger/*/win_dbg_logger/
337 //! [db_logger]: https://docs.rs/db_logger/*/db_logger/
338 //! [log-to-defmt]: https://docs.rs/log-to-defmt/*/log_to_defmt/
339 //! [console_log]: https://docs.rs/console_log/*/console_log/
340 //! [structured-logger]: https://docs.rs/structured-logger/latest/structured_logger/
341 //! [logcontrol-log]: https://docs.rs/logcontrol-log/*/logcontrol_log/
342 //! [log_err]: https://docs.rs/log_err/*/log_err/
343 //! [log-reload]: https://docs.rs/log-reload/*/log_reload/
344 //! [alterable_logger]: https://docs.rs/alterable_logger/*/alterable_logger
345 //! [clang_log]: https://docs.rs/clang_log/latest/clang_log
346 //! [ftail]: https://docs.rs/ftail/latest/ftail
347
348 #![doc(
349 html_logo_url = "https://prev.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
350 html_favicon_url = "https://prev.rust-lang.org/favicon.ico",
351 html_root_url = "https://docs.rs/log/0.4.29"
352 )]
353 #![warn(missing_docs)]
354 #![deny(missing_debug_implementations, unconditional_recursion)]
355 #![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
356
357 #[cfg(any(
358 all(feature = "max_level_off", feature = "max_level_error"),
359 all(feature = "max_level_off", feature = "max_level_warn"),
360 all(feature = "max_level_off", feature = "max_level_info"),
361 all(feature = "max_level_off", feature = "max_level_debug"),
362 all(feature = "max_level_off", feature = "max_level_trace"),
363 all(feature = "max_level_error", feature = "max_level_warn"),
364 all(feature = "max_level_error", feature = "max_level_info"),
365 all(feature = "max_level_error", feature = "max_level_debug"),
366 all(feature = "max_level_error", feature = "max_level_trace"),
367 all(feature = "max_level_warn", feature = "max_level_info"),
368 all(feature = "max_level_warn", feature = "max_level_debug"),
369 all(feature = "max_level_warn", feature = "max_level_trace"),
370 all(feature = "max_level_info", feature = "max_level_debug"),
371 all(feature = "max_level_info", feature = "max_level_trace"),
372 all(feature = "max_level_debug", feature = "max_level_trace"),
373 ))]
374 compile_error!("multiple max_level_* features set");
375
376 #[rustfmt::skip]
377 #[cfg(any(
378 all(feature = "release_max_level_off", feature = "release_max_level_error"),
379 all(feature = "release_max_level_off", feature = "release_max_level_warn"),
380 all(feature = "release_max_level_off", feature = "release_max_level_info"),
381 all(feature = "release_max_level_off", feature = "release_max_level_debug"),
382 all(feature = "release_max_level_off", feature = "release_max_level_trace"),
383 all(feature = "release_max_level_error", feature = "release_max_level_warn"),
384 all(feature = "release_max_level_error", feature = "release_max_level_info"),
385 all(feature = "release_max_level_error", feature = "release_max_level_debug"),
386 all(feature = "release_max_level_error", feature = "release_max_level_trace"),
387 all(feature = "release_max_level_warn", feature = "release_max_level_info"),
388 all(feature = "release_max_level_warn", feature = "release_max_level_debug"),
389 all(feature = "release_max_level_warn", feature = "release_max_level_trace"),
390 all(feature = "release_max_level_info", feature = "release_max_level_debug"),
391 all(feature = "release_max_level_info", feature = "release_max_level_trace"),
392 all(feature = "release_max_level_debug", feature = "release_max_level_trace"),
393 ))]
394 compile_error!("multiple release_max_level_* features set");
395
396 #[cfg(all(not(feature = "std"), not(test)))]
397 extern crate core as std;
398
399 use std::cfg;
400 #[cfg(feature = "std")]
401 use std::error;
402 use std::str::FromStr;
403 use std::{cmp, fmt, mem};
404
405 #[macro_use]
406 mod macros;
407 mod serde;
408
409 #[cfg(feature = "kv")]
410 pub mod kv;
411
412 #[cfg(target_has_atomic = "ptr")]
413 use std::sync::atomic::{AtomicUsize, Ordering};
414
415 #[cfg(not(target_has_atomic = "ptr"))]
416 use std::cell::Cell;
417 #[cfg(not(target_has_atomic = "ptr"))]
418 use std::sync::atomic::Ordering;
419
420 #[cfg(not(target_has_atomic = "ptr"))]
421 struct AtomicUsize {
422 v: Cell<usize>,
423 }
424
425 #[cfg(not(target_has_atomic = "ptr"))]
426 impl AtomicUsize {
427 const fn new(v: usize) -> AtomicUsize {
428 AtomicUsize { v: Cell::new(v) }
429 }
430
431 fn load(&self, _order: Ordering) -> usize {
432 self.v.get()
433 }
434
435 fn store(&self, val: usize, _order: Ordering) {
436 self.v.set(val)
437 }
438 }
439
440 // Any platform without atomics is unlikely to have multiple cores, so
441 // writing via Cell will not be a race condition.
442 #[cfg(not(target_has_atomic = "ptr"))]
443 unsafe impl Sync for AtomicUsize {}
444
445 // The LOGGER static holds a pointer to the global logger. It is protected by
446 // the STATE static which determines whether LOGGER has been initialized yet.
447 static mut LOGGER: &dyn Log = &NopLogger;
448
449 static STATE: AtomicUsize = AtomicUsize::new(0);
450
451 // There are three different states that we care about: the logger's
452 // uninitialized, the logger's initializing (set_logger's been called but
453 // LOGGER hasn't actually been set yet), or the logger's active.
454 const UNINITIALIZED: usize = 0;
455 const INITIALIZING: usize = 1;
456 const INITIALIZED: usize = 2;
457
458 static MAX_LOG_LEVEL_FILTER: AtomicUsize = AtomicUsize::new(0);
459
460 static LOG_LEVEL_NAMES: [&str; 6] = ["OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"];
461
462 static SET_LOGGER_ERROR: &str = "attempted to set a logger after the logging system \
463 was already initialized";
464 static LEVEL_PARSE_ERROR: &str =
465 "attempted to convert a string that doesn't match an existing log level";
466
467 /// An enum representing the available verbosity levels of the logger.
468 ///
469 /// Typical usage includes: checking if a certain `Level` is enabled with
470 /// [`log_enabled!`](macro.log_enabled.html), specifying the `Level` of
471 /// [`log!`](macro.log.html), and comparing a `Level` directly to a
472 /// [`LevelFilter`](enum.LevelFilter.html).
473 #[repr(usize)]
474 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
475 pub enum Level {
476 /// The "error" level.
477 ///
478 /// Designates very serious errors.
479 // This way these line up with the discriminants for LevelFilter below
480 // This works because Rust treats field-less enums the same way as C does:
481 // https://doc.rust-lang.org/reference/items/enumerations.html#custom-discriminant-values-for-field-less-enumerations
482 Error = 1,
483 /// The "warn" level.
484 ///
485 /// Designates hazardous situations.
486 Warn,
487 /// The "info" level.
488 ///
489 /// Designates useful information.
490 Info,
491 /// The "debug" level.
492 ///
493 /// Designates lower priority information.
494 Debug,
495 /// The "trace" level.
496 ///
497 /// Designates very low priority, often extremely verbose, information.
498 Trace,
499 }
500
501 impl PartialEq<LevelFilter> for Level {
502 #[inline]
503 fn eq(&self, other: &LevelFilter) -> bool {
504 *self as usize == *other as usize
505 }
506 }
507
508 impl PartialOrd<LevelFilter> for Level {
509 #[inline]
510 fn partial_cmp(&self, other: &LevelFilter) -> Option<cmp::Ordering> {
511 Some((*self as usize).cmp(&(*other as usize)))
512 }
513 }
514
515 impl FromStr for Level {
516 type Err = ParseLevelError;
517 fn from_str(level: &str) -> Result<Level, Self::Err> {
518 // iterate from 1, excluding "OFF"
519 for idx in 1..LOG_LEVEL_NAMES.len() {
520 if LOG_LEVEL_NAMES[idx].eq_ignore_ascii_case(level) {
521 return Ok(Level::from_usize(idx).unwrap());
522 }
523 }
524 Err(ParseLevelError(()))
525 }
526 }
527
528 impl fmt::Display for Level {
529 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
530 fmt.pad(self.as_str())
531 }
532 }
533
534 impl Level {
535 fn from_usize(u: usize) -> Option<Level> {
536 match u {
537 1 => Some(Level::Error),
538 2 => Some(Level::Warn),
539 3 => Some(Level::Info),
540 4 => Some(Level::Debug),
541 5 => Some(Level::Trace),
542 _ => None,
543 }
544 }
545
546 /// Returns the most verbose logging level.
547 #[inline]
548 pub fn max() -> Level {
549 Level::Trace
550 }
551
552 /// Converts the `Level` to the equivalent `LevelFilter`.
553 #[inline]
554 pub fn to_level_filter(&self) -> LevelFilter {
555 LevelFilter::from_usize(*self as usize).unwrap()
556 }
557
558 /// Returns the string representation of the `Level`.
559 ///
560 /// This returns the same string as the `fmt::Display` implementation.
561 pub fn as_str(&self) -> &'static str {
562 LOG_LEVEL_NAMES[*self as usize]
563 }
564
565 /// Iterate through all supported logging levels.
566 ///
567 /// The order of iteration is from more severe to less severe log messages.
568 ///
569 /// # Examples
570 ///
571 /// ```
572 /// use log::Level;
573 ///
574 /// let mut levels = Level::iter();
575 ///
576 /// assert_eq!(Some(Level::Error), levels.next());
577 /// assert_eq!(Some(Level::Trace), levels.last());
578 /// ```
579 pub fn iter() -> impl Iterator<Item = Self> {
580 (1..6).map(|i| Self::from_usize(i).unwrap())
581 }
582
583 /// Get the next-highest `Level` from this one.
584 ///
585 /// If the current `Level` is at the highest level, the returned `Level` will be the same as the
586 /// current one.
587 ///
588 /// # Examples
589 ///
590 /// ```
591 /// use log::Level;
592 ///
593 /// let level = Level::Info;
594 ///
595 /// assert_eq!(Level::Debug, level.increment_severity());
596 /// assert_eq!(Level::Trace, level.increment_severity().increment_severity());
597 /// assert_eq!(Level::Trace, level.increment_severity().increment_severity().increment_severity()); // max level
598 /// ```
599 pub fn increment_severity(&self) -> Self {
600 let current = *self as usize;
601 Self::from_usize(current + 1).unwrap_or(*self)
602 }
603
604 /// Get the next-lowest `Level` from this one.
605 ///
606 /// If the current `Level` is at the lowest level, the returned `Level` will be the same as the
607 /// current one.
608 ///
609 /// # Examples
610 ///
611 /// ```
612 /// use log::Level;
613 ///
614 /// let level = Level::Info;
615 ///
616 /// assert_eq!(Level::Warn, level.decrement_severity());
617 /// assert_eq!(Level::Error, level.decrement_severity().decrement_severity());
618 /// assert_eq!(Level::Error, level.decrement_severity().decrement_severity().decrement_severity()); // min level
619 /// ```
620 pub fn decrement_severity(&self) -> Self {
621 let current = *self as usize;
622 Self::from_usize(current.saturating_sub(1)).unwrap_or(*self)
623 }
624 }
625
626 /// An enum representing the available verbosity level filters of the logger.
627 ///
628 /// A `LevelFilter` may be compared directly to a [`Level`]. Use this type
629 /// to get and set the maximum log level with [`max_level()`] and [`set_max_level`].
630 ///
631 /// [`Level`]: enum.Level.html
632 /// [`max_level()`]: fn.max_level.html
633 /// [`set_max_level`]: fn.set_max_level.html
634 #[repr(usize)]
635 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
636 pub enum LevelFilter {
637 /// A level lower than all log levels.
638 Off,
639 /// Corresponds to the `Error` log level.
640 Error,
641 /// Corresponds to the `Warn` log level.
642 Warn,
643 /// Corresponds to the `Info` log level.
644 Info,
645 /// Corresponds to the `Debug` log level.
646 Debug,
647 /// Corresponds to the `Trace` log level.
648 Trace,
649 }
650
651 impl PartialEq<Level> for LevelFilter {
652 #[inline]
653 fn eq(&self, other: &Level) -> bool {
654 other.eq(self)
655 }
656 }
657
658 impl PartialOrd<Level> for LevelFilter {
659 #[inline]
660 fn partial_cmp(&self, other: &Level) -> Option<cmp::Ordering> {
661 Some((*self as usize).cmp(&(*other as usize)))
662 }
663 }
664
665 impl FromStr for LevelFilter {
666 type Err = ParseLevelError;
667 fn from_str(level: &str) -> Result<LevelFilter, Self::Err> {
668 // iterate from 0, including "OFF"
669 for idx in 0..LOG_LEVEL_NAMES.len() {
670 if LOG_LEVEL_NAMES[idx].eq_ignore_ascii_case(level) {
671 return Ok(LevelFilter::from_usize(idx).unwrap());
672 }
673 }
674 Err(ParseLevelError(()))
675 }
676 }
677
678 impl fmt::Display for LevelFilter {
679 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
680 fmt.pad(self.as_str())
681 }
682 }
683
684 impl LevelFilter {
685 fn from_usize(u: usize) -> Option<LevelFilter> {
686 match u {
687 0 => Some(LevelFilter::Off),
688 1 => Some(LevelFilter::Error),
689 2 => Some(LevelFilter::Warn),
690 3 => Some(LevelFilter::Info),
691 4 => Some(LevelFilter::Debug),
692 5 => Some(LevelFilter::Trace),
693 _ => None,
694 }
695 }
696
697 /// Returns the most verbose logging level filter.
698 #[inline]
699 pub fn max() -> LevelFilter {
700 LevelFilter::Trace
701 }
702
703 /// Converts `self` to the equivalent `Level`.
704 ///
705 /// Returns `None` if `self` is `LevelFilter::Off`.
706 #[inline]
707 pub fn to_level(&self) -> Option<Level> {
708 Level::from_usize(*self as usize)
709 }
710
711 /// Returns the string representation of the `LevelFilter`.
712 ///
713 /// This returns the same string as the `fmt::Display` implementation.
714 pub fn as_str(&self) -> &'static str {
715 LOG_LEVEL_NAMES[*self as usize]
716 }
717
718 /// Iterate through all supported filtering levels.
719 ///
720 /// The order of iteration is from less to more verbose filtering.
721 ///
722 /// # Examples
723 ///
724 /// ```
725 /// use log::LevelFilter;
726 ///
727 /// let mut levels = LevelFilter::iter();
728 ///
729 /// assert_eq!(Some(LevelFilter::Off), levels.next());
730 /// assert_eq!(Some(LevelFilter::Trace), levels.last());
731 /// ```
732 pub fn iter() -> impl Iterator<Item = Self> {
733 (0..6).map(|i| Self::from_usize(i).unwrap())
734 }
735
736 /// Get the next-highest `LevelFilter` from this one.
737 ///
738 /// If the current `LevelFilter` is at the highest level, the returned `LevelFilter` will be the
739 /// same as the current one.
740 ///
741 /// # Examples
742 ///
743 /// ```
744 /// use log::LevelFilter;
745 ///
746 /// let level_filter = LevelFilter::Info;
747 ///
748 /// assert_eq!(LevelFilter::Debug, level_filter.increment_severity());
749 /// assert_eq!(LevelFilter::Trace, level_filter.increment_severity().increment_severity());
750 /// assert_eq!(LevelFilter::Trace, level_filter.increment_severity().increment_severity().increment_severity()); // max level
751 /// ```
752 pub fn increment_severity(&self) -> Self {
753 let current = *self as usize;
754 Self::from_usize(current + 1).unwrap_or(*self)
755 }
756
757 /// Get the next-lowest `LevelFilter` from this one.
758 ///
759 /// If the current `LevelFilter` is at the lowest level, the returned `LevelFilter` will be the
760 /// same as the current one.
761 ///
762 /// # Examples
763 ///
764 /// ```
765 /// use log::LevelFilter;
766 ///
767 /// let level_filter = LevelFilter::Info;
768 ///
769 /// assert_eq!(LevelFilter::Warn, level_filter.decrement_severity());
770 /// assert_eq!(LevelFilter::Error, level_filter.decrement_severity().decrement_severity());
771 /// assert_eq!(LevelFilter::Off, level_filter.decrement_severity().decrement_severity().decrement_severity());
772 /// assert_eq!(LevelFilter::Off, level_filter.decrement_severity().decrement_severity().decrement_severity().decrement_severity()); // min level
773 /// ```
774 pub fn decrement_severity(&self) -> Self {
775 let current = *self as usize;
776 Self::from_usize(current.saturating_sub(1)).unwrap_or(*self)
777 }
778 }
779
780 #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
781 enum MaybeStaticStr<'a> {
782 Static(&'static str),
783 Borrowed(&'a str),
784 }
785
786 impl<'a> MaybeStaticStr<'a> {
787 #[inline]
788 fn get(&self) -> &'a str {
789 match *self {
790 MaybeStaticStr::Static(s) => s,
791 MaybeStaticStr::Borrowed(s) => s,
792 }
793 }
794 }
795
796 /// The "payload" of a log message.
797 ///
798 /// # Use
799 ///
800 /// `Record` structures are passed as parameters to the [`log`][method.log]
801 /// method of the [`Log`] trait. Logger implementors manipulate these
802 /// structures in order to display log messages. `Record`s are automatically
803 /// created by the [`log!`] macro and so are not seen by log users.
804 ///
805 /// Note that the [`level()`] and [`target()`] accessors are equivalent to
806 /// `self.metadata().level()` and `self.metadata().target()` respectively.
807 /// These methods are provided as a convenience for users of this structure.
808 ///
809 /// # Example
810 ///
811 /// The following example shows a simple logger that displays the level,
812 /// module path, and message of any `Record` that is passed to it.
813 ///
814 /// ```
815 /// struct SimpleLogger;
816 ///
817 /// impl log::Log for SimpleLogger {
818 /// fn enabled(&self, _metadata: &log::Metadata) -> bool {
819 /// true
820 /// }
821 ///
822 /// fn log(&self, record: &log::Record) {
823 /// if !self.enabled(record.metadata()) {
824 /// return;
825 /// }
826 ///
827 /// println!("{}:{} -- {}",
828 /// record.level(),
829 /// record.target(),
830 /// record.args());
831 /// }
832 /// fn flush(&self) {}
833 /// }
834 /// ```
835 ///
836 /// [method.log]: trait.Log.html#tymethod.log
837 /// [`Log`]: trait.Log.html
838 /// [`log!`]: macro.log.html
839 /// [`level()`]: struct.Record.html#method.level
840 /// [`target()`]: struct.Record.html#method.target
841 #[derive(Clone, Debug)]
842 pub struct Record<'a> {
843 metadata: Metadata<'a>,
844 args: fmt::Arguments<'a>,
845 module_path: Option<MaybeStaticStr<'a>>,
846 file: Option<MaybeStaticStr<'a>>,
847 line: Option<u32>,
848 #[cfg(feature = "kv")]
849 key_values: KeyValues<'a>,
850 }
851
852 // This wrapper type is only needed so we can
853 // `#[derive(Debug)]` on `Record`. It also
854 // provides a useful `Debug` implementation for
855 // the underlying `Source`.
856 #[cfg(feature = "kv")]
857 #[derive(Clone)]
858 struct KeyValues<'a>(&'a dyn kv::Source);
859
860 #[cfg(feature = "kv")]
861 impl<'a> fmt::Debug for KeyValues<'a> {
862 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
863 let mut visitor = f.debug_map();
864 self.0.visit(&mut visitor).map_err(|_| fmt::Error)?;
865 visitor.finish()
866 }
867 }
868
869 impl<'a> Record<'a> {
870 /// Returns a new builder.
871 #[inline]
872 pub fn builder() -> RecordBuilder<'a> {
873 RecordBuilder::new()
874 }
875
876 /// The message body.
877 #[inline]
878 pub fn args(&self) -> &fmt::Arguments<'a> {
879 &self.args
880 }
881
882 /// Metadata about the log directive.
883 #[inline]
884 pub fn metadata(&self) -> &Metadata<'a> {
885 &self.metadata
886 }
887
888 /// The verbosity level of the message.
889 #[inline]
890 pub fn level(&self) -> Level {
891 self.metadata.level()
892 }
893
894 /// The name of the target of the directive.
895 #[inline]
896 pub fn target(&self) -> &'a str {
897 self.metadata.target()
898 }
899
900 /// The module path of the message.
901 #[inline]
902 pub fn module_path(&self) -> Option<&'a str> {
903 self.module_path.map(|s| s.get())
904 }
905
906 /// The module path of the message, if it is a `'static` string.
907 #[inline]
908 pub fn module_path_static(&self) -> Option<&'static str> {
909 match self.module_path {
910 Some(MaybeStaticStr::Static(s)) => Some(s),
911 _ => None,
912 }
913 }
914
915 /// The source file containing the message.
916 #[inline]
917 pub fn file(&self) -> Option<&'a str> {
918 self.file.map(|s| s.get())
919 }
920
921 /// The source file containing the message, if it is a `'static` string.
922 #[inline]
923 pub fn file_static(&self) -> Option<&'static str> {
924 match self.file {
925 Some(MaybeStaticStr::Static(s)) => Some(s),
926 _ => None,
927 }
928 }
929
930 /// The line containing the message.
931 #[inline]
932 pub fn line(&self) -> Option<u32> {
933 self.line
934 }
935
936 /// The structured key-value pairs associated with the message.
937 #[cfg(feature = "kv")]
938 #[inline]
939 pub fn key_values(&self) -> &dyn kv::Source {
940 self.key_values.0
941 }
942
943 /// Create a new [`RecordBuilder`](struct.RecordBuilder.html) based on this record.
944 #[cfg(feature = "kv")]
945 #[inline]
946 pub fn to_builder(&self) -> RecordBuilder<'_> {
947 RecordBuilder {
948 record: Record {
949 metadata: Metadata {
950 level: self.metadata.level,
951 target: self.metadata.target,
952 },
953 args: self.args,
954 module_path: self.module_path,
955 file: self.file,
956 line: self.line,
957 key_values: self.key_values.clone(),
958 },
959 }
960 }
961 }
962
963 /// Builder for [`Record`](struct.Record.html).
964 ///
965 /// Typically should only be used by log library creators or for testing and "shim loggers".
966 /// The `RecordBuilder` can set the different parameters of `Record` object, and returns
967 /// the created object when `build` is called.
968 ///
969 /// # Examples
970 ///
971 /// ```
972 /// use log::{Level, Record};
973 ///
974 /// let record = Record::builder()
975 /// .args(format_args!("Error!"))
976 /// .level(Level::Error)
977 /// .target("myApp")
978 /// .file(Some("server.rs"))
979 /// .line(Some(144))
980 /// .module_path(Some("server"))
981 /// .build();
982 /// ```
983 ///
984 /// Alternatively, use [`MetadataBuilder`](struct.MetadataBuilder.html):
985 ///
986 /// ```
987 /// use log::{Record, Level, MetadataBuilder};
988 ///
989 /// let error_metadata = MetadataBuilder::new()
990 /// .target("myApp")
991 /// .level(Level::Error)
992 /// .build();
993 ///
994 /// let record = Record::builder()
995 /// .metadata(error_metadata)
996 /// .args(format_args!("Error!"))
997 /// .line(Some(433))
998 /// .file(Some("app.rs"))
999 /// .module_path(Some("server"))
1000 /// .build();
1001 /// ```
1002 #[derive(Debug)]
1003 pub struct RecordBuilder<'a> {
1004 record: Record<'a>,
1005 }
1006
1007 impl<'a> RecordBuilder<'a> {
1008 /// Construct new `RecordBuilder`.
1009 ///
1010 /// The default options are:
1011 ///
1012 /// - `args`: [`format_args!("")`]
1013 /// - `metadata`: [`Metadata::builder().build()`]
1014 /// - `module_path`: `None`
1015 /// - `file`: `None`
1016 /// - `line`: `None`
1017 ///
1018 /// [`format_args!("")`]: https://doc.rust-lang.org/std/macro.format_args.html
1019 /// [`Metadata::builder().build()`]: struct.MetadataBuilder.html#method.build
1020 #[inline]
1021 pub fn new() -> RecordBuilder<'a> {
1022 RecordBuilder {
1023 record: Record {
1024 args: format_args!(""),
1025 metadata: Metadata::builder().build(),
1026 module_path: None,
1027 file: None,
1028 line: None,
1029 #[cfg(feature = "kv")]
1030 key_values: KeyValues(&None::<(kv::Key, kv::Value)>),
1031 },
1032 }
1033 }
1034
1035 /// Set [`args`](struct.Record.html#method.args).
1036 #[inline]
1037 pub fn args(&mut self, args: fmt::Arguments<'a>) -> &mut RecordBuilder<'a> {
1038 self.record.args = args;
1039 self
1040 }
1041
1042 /// Set [`metadata`](struct.Record.html#method.metadata). Construct a `Metadata` object with [`MetadataBuilder`](struct.MetadataBuilder.html).
1043 #[inline]
1044 pub fn metadata(&mut self, metadata: Metadata<'a>) -> &mut RecordBuilder<'a> {
1045 self.record.metadata = metadata;
1046 self
1047 }
1048
1049 /// Set [`Metadata::level`](struct.Metadata.html#method.level).
1050 #[inline]
1051 pub fn level(&mut self, level: Level) -> &mut RecordBuilder<'a> {
1052 self.record.metadata.level = level;
1053 self
1054 }
1055
1056 /// Set [`Metadata::target`](struct.Metadata.html#method.target)
1057 #[inline]
1058 pub fn target(&mut self, target: &'a str) -> &mut RecordBuilder<'a> {
1059 self.record.metadata.target = target;
1060 self
1061 }
1062
1063 /// Set [`module_path`](struct.Record.html#method.module_path)
1064 #[inline]
1065 pub fn module_path(&mut self, path: Option<&'a str>) -> &mut RecordBuilder<'a> {
1066 self.record.module_path = path.map(MaybeStaticStr::Borrowed);
1067 self
1068 }
1069
1070 /// Set [`module_path`](struct.Record.html#method.module_path) to a `'static` string
1071 #[inline]
1072 pub fn module_path_static(&mut self, path: Option<&'static str>) -> &mut RecordBuilder<'a> {
1073 self.record.module_path = path.map(MaybeStaticStr::Static);
1074 self
1075 }
1076
1077 /// Set [`file`](struct.Record.html#method.file)
1078 #[inline]
1079 pub fn file(&mut self, file: Option<&'a str>) -> &mut RecordBuilder<'a> {
1080 self.record.file = file.map(MaybeStaticStr::Borrowed);
1081 self
1082 }
1083
1084 /// Set [`file`](struct.Record.html#method.file) to a `'static` string.
1085 #[inline]
1086 pub fn file_static(&mut self, file: Option<&'static str>) -> &mut RecordBuilder<'a> {
1087 self.record.file = file.map(MaybeStaticStr::Static);
1088 self
1089 }
1090
1091 /// Set [`line`](struct.Record.html#method.line)
1092 #[inline]
1093 pub fn line(&mut self, line: Option<u32>) -> &mut RecordBuilder<'a> {
1094 self.record.line = line;
1095 self
1096 }
1097
1098 /// Set [`key_values`](struct.Record.html#method.key_values)
1099 #[cfg(feature = "kv")]
1100 #[inline]
1101 pub fn key_values(&mut self, kvs: &'a dyn kv::Source) -> &mut RecordBuilder<'a> {
1102 self.record.key_values = KeyValues(kvs);
1103 self
1104 }
1105
1106 /// Invoke the builder and return a `Record`
1107 #[inline]
1108 pub fn build(&self) -> Record<'a> {
1109 self.record.clone()
1110 }
1111 }
1112
1113 impl Default for RecordBuilder<'_> {
1114 fn default() -> Self {
1115 Self::new()
1116 }
1117 }
1118
1119 /// Metadata about a log message.
1120 ///
1121 /// # Use
1122 ///
1123 /// `Metadata` structs are created when users of the library use
1124 /// logging macros.
1125 ///
1126 /// They are consumed by implementations of the `Log` trait in the
1127 /// `enabled` method.
1128 ///
1129 /// `Record`s use `Metadata` to determine the log message's severity
1130 /// and target.
1131 ///
1132 /// Users should use the `log_enabled!` macro in their code to avoid
1133 /// constructing expensive log messages.
1134 ///
1135 /// # Examples
1136 ///
1137 /// ```
1138 /// use log::{Record, Level, Metadata};
1139 ///
1140 /// struct MyLogger;
1141 ///
1142 /// impl log::Log for MyLogger {
1143 /// fn enabled(&self, metadata: &Metadata) -> bool {
1144 /// metadata.level() <= Level::Info
1145 /// }
1146 ///
1147 /// fn log(&self, record: &Record) {
1148 /// if self.enabled(record.metadata()) {
1149 /// println!("{} - {}", record.level(), record.args());
1150 /// }
1151 /// }
1152 /// fn flush(&self) {}
1153 /// }
1154 ///
1155 /// # fn main(){}
1156 /// ```
1157 #[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
1158 pub struct Metadata<'a> {
1159 level: Level,
1160 target: &'a str,
1161 }
1162
1163 impl<'a> Metadata<'a> {
1164 /// Returns a new builder.
1165 #[inline]
1166 pub fn builder() -> MetadataBuilder<'a> {
1167 MetadataBuilder::new()
1168 }
1169
1170 /// The verbosity level of the message.
1171 #[inline]
1172 pub fn level(&self) -> Level {
1173 self.level
1174 }
1175
1176 /// The name of the target of the directive.
1177 #[inline]
1178 pub fn target(&self) -> &'a str {
1179 self.target
1180 }
1181 }
1182
1183 /// Builder for [`Metadata`](struct.Metadata.html).
1184 ///
1185 /// Typically should only be used by log library creators or for testing and "shim loggers".
1186 /// The `MetadataBuilder` can set the different parameters of a `Metadata` object, and returns
1187 /// the created object when `build` is called.
1188 ///
1189 /// # Example
1190 ///
1191 /// ```
1192 /// let target = "myApp";
1193 /// use log::{Level, MetadataBuilder};
1194 /// let metadata = MetadataBuilder::new()
1195 /// .level(Level::Debug)
1196 /// .target(target)
1197 /// .build();
1198 /// ```
1199 #[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
1200 pub struct MetadataBuilder<'a> {
1201 metadata: Metadata<'a>,
1202 }
1203
1204 impl<'a> MetadataBuilder<'a> {
1205 /// Construct a new `MetadataBuilder`.
1206 ///
1207 /// The default options are:
1208 ///
1209 /// - `level`: `Level::Info`
1210 /// - `target`: `""`
1211 #[inline]
1212 pub fn new() -> MetadataBuilder<'a> {
1213 MetadataBuilder {
1214 metadata: Metadata {
1215 level: Level::Info,
1216 target: "",
1217 },
1218 }
1219 }
1220
1221 /// Setter for [`level`](struct.Metadata.html#method.level).
1222 #[inline]
1223 pub fn level(&mut self, arg: Level) -> &mut MetadataBuilder<'a> {
1224 self.metadata.level = arg;
1225 self
1226 }
1227
1228 /// Setter for [`target`](struct.Metadata.html#method.target).
1229 #[inline]
1230 pub fn target(&mut self, target: &'a str) -> &mut MetadataBuilder<'a> {
1231 self.metadata.target = target;
1232 self
1233 }
1234
1235 /// Returns a `Metadata` object.
1236 #[inline]
1237 pub fn build(&self) -> Metadata<'a> {
1238 self.metadata.clone()
1239 }
1240 }
1241
1242 impl Default for MetadataBuilder<'_> {
1243 fn default() -> Self {
1244 Self::new()
1245 }
1246 }
1247
1248 /// A trait encapsulating the operations required of a logger.
1249 pub trait Log: Sync + Send {
1250 /// Determines if a log message with the specified metadata would be
1251 /// logged.
1252 ///
1253 /// This is used by the `log_enabled!` macro to allow callers to avoid
1254 /// expensive computation of log message arguments if the message would be
1255 /// discarded anyway.
1256 ///
1257 /// # For implementors
1258 ///
1259 /// This method isn't called automatically by the `log!` macros.
1260 /// It's up to an implementation of the `Log` trait to call `enabled` in its own
1261 /// `log` method implementation to guarantee that filtering is applied.
1262 fn enabled(&self, metadata: &Metadata) -> bool;
1263
1264 /// Logs the `Record`.
1265 ///
1266 /// # For implementors
1267 ///
1268 /// Note that `enabled` is *not* necessarily called before this method.
1269 /// Implementations of `log` should perform all necessary filtering
1270 /// internally.
1271 fn log(&self, record: &Record);
1272
1273 /// Flushes any buffered records.
1274 ///
1275 /// # For implementors
1276 ///
1277 /// This method isn't called automatically by the `log!` macros.
1278 /// It can be called manually on shut-down to ensure any in-flight records are flushed.
1279 fn flush(&self);
1280 }
1281
1282 /// A dummy initial value for LOGGER.
1283 struct NopLogger;
1284
1285 impl Log for NopLogger {
1286 fn enabled(&self, _: &Metadata) -> bool {
1287 false
1288 }
1289
1290 fn log(&self, _: &Record) {}
1291 fn flush(&self) {}
1292 }
1293
1294 impl<T> Log for &'_ T
1295 where
1296 T: ?Sized + Log,
1297 {
1298 fn enabled(&self, metadata: &Metadata) -> bool {
1299 (**self).enabled(metadata)
1300 }
1301
1302 fn log(&self, record: &Record) {
1303 (**self).log(record);
1304 }
1305 fn flush(&self) {
1306 (**self).flush();
1307 }
1308 }
1309
1310 #[cfg(feature = "std")]
1311 impl<T> Log for std::boxed::Box<T>
1312 where
1313 T: ?Sized + Log,
1314 {
1315 fn enabled(&self, metadata: &Metadata) -> bool {
1316 self.as_ref().enabled(metadata)
1317 }
1318
1319 fn log(&self, record: &Record) {
1320 self.as_ref().log(record);
1321 }
1322 fn flush(&self) {
1323 self.as_ref().flush();
1324 }
1325 }
1326
1327 #[cfg(feature = "std")]
1328 impl<T> Log for std::sync::Arc<T>
1329 where
1330 T: ?Sized + Log,
1331 {
1332 fn enabled(&self, metadata: &Metadata) -> bool {
1333 self.as_ref().enabled(metadata)
1334 }
1335
1336 fn log(&self, record: &Record) {
1337 self.as_ref().log(record);
1338 }
1339 fn flush(&self) {
1340 self.as_ref().flush();
1341 }
1342 }
1343
1344 /// Sets the global maximum log level.
1345 ///
1346 /// Generally, this should only be called by the active logging implementation.
1347 ///
1348 /// Note that `Trace` is the maximum level, because it provides the maximum amount of detail in the emitted logs.
1349 #[inline]
1350 #[cfg(target_has_atomic = "ptr")]
1351 pub fn set_max_level(level: LevelFilter) {
1352 MAX_LOG_LEVEL_FILTER.store(level as usize, Ordering::Relaxed);
1353 }
1354
1355 /// A thread-unsafe version of [`set_max_level`].
1356 ///
1357 /// This function is available on all platforms, even those that do not have
1358 /// support for atomics that is needed by [`set_max_level`].
1359 ///
1360 /// In almost all cases, [`set_max_level`] should be preferred.
1361 ///
1362 /// # Safety
1363 ///
1364 /// This function is only safe to call when it cannot race with any other
1365 /// calls to `set_max_level` or `set_max_level_racy`.
1366 ///
1367 /// This can be upheld by (for example) making sure that **there are no other
1368 /// threads**, and (on embedded) that **interrupts are disabled**.
1369 ///
1370 /// It is safe to use all other logging functions while this function runs
1371 /// (including all logging macros).
1372 ///
1373 /// [`set_max_level`]: fn.set_max_level.html
1374 #[inline]
1375 pub unsafe fn set_max_level_racy(level: LevelFilter) {
1376 // `MAX_LOG_LEVEL_FILTER` uses a `Cell` as the underlying primitive when a
1377 // platform doesn't support `target_has_atomic = "ptr"`, so even though this looks the same
1378 // as `set_max_level` it may have different safety properties.
1379 MAX_LOG_LEVEL_FILTER.store(level as usize, Ordering::Relaxed);
1380 }
1381
1382 /// Returns the current maximum log level.
1383 ///
1384 /// The [`log!`], [`error!`], [`warn!`], [`info!`], [`debug!`], and [`trace!`] macros check
1385 /// this value and discard any message logged at a higher level. The maximum
1386 /// log level is set by the [`set_max_level`] function.
1387 ///
1388 /// [`log!`]: macro.log.html
1389 /// [`error!`]: macro.error.html
1390 /// [`warn!`]: macro.warn.html
1391 /// [`info!`]: macro.info.html
1392 /// [`debug!`]: macro.debug.html
1393 /// [`trace!`]: macro.trace.html
1394 /// [`set_max_level`]: fn.set_max_level.html
1395 #[inline(always)]
1396 pub fn max_level() -> LevelFilter {
1397 // Since `LevelFilter` is `repr(usize)`,
1398 // this transmute is sound if and only if `MAX_LOG_LEVEL_FILTER`
1399 // is set to a usize that is a valid discriminant for `LevelFilter`.
1400 // Since `MAX_LOG_LEVEL_FILTER` is private, the only time it's set
1401 // is by `set_max_level` above, i.e. by casting a `LevelFilter` to `usize`.
1402 // So any usize stored in `MAX_LOG_LEVEL_FILTER` is a valid discriminant.
1403 unsafe { mem::transmute(MAX_LOG_LEVEL_FILTER.load(Ordering::Relaxed)) }
1404 }
1405
1406 /// Sets the global logger to a `Box<Log>`.
1407 ///
1408 /// This is a simple convenience wrapper over `set_logger`, which takes a
1409 /// `Box<Log>` rather than a `&'static Log`. See the documentation for
1410 /// [`set_logger`] for more details.
1411 ///
1412 /// Requires the `std` feature.
1413 ///
1414 /// # Errors
1415 ///
1416 /// An error is returned if a logger has already been set.
1417 ///
1418 /// [`set_logger`]: fn.set_logger.html
1419 #[cfg(all(feature = "std", target_has_atomic = "ptr"))]
1420 pub fn set_boxed_logger(logger: Box<dyn Log>) -> Result<(), SetLoggerError> {
1421 set_logger_inner(|| Box::leak(logger))
1422 }
1423
1424 /// Sets the global logger to a `&'static Log`.
1425 ///
1426 /// This function may only be called once in the lifetime of a program. Any log
1427 /// events that occur before the call to `set_logger` completes will be ignored.
1428 ///
1429 /// This function does not typically need to be called manually. Logger
1430 /// implementations should provide an initialization method that installs the
1431 /// logger internally.
1432 ///
1433 /// # Availability
1434 ///
1435 /// This method is available even when the `std` feature is disabled. However,
1436 /// it is currently unavailable on `thumbv6` targets, which lack support for
1437 /// some atomic operations which are used by this function. Even on those
1438 /// targets, [`set_logger_racy`] will be available.
1439 ///
1440 /// # Errors
1441 ///
1442 /// An error is returned if a logger has already been set.
1443 ///
1444 /// # Examples
1445 ///
1446 /// ```
1447 /// use log::{error, info, warn, Record, Level, Metadata, LevelFilter};
1448 ///
1449 /// static MY_LOGGER: MyLogger = MyLogger;
1450 ///
1451 /// struct MyLogger;
1452 ///
1453 /// impl log::Log for MyLogger {
1454 /// fn enabled(&self, metadata: &Metadata) -> bool {
1455 /// metadata.level() <= Level::Info
1456 /// }
1457 ///
1458 /// fn log(&self, record: &Record) {
1459 /// if self.enabled(record.metadata()) {
1460 /// println!("{} - {}", record.level(), record.args());
1461 /// }
1462 /// }
1463 /// fn flush(&self) {}
1464 /// }
1465 ///
1466 /// # fn main(){
1467 /// log::set_logger(&MY_LOGGER).unwrap();
1468 /// log::set_max_level(LevelFilter::Info);
1469 ///
1470 /// info!("hello log");
1471 /// warn!("warning");
1472 /// error!("oops");
1473 /// # }
1474 /// ```
1475 ///
1476 /// [`set_logger_racy`]: fn.set_logger_racy.html
1477 #[cfg(target_has_atomic = "ptr")]
1478 pub fn set_logger(logger: &'static dyn Log) -> Result<(), SetLoggerError> {
1479 set_logger_inner(|| logger)
1480 }
1481
1482 #[cfg(target_has_atomic = "ptr")]
1483 fn set_logger_inner<F>(make_logger: F) -> Result<(), SetLoggerError>
1484 where
1485 F: FnOnce() -> &'static dyn Log,
1486 {
1487 match STATE.compare_exchange(
1488 UNINITIALIZED,
1489 INITIALIZING,
1490 Ordering::Acquire,
1491 Ordering::Relaxed,
1492 ) {
1493 Ok(UNINITIALIZED) => {
1494 unsafe {
1495 LOGGER = make_logger();
1496 }
1497 STATE.store(INITIALIZED, Ordering::Release);
1498 Ok(())
1499 }
1500 Err(INITIALIZING) => {
1501 while STATE.load(Ordering::Relaxed) == INITIALIZING {
1502 std::hint::spin_loop();
1503 }
1504 Err(SetLoggerError(()))
1505 }
1506 _ => Err(SetLoggerError(())),
1507 }
1508 }
1509
1510 /// A thread-unsafe version of [`set_logger`].
1511 ///
1512 /// This function is available on all platforms, even those that do not have
1513 /// support for atomics that is needed by [`set_logger`].
1514 ///
1515 /// In almost all cases, [`set_logger`] should be preferred.
1516 ///
1517 /// # Safety
1518 ///
1519 /// This function is only safe to call when it cannot race with any other
1520 /// calls to `set_logger` or `set_logger_racy`.
1521 ///
1522 /// This can be upheld by (for example) making sure that **there are no other
1523 /// threads**, and (on embedded) that **interrupts are disabled**.
1524 ///
1525 /// It is safe to use other logging functions while this function runs
1526 /// (including all logging macros).
1527 ///
1528 /// [`set_logger`]: fn.set_logger.html
1529 pub unsafe fn set_logger_racy(logger: &'static dyn Log) -> Result<(), SetLoggerError> {
1530 match STATE.load(Ordering::Acquire) {
1531 UNINITIALIZED => {
1532 LOGGER = logger;
1533 STATE.store(INITIALIZED, Ordering::Release);
1534 Ok(())
1535 }
1536 INITIALIZING => {
1537 // This is just plain UB, since we were racing another initialization function
1538 unreachable!("set_logger_racy must not be used with other initialization functions")
1539 }
1540 _ => Err(SetLoggerError(())),
1541 }
1542 }
1543
1544 /// The type returned by [`set_logger`] if [`set_logger`] has already been called.
1545 ///
1546 /// [`set_logger`]: fn.set_logger.html
1547 #[allow(missing_copy_implementations)]
1548 #[derive(Debug)]
1549 pub struct SetLoggerError(());
1550
1551 impl fmt::Display for SetLoggerError {
1552 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1553 fmt.write_str(SET_LOGGER_ERROR)
1554 }
1555 }
1556
1557 // The Error trait is not available in libcore
1558 #[cfg(feature = "std")]
1559 impl error::Error for SetLoggerError {}
1560
1561 /// The type returned by [`from_str`] when the string doesn't match any of the log levels.
1562 ///
1563 /// [`from_str`]: https://doc.rust-lang.org/std/str/trait.FromStr.html#tymethod.from_str
1564 #[allow(missing_copy_implementations)]
1565 #[derive(Debug, PartialEq, Eq)]
1566 pub struct ParseLevelError(());
1567
1568 impl fmt::Display for ParseLevelError {
1569 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1570 fmt.write_str(LEVEL_PARSE_ERROR)
1571 }
1572 }
1573
1574 // The Error trait is not available in libcore
1575 #[cfg(feature = "std")]
1576 impl error::Error for ParseLevelError {}
1577
1578 /// Returns a reference to the logger.
1579 ///
1580 /// If a logger has not been set, a no-op implementation is returned.
1581 pub fn logger() -> &'static dyn Log {
1582 // Acquire memory ordering guarantees that current thread would see any
1583 // memory writes that happened before store of the value
1584 // into `STATE` with memory ordering `Release` or stronger.
1585 //
1586 // Since the value `INITIALIZED` is written only after `LOGGER` was
1587 // initialized, observing it after `Acquire` load here makes both
1588 // write to the `LOGGER` static and initialization of the logger
1589 // internal state synchronized with current thread.
1590 if STATE.load(Ordering::Acquire) != INITIALIZED {
1591 static NOP: NopLogger = NopLogger;
1592 &NOP
1593 } else {
1594 unsafe { LOGGER }
1595 }
1596 }
1597
1598 // WARNING: this is not part of the crate's public API and is subject to change at any time
1599 #[doc(hidden)]
1600 pub mod __private_api;
1601
1602 /// The statically resolved maximum log level.
1603 ///
1604 /// See the crate level documentation for information on how to configure this.
1605 ///
1606 /// This value is checked by the log macros, but not by the `Log`ger returned by
1607 /// the [`logger`] function. Code that manually calls functions on that value
1608 /// should compare the level against this value.
1609 ///
1610 /// [`logger`]: fn.logger.html
1611 pub const STATIC_MAX_LEVEL: LevelFilter = match cfg!(debug_assertions) {
1612 false if cfg!(feature = "release_max_level_off") => LevelFilter::Off,
1613 false if cfg!(feature = "release_max_level_error") => LevelFilter::Error,
1614 false if cfg!(feature = "release_max_level_warn") => LevelFilter::Warn,
1615 false if cfg!(feature = "release_max_level_info") => LevelFilter::Info,
1616 false if cfg!(feature = "release_max_level_debug") => LevelFilter::Debug,
1617 false if cfg!(feature = "release_max_level_trace") => LevelFilter::Trace,
1618 _ if cfg!(feature = "max_level_off") => LevelFilter::Off,
1619 _ if cfg!(feature = "max_level_error") => LevelFilter::Error,
1620 _ if cfg!(feature = "max_level_warn") => LevelFilter::Warn,
1621 _ if cfg!(feature = "max_level_info") => LevelFilter::Info,
1622 _ if cfg!(feature = "max_level_debug") => LevelFilter::Debug,
1623 _ => LevelFilter::Trace,
1624 };
1625
1626 #[cfg(test)]
1627 mod tests {
1628 use super::{Level, LevelFilter, ParseLevelError, STATIC_MAX_LEVEL};
1629
1630 #[test]
1631 fn test_levelfilter_from_str() {
1632 let tests = [
1633 ("off", Ok(LevelFilter::Off)),
1634 ("error", Ok(LevelFilter::Error)),
1635 ("warn", Ok(LevelFilter::Warn)),
1636 ("info", Ok(LevelFilter::Info)),
1637 ("debug", Ok(LevelFilter::Debug)),
1638 ("trace", Ok(LevelFilter::Trace)),
1639 ("OFF", Ok(LevelFilter::Off)),
1640 ("ERROR", Ok(LevelFilter::Error)),
1641 ("WARN", Ok(LevelFilter::Warn)),
1642 ("INFO", Ok(LevelFilter::Info)),
1643 ("DEBUG", Ok(LevelFilter::Debug)),
1644 ("TRACE", Ok(LevelFilter::Trace)),
1645 ("asdf", Err(ParseLevelError(()))),
1646 ];
1647 for &(s, ref expected) in &tests {
1648 assert_eq!(expected, &s.parse());
1649 }
1650 }
1651
1652 #[test]
1653 fn test_level_from_str() {
1654 let tests = [
1655 ("OFF", Err(ParseLevelError(()))),
1656 ("error", Ok(Level::Error)),
1657 ("warn", Ok(Level::Warn)),
1658 ("info", Ok(Level::Info)),
1659 ("debug", Ok(Level::Debug)),
1660 ("trace", Ok(Level::Trace)),
1661 ("ERROR", Ok(Level::Error)),
1662 ("WARN", Ok(Level::Warn)),
1663 ("INFO", Ok(Level::Info)),
1664 ("DEBUG", Ok(Level::Debug)),
1665 ("TRACE", Ok(Level::Trace)),
1666 ("asdf", Err(ParseLevelError(()))),
1667 ];
1668 for &(s, ref expected) in &tests {
1669 assert_eq!(expected, &s.parse());
1670 }
1671 }
1672
1673 #[test]
1674 fn test_level_as_str() {
1675 let tests = &[
1676 (Level::Error, "ERROR"),
1677 (Level::Warn, "WARN"),
1678 (Level::Info, "INFO"),
1679 (Level::Debug, "DEBUG"),
1680 (Level::Trace, "TRACE"),
1681 ];
1682 for (input, expected) in tests {
1683 assert_eq!(*expected, input.as_str());
1684 }
1685 }
1686
1687 #[test]
1688 fn test_level_show() {
1689 assert_eq!("INFO", Level::Info.to_string());
1690 assert_eq!("ERROR", Level::Error.to_string());
1691 }
1692
1693 #[test]
1694 fn test_levelfilter_show() {
1695 assert_eq!("OFF", LevelFilter::Off.to_string());
1696 assert_eq!("ERROR", LevelFilter::Error.to_string());
1697 }
1698
1699 #[test]
1700 fn test_cross_cmp() {
1701 assert!(Level::Debug > LevelFilter::Error);
1702 assert!(LevelFilter::Warn < Level::Trace);
1703 assert!(LevelFilter::Off < Level::Error);
1704 }
1705
1706 #[test]
1707 fn test_cross_eq() {
1708 assert!(Level::Error == LevelFilter::Error);
1709 assert!(LevelFilter::Off != Level::Error);
1710 assert!(Level::Trace == LevelFilter::Trace);
1711 }
1712
1713 #[test]
1714 fn test_to_level() {
1715 assert_eq!(Some(Level::Error), LevelFilter::Error.to_level());
1716 assert_eq!(None, LevelFilter::Off.to_level());
1717 assert_eq!(Some(Level::Debug), LevelFilter::Debug.to_level());
1718 }
1719
1720 #[test]
1721 fn test_to_level_filter() {
1722 assert_eq!(LevelFilter::Error, Level::Error.to_level_filter());
1723 assert_eq!(LevelFilter::Trace, Level::Trace.to_level_filter());
1724 }
1725
1726 #[test]
1727 fn test_level_filter_as_str() {
1728 let tests = &[
1729 (LevelFilter::Off, "OFF"),
1730 (LevelFilter::Error, "ERROR"),
1731 (LevelFilter::Warn, "WARN"),
1732 (LevelFilter::Info, "INFO"),
1733 (LevelFilter::Debug, "DEBUG"),
1734 (LevelFilter::Trace, "TRACE"),
1735 ];
1736 for (input, expected) in tests {
1737 assert_eq!(*expected, input.as_str());
1738 }
1739 }
1740
1741 #[test]
1742 fn test_level_up() {
1743 let info = Level::Info;
1744 let up = info.increment_severity();
1745 assert_eq!(up, Level::Debug);
1746
1747 let trace = Level::Trace;
1748 let up = trace.increment_severity();
1749 // trace is already highest level
1750 assert_eq!(up, trace);
1751 }
1752
1753 #[test]
1754 fn test_level_filter_up() {
1755 let info = LevelFilter::Info;
1756 let up = info.increment_severity();
1757 assert_eq!(up, LevelFilter::Debug);
1758
1759 let trace = LevelFilter::Trace;
1760 let up = trace.increment_severity();
1761 // trace is already highest level
1762 assert_eq!(up, trace);
1763 }
1764
1765 #[test]
1766 fn test_level_down() {
1767 let info = Level::Info;
1768 let down = info.decrement_severity();
1769 assert_eq!(down, Level::Warn);
1770
1771 let error = Level::Error;
1772 let down = error.decrement_severity();
1773 // error is already lowest level
1774 assert_eq!(down, error);
1775 }
1776
1777 #[test]
1778 fn test_level_filter_down() {
1779 let info = LevelFilter::Info;
1780 let down = info.decrement_severity();
1781 assert_eq!(down, LevelFilter::Warn);
1782
1783 let error = LevelFilter::Error;
1784 let down = error.decrement_severity();
1785 assert_eq!(down, LevelFilter::Off);
1786 // Off is already the lowest
1787 assert_eq!(down.decrement_severity(), down);
1788 }
1789
1790 #[test]
1791 #[cfg_attr(not(debug_assertions), ignore)]
1792 fn test_static_max_level_debug() {
1793 if cfg!(feature = "max_level_off") {
1794 assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Off);
1795 } else if cfg!(feature = "max_level_error") {
1796 assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Error);
1797 } else if cfg!(feature = "max_level_warn") {
1798 assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Warn);
1799 } else if cfg!(feature = "max_level_info") {
1800 assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Info);
1801 } else if cfg!(feature = "max_level_debug") {
1802 assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Debug);
1803 } else {
1804 assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Trace);
1805 }
1806 }
1807
1808 #[test]
1809 #[cfg_attr(debug_assertions, ignore)]
1810 fn test_static_max_level_release() {
1811 if cfg!(feature = "release_max_level_off") {
1812 assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Off);
1813 } else if cfg!(feature = "release_max_level_error") {
1814 assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Error);
1815 } else if cfg!(feature = "release_max_level_warn") {
1816 assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Warn);
1817 } else if cfg!(feature = "release_max_level_info") {
1818 assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Info);
1819 } else if cfg!(feature = "release_max_level_debug") {
1820 assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Debug);
1821 } else if cfg!(feature = "release_max_level_trace") {
1822 assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Trace);
1823 } else if cfg!(feature = "max_level_off") {
1824 assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Off);
1825 } else if cfg!(feature = "max_level_error") {
1826 assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Error);
1827 } else if cfg!(feature = "max_level_warn") {
1828 assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Warn);
1829 } else if cfg!(feature = "max_level_info") {
1830 assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Info);
1831 } else if cfg!(feature = "max_level_debug") {
1832 assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Debug);
1833 } else {
1834 assert_eq!(STATIC_MAX_LEVEL, LevelFilter::Trace);
1835 }
1836 }
1837
1838 #[test]
1839 #[cfg(feature = "std")]
1840 fn test_error_trait() {
1841 use super::SetLoggerError;
1842 let e = SetLoggerError(());
1843 assert_eq!(
1844 &e.to_string(),
1845 "attempted to set a logger after the logging system \
1846 was already initialized"
1847 );
1848 }
1849
1850 #[test]
1851 fn test_metadata_builder() {
1852 use super::MetadataBuilder;
1853 let target = "myApp";
1854 let metadata_test = MetadataBuilder::new()
1855 .level(Level::Debug)
1856 .target(target)
1857 .build();
1858 assert_eq!(metadata_test.level(), Level::Debug);
1859 assert_eq!(metadata_test.target(), "myApp");
1860 }
1861
1862 #[test]
1863 fn test_metadata_convenience_builder() {
1864 use super::Metadata;
1865 let target = "myApp";
1866 let metadata_test = Metadata::builder()
1867 .level(Level::Debug)
1868 .target(target)
1869 .build();
1870 assert_eq!(metadata_test.level(), Level::Debug);
1871 assert_eq!(metadata_test.target(), "myApp");
1872 }
1873
1874 #[test]
1875 fn test_record_builder() {
1876 use super::{MetadataBuilder, RecordBuilder};
1877 let target = "myApp";
1878 let metadata = MetadataBuilder::new().target(target).build();
1879 let fmt_args = format_args!("hello");
1880 let record_test = RecordBuilder::new()
1881 .args(fmt_args)
1882 .metadata(metadata)
1883 .module_path(Some("foo"))
1884 .file(Some("bar"))
1885 .line(Some(30))
1886 .build();
1887 assert_eq!(record_test.metadata().target(), "myApp");
1888 assert_eq!(record_test.module_path(), Some("foo"));
1889 assert_eq!(record_test.file(), Some("bar"));
1890 assert_eq!(record_test.line(), Some(30));
1891 }
1892
1893 #[test]
1894 fn test_record_convenience_builder() {
1895 use super::{Metadata, Record};
1896 let target = "myApp";
1897 let metadata = Metadata::builder().target(target).build();
1898 let fmt_args = format_args!("hello");
1899 let record_test = Record::builder()
1900 .args(fmt_args)
1901 .metadata(metadata)
1902 .module_path(Some("foo"))
1903 .file(Some("bar"))
1904 .line(Some(30))
1905 .build();
1906 assert_eq!(record_test.target(), "myApp");
1907 assert_eq!(record_test.module_path(), Some("foo"));
1908 assert_eq!(record_test.file(), Some("bar"));
1909 assert_eq!(record_test.line(), Some(30));
1910 }
1911
1912 #[test]
1913 fn test_record_complete_builder() {
1914 use super::{Level, Record};
1915 let target = "myApp";
1916 let record_test = Record::builder()
1917 .module_path(Some("foo"))
1918 .file(Some("bar"))
1919 .line(Some(30))
1920 .target(target)
1921 .level(Level::Error)
1922 .build();
1923 assert_eq!(record_test.target(), "myApp");
1924 assert_eq!(record_test.level(), Level::Error);
1925 assert_eq!(record_test.module_path(), Some("foo"));
1926 assert_eq!(record_test.file(), Some("bar"));
1927 assert_eq!(record_test.line(), Some(30));
1928 }
1929
1930 #[test]
1931 #[cfg(feature = "kv")]
1932 fn test_record_key_values_builder() {
1933 use super::Record;
1934 use crate::kv::{self, VisitSource};
1935
1936 struct TestVisitSource {
1937 seen_pairs: usize,
1938 }
1939
1940 impl<'kvs> VisitSource<'kvs> for TestVisitSource {
1941 fn visit_pair(
1942 &mut self,
1943 _: kv::Key<'kvs>,
1944 _: kv::Value<'kvs>,
1945 ) -> Result<(), kv::Error> {
1946 self.seen_pairs += 1;
1947 Ok(())
1948 }
1949 }
1950
1951 let kvs: &[(&str, i32)] = &[("a", 1), ("b", 2)];
1952 let record_test = Record::builder().key_values(&kvs).build();
1953
1954 let mut visitor = TestVisitSource { seen_pairs: 0 };
1955
1956 record_test.key_values().visit(&mut visitor).unwrap();
1957
1958 assert_eq!(2, visitor.seen_pairs);
1959 }
1960
1961 #[test]
1962 #[cfg(feature = "kv")]
1963 fn test_record_key_values_get_coerce() {
1964 use super::Record;
1965
1966 let kvs: &[(&str, &str)] = &[("a", "1"), ("b", "2")];
1967 let record = Record::builder().key_values(&kvs).build();
1968
1969 assert_eq!(
1970 "2",
1971 record
1972 .key_values()
1973 .get("b".into())
1974 .expect("missing key")
1975 .to_borrowed_str()
1976 .expect("invalid value")
1977 );
1978 }
1979
1980 // Test that the `impl Log for Foo` blocks work
1981 // This test mostly operates on a type level, so failures will be compile errors
1982 #[test]
1983 fn test_foreign_impl() {
1984 use super::Log;
1985 #[cfg(feature = "std")]
1986 use std::sync::Arc;
1987
1988 fn assert_is_log<T: Log + ?Sized>() {}
1989
1990 assert_is_log::<&dyn Log>();
1991
1992 #[cfg(feature = "std")]
1993 assert_is_log::<Box<dyn Log>>();
1994
1995 #[cfg(feature = "std")]
1996 assert_is_log::<Arc<dyn Log>>();
1997
1998 // Assert these statements for all T: Log + ?Sized
1999 #[allow(unused)]
2000 fn forall<T: Log + ?Sized>() {
2001 #[cfg(feature = "std")]
2002 assert_is_log::<Box<T>>();
2003
2004 assert_is_log::<&T>();
2005
2006 #[cfg(feature = "std")]
2007 assert_is_log::<Arc<T>>();
2008 }
2009 }
2010 }