Skip to main content

chaud_hot/util/
minilog.rs

1use log::{Level, LevelFilter, Log, Metadata, Record};
2
3struct MiniLogger;
4
5impl Log for MiniLogger {
6    fn enabled(&self, metadata: &Metadata) -> bool {
7        metadata.target().starts_with("chaud")
8    }
9
10    fn log(&self, record: &Record) {
11        if !self.enabled(record.metadata()) {
12            return;
13        }
14
15        eprintln!(
16            "[chaud] [{}] [{}]: {}",
17            record.level(),
18            record.target(),
19            record.args()
20        );
21    }
22
23    fn flush(&self) {}
24}
25
26/// Configure [`MiniLogger`] as the logger if no logger has been configured yet.
27///
28/// This function should be called only once.
29pub fn init() {
30    if log::set_logger(&MiniLogger).is_ok() {
31        log::set_max_level(LevelFilter::Warn);
32        log::warn!("No logger installed. Installing minimal stderr logging.");
33    } else if !log::log_enabled!(Level::Warn) && !cfg!(feature = "silence-log-level-warning") {
34        eprintln!(
35            "[chaud] [WARN] Logging for `chaud` is disabled, you may miss \
36                    important messages about hot reloading issues."
37        );
38    }
39}