Skip to content
Myr

logger.rs

This file

Path

crates/core/logger.rs

72 lines · 2.1 KiB · Rust

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than 5f5da48 point at it.

globset: support nested alternatesLuke Sandbergbefore adoption5f5da48

lines 1–72why, one step back
1/*!
2Defines a super simple logger that works with the `log` crate.34We don't do anything fancy. We just need basic log levels and the ability to5print to stderr. We therefore avoid bringing in extra dependencies just for6this functionality.7*/89use log::Log;1011/// The simplest possible logger that logs to stderr.12///13/// This logger does no filtering. Instead, it relies on the `log` crates14/// filtering via its global max_level setting.15#[derive(Debug)]16pub(crate) struct Logger(());1718/// A singleton used as the target for an implementation of the `Log` trait.19const LOGGER: &'static Logger = &Logger(());2021impl Logger {22 /// Create a new logger that logs to stderr and initialize it as the23 /// global logger. If there was a problem setting the logger, then an24 /// error is returned.25 pub(crate) fn init() -> Result<(), log::SetLoggerError> {26 log::set_logger(LOGGER)27 }28}2930impl Log for Logger {31 fn enabled(&self, _: &log::Metadata<'_>) -> bool {32 // We set the log level via log::set_max_level, so we don't need to33 // implement filtering here.34 true35 }3637 fn log(&self, record: &log::Record<'_>) {38 match (record.file(), record.line()) {39 (Some(file), Some(line)) => {40 eprintln_locked!(41 "{}|{}|{}:{}: {}",42 record.level(),43 record.target(),44 file,45 line,46 record.args()47 );48 }49 (Some(file), None) => {50 eprintln_locked!(51 "{}|{}|{}: {}",52 record.level(),53 record.target(),54 file,55 record.args()56 );57 }58 _ => {59 eprintln_locked!(60 "{}|{}: {}",61 record.level(),62 record.target(),63 record.args()64 );65 }66 }67 }6869 fn flush(&self) {70 // We use eprintln_locked! which is flushed on every call.71 }72}