info_utils/output/
error.rs

1///# Error
2/// Logs errors and then kills the program with exit code 1.
3///
4///## ⚠WARNING⚠
5/// `error!` creates a hook for `panic!` which also silences the typical error message.
6/// This hook applies to ALL uses of panic once `error!` has been invoked.
7/// The `panic!` macro will be affected for the rest of your program's runtime.
8///
9/// Examples
10///
11/// Kill program
12/// ```no_run
13///# use info_utils::prelude::*;
14///# fn main()  {
15///     // Something is happening..
16///     // Uh oh, an error has occurred.
17///     error!("Something unrecoverable went wrong!");
18///# }
19///```
20///
21///Kill thread
22/// ```rust
23///# use std::thread;
24///# use info_utils::prelude::*;
25///# fn main() {
26/// thread::spawn(|| {
27///     // Do a calculation
28///     // Oh no, an error happened and we need to kill the thread!
29///     error!("Noncritical error, thread aborting");
30/// }).join().eval_or_default();
31///# }
32///```
33#[macro_export]
34macro_rules! error {
35    ($($msg:tt)*) => {{
36        let binding = format!("{}",format_args!($($msg)*));
37        eprintln!("\x1b[0;1;31merror\x1b[0;36m [{:?}]:\x1b[0m {}", std::thread::current().name().unwrap_or("<unknown>"), binding);
38
39            // eprintln!("\x1b[1;31mERR\x1b[0m\r");
40            // println!("\x1b[36m[{:?}]:\x1b[39m\r", std::thread::current().name().unwrap_or("<unknown>"));
41            // for line in binding.lines() {
42            //     // eprint!("  ");
43            //     println!("{}\r", line);
44            // }
45        std::panic::set_hook(Box::new(|_|{}));
46        panic!();
47    }}
48}