1use std::sync::atomic::{AtomicUsize, Ordering};
2
3use colored::{Color, Colorize};
4use env_logger::fmt::Formatter;
5
6use crate::constants::STYLEX_PREFIX;
7
8pub const ANSI_RED: &str = "\x1B[31m";
10pub const ANSI_YELLOW: &str = "\x1B[33m";
11pub const ANSI_GREEN: &str = "\x1B[32m";
12pub const ANSI_BLUE: &str = "\x1B[34m";
13pub const ANSI_WHITE: &str = "\x1B[37m";
14pub const ANSI_CYAN: &str = "\x1B[36m";
15pub const ANSI_BOLD: &str = "\x1B[1m";
16pub const ANSI_DIM: &str = "\x1B[2m";
17pub const ANSI_RESET: &str = "\x1B[0m";
18
19pub const ANSI_ORANGE: &str = "\x1B[38;5;208m";
20
21static MAX_MODULE_WIDTH: AtomicUsize = AtomicUsize::new(0);
22
23fn max_target_width(target: &str) -> usize {
24 let max_width = MAX_MODULE_WIDTH.load(Ordering::Relaxed);
25
26 if max_width < target.len() {
27 MAX_MODULE_WIDTH.store(target.len(), Ordering::Relaxed);
28 target.len()
29 } else {
30 max_width
31 }
32}
33
34pub fn log_formatter(f: &mut Formatter, record: &log::Record) -> std::io::Result<()> {
35 use std::io::Write;
36
37 let target = record.target();
38 let max_width = max_target_width(target);
39
40 let level_color = match record.level() {
41 log::Level::Error => Color::Red,
42 log::Level::Warn => Color::Yellow,
43 log::Level::Info => Color::Green,
44 log::Level::Debug => Color::Blue,
45 log::Level::Trace => Color::White,
46 };
47
48 writeln!(
49 f,
50 "{} {} {:width$} > {}",
51 record.level().to_string().color(level_color).bold(),
52 STYLEX_PREFIX.bright_blue().bold(),
53 target.bold(),
54 record.args(),
55 width = max_width
56 )
57}