facet_testhelpers/
lib.rs

1#![warn(missing_docs)]
2#![warn(clippy::std_instead_of_core)]
3#![warn(clippy::std_instead_of_alloc)]
4#![forbid(unsafe_code)]
5#![doc = include_str!("../README.md")]
6
7pub use color_eyre::eyre;
8pub use facet_testhelpers_macros::test;
9
10use log::{Level, LevelFilter, Log, Metadata, Record};
11use owo_colors::{OwoColorize, Style};
12use std::io::Write;
13
14struct SimpleLogger;
15
16impl Log for SimpleLogger {
17    fn enabled(&self, _metadata: &Metadata) -> bool {
18        true
19    }
20
21    fn log(&self, record: &Record) {
22        // Create style based on log level
23        let level_style = match record.level() {
24            Level::Error => Style::new().fg_rgb::<243, 139, 168>(), // Catppuccin red (Maroon)
25            Level::Warn => Style::new().fg_rgb::<249, 226, 175>(),  // Catppuccin yellow (Peach)
26            Level::Info => Style::new().fg_rgb::<166, 227, 161>(),  // Catppuccin green (Green)
27            Level::Debug => Style::new().fg_rgb::<137, 180, 250>(), // Catppuccin blue (Blue)
28            Level::Trace => Style::new().fg_rgb::<148, 226, 213>(), // Catppuccin teal (Teal)
29        };
30
31        // Convert level to styled display
32        eprintln!(
33            "{} - {}: {}",
34            record.level().style(level_style),
35            record
36                .target()
37                .style(Style::new().fg_rgb::<137, 180, 250>()), // Blue for the target
38            record.args()
39        );
40    }
41
42    fn flush(&self) {
43        let _ = std::io::stderr().flush();
44    }
45}
46
47/// Installs color-backtrace (except on miri), and sets up a simple logger.
48pub fn setup() {
49    #[cfg(not(miri))]
50    color_eyre::install().expect("Failed to set up color-eyre");
51    #[cfg(not(miri))]
52    color_backtrace::install();
53
54    let logger = Box::new(SimpleLogger);
55    log::set_boxed_logger(logger).unwrap();
56    log::set_max_level(LevelFilter::Trace);
57}