Debian Codegraph @

debian/missing-sources/jiff/tests/lib.rs

raw
1 // See: https://github.com/rust-lang/rust/pull/121364
2 #![allow(unknown_lints, ambiguous_negative_literals)]
3
4 mod init;
5 mod procmacro;
6 mod tc39_262;
7
8 /// The simplest possible logger that logs to stderr.
9 ///
10 /// This logger does no filtering. Instead, it relies on the `log` crates
11 /// filtering via its global max_level setting.
12 ///
13 /// This provides a super simple logger that works with the `log` crate.
14 /// We don't need anything fancy; just basic log levels and the ability to
15 /// print to stderr. We therefore avoid bringing in extra dependencies just
16 /// for this functionality.
17 #[cfg(all(test))]
18 #[derive(Debug)]
19 pub(crate) struct Logger(());
20
21 #[cfg(all(test, feature = "std", feature = "logging"))]
22 const LOGGER: &'static Logger = &Logger(());
23
24 #[cfg(all(test))]
25 impl Logger {
26 /// Create a new logger that logs to stderr and initialize it as the
27 /// global logger. If there was a problem setting the logger, then an
28 /// error is returned.
29 pub(crate) fn init() -> Result<(), log::SetLoggerError> {
30 #[cfg(all(feature = "std", feature = "logging"))]
31 {
32 log::set_logger(LOGGER)?;
33 log::set_max_level(log::LevelFilter::Trace);
34 Ok(())
35 }
36 #[cfg(not(all(feature = "std", feature = "logging")))]
37 {
38 Ok(())
39 }
40 }
41 }
42
43 #[cfg(all(test, feature = "std", feature = "logging"))]
44 impl log::Log for Logger {
45 fn enabled(&self, _: &log::Metadata<'_>) -> bool {
46 // We set the log level via log::set_max_level, so we don't need to
47 // implement filtering here.
48 true
49 }
50
51 fn log(&self, record: &log::Record<'_>) {
52 let now = jiff::Timestamp::now();
53 match (record.file(), record.line()) {
54 (Some(file), Some(line)) => {
55 std::eprintln!(
56 "{}|{}|{}|{}:{}: {}",
57 now,
58 record.level(),
59 record.target(),
60 file,
61 line,
62 record.args()
63 );
64 }
65 (Some(file), None) => {
66 std::eprintln!(
67 "{}|{}|{}|{}: {}",
68 now,
69 record.level(),
70 record.target(),
71 file,
72 record.args()
73 );
74 }
75 _ => {
76 std::eprintln!(
77 "{}|{}|{}: {}",
78 now,
79 record.level(),
80 record.target(),
81 record.args()
82 );
83 }
84 }
85 }
86
87 fn flush(&self) {
88 // We use eprintln! which is flushed on every call.
89 }
90 }