Skip to main content

optic_core/
log.rs

1/// Print colored output using an [`ANSI`](crate::ansi::ANSI) constant.
2///
3/// ```
4/// use optic_core::*;
5///
6/// log_color!("processing: {}", RED, "item 1");
7/// ```
8#[macro_export]
9macro_rules! log_color {
10    ($fmt:expr, $color:expr) => {
11        let fmt = format!($fmt);
12        println!("{}{}{}", $color.prefix, fmt, $color.suffix);
13    };
14    ($fmt:expr, $color:expr, $($args:tt)*) => {
15        let fmt = format!($fmt, $($args)*);
16        println!("{}{}{}", $color.prefix, fmt, $color.suffix);
17    };
18}
19
20/// Print a bold-blue `[EVENT]` message.
21#[macro_export]
22macro_rules! log_event {
23    ($fmt:expr) => {
24        let fmt = format!($fmt);
25        println!("{}[EVENT] {}{}", $crate::ansi::BOLD_BLUE.prefix, fmt, $crate::ansi::BOLD_BLUE.suffix);
26    };
27    ($fmt:expr, $($args:tt)*) => {
28        let fmt = format!($fmt, $($args)*);
29        println!("{}[EVENT] {}{}", $crate::ansi::BOLD_BLUE.prefix, fmt, $crate::ansi::BOLD_BLUE.suffix);
30    };
31}
32
33/// Print a bold-green `[INFO]` message.
34#[macro_export]
35macro_rules! log_info {
36    ($fmt:expr) => {
37        let fmt = format!($fmt);
38        println!("{}[INFO] {}{}", $crate::ansi::BOLD_GREEN.prefix, fmt, $crate::ansi::BOLD_GREEN.suffix);
39    };
40    ($fmt:expr, $($args:tt)*) => {
41        let fmt = format!($fmt, $($args)*);
42        println!("{}[INFO] {}{}", $crate::ansi::BOLD_GREEN.prefix, fmt, $crate::ansi::BOLD_GREEN.suffix);
43    };
44}
45
46/// Print a bold-yellow `[WARN]` message.
47#[macro_export]
48macro_rules! log_warn {
49    ($fmt:expr) => {
50        let fmt = format!($fmt);
51        println!("{}[WARN] {}{}", $crate::ansi::BOLD_YELLOW.prefix, fmt, $crate::ansi::BOLD_YELLOW.suffix);
52    };
53    ($fmt:expr, $($args:tt)*) => {
54        let fmt = format!($fmt, $($args)*);
55        println!("{}[WARN] {}{}", $crate::ansi::BOLD_YELLOW.prefix, fmt, $crate::ansi::BOLD_YELLOW.suffix);
56    };
57}
58
59/// Print a bold-red `[FATAL]` message, then abort the process.
60#[macro_export]
61macro_rules! log_fatal {
62    ($fmt:expr) => {
63        let fmt = format!($fmt);
64        println!("{}[FATAL] {}{}", $crate::ansi::BOLD_RED.prefix, fmt, $crate::ansi::BOLD_RED.suffix);
65    };
66    ($fmt:expr, $($args:tt)*) => {
67        let fmt = format!($fmt, $($args)*);
68        println!("{}[FATAL] {}{}", $crate::ansi::BOLD_RED.prefix, fmt, $crate::ansi::BOLD_RED.suffix);
69    };
70}
71
72/// Print a bold-red `[ERROR]` message.
73#[macro_export]
74macro_rules! log_error {
75    ($fmt:expr) => {
76        let fmt = format!($fmt);
77        println!("{}[ERROR] {}{}", $crate::ansi::BOLD_RED.prefix, fmt, $crate::ansi::BOLD_RED.suffix);
78    };
79    ($fmt:expr, $($args:tt)*) => {
80        let fmt = format!($fmt, $($args)*);
81        println!("{}[ERROR] {}{}", $crate::ansi::BOLD_RED.prefix, fmt, $crate::ansi::BOLD_RED.suffix);
82    };
83}