1use log::{Level, LevelFilter, Log, Metadata, Record};
3
4use std::io::{self, Write};
5
6static LOGGER: CliLog = CliLog;
7
8struct CliLog;
9
10#[allow(clippy::print_stdout, clippy::print_stderr)]
11impl Log for CliLog {
12 fn enabled(&self, metadata: &Metadata<'_>) -> bool {
13 metadata.level() >= log::max_level()
14 }
15
16 fn log(&self, record: &Record<'_>) {
17 if record.level() <= log::max_level() {
18 match record.level() {
19 Level::Error => eprintln!("error: {}", record.args()),
20 Level::Info | Level::Warn => {
21 println!("{}", record.args());
22 }
23 Level::Debug => eprintln!("debug: {}", record.args()),
24 Level::Trace => eprintln!("trace: {}", record.args()),
25 }
26 }
27 }
28
29 #[allow(unused_results, unused_must_use)]
30 fn flush(&self) {
31 io::stdout().flush();
32 io::stderr().flush();
33 }
34}
35
36#[inline]
38pub fn init_with_level(filter: LevelFilter) {
39 log::set_logger(&LOGGER).expect("Setup logger");
40 log::set_max_level(filter);
41}