Debian Codegraph @

debian/rust-vendor/jiff/tests/lib.rs

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