logforth_bridge_log/
lib.rs

1// Copyright 2024 FastLabs Developers
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! A bridge to forward logs from the `log` crate to `logforth`.
16
17#![cfg_attr(docsrs, feature(doc_auto_cfg))]
18
19use logforth_core::default_logger;
20
21struct LogCrateLogger(());
22
23impl log::Log for LogCrateLogger {
24    fn enabled(&self, metadata: &log::Metadata) -> bool {
25        let Some(logger) = default_logger() else {
26            return false;
27        };
28
29        log::Log::enabled(logger, metadata)
30    }
31
32    fn log(&self, record: &log::Record) {
33        if let Some(logger) = default_logger() {
34            log::Log::log(logger, record);
35        }
36    }
37
38    fn flush(&self) {
39        if let Some(logger) = default_logger() {
40            log::Log::flush(logger);
41        }
42    }
43}
44
45/// Set up the log crate global logger.
46///
47/// This function calls [`log::set_logger`] to set up a `LogCrateProxy` and
48/// all logs from log crate will be forwarded to `logforth`'s logger.
49///
50/// This should be called early in the execution of a Rust program. Any log events that occur
51/// before initialization will be ignored.
52///
53/// This function will set the global maximum log level to `Trace`. To override this, call
54/// [`log::set_max_level`] after this function.
55///
56/// # Errors
57///
58/// Return an error if the log crate global logger has already been set.
59///
60/// # Examples
61///
62/// ```
63/// logforth_bridge_log::setup_log_crate();
64/// logforth_core::builder().apply()
65/// ```
66pub fn try_setup_log_crate() -> Result<(), log::SetLoggerError> {
67    static LOGGER: LogCrateLogger = LogCrateLogger(());
68    log::set_logger(&LOGGER)?;
69    log::set_max_level(log::LevelFilter::Trace);
70    Ok(())
71}
72
73/// Set up the log crate global logger.
74///
75/// This function calls [`log::set_logger`] to set up a `LogCrateProxy` and
76/// all logs from log crate will be forwarded to `logforth`'s logger.
77///
78/// This should be called early in the execution of a Rust program. Any log events that occur
79/// before initialization will be ignored.
80///
81/// This function will panic if it is called more than once, or if another library has already
82/// initialized the log crate global logger.
83///
84/// This function will set the global maximum log level to `Trace`. To override this, call
85/// [`log::set_max_level`] after this function.
86///
87/// # Panics
88///
89/// Panic if the log crate global logger has already been set.
90///
91/// # Examples
92///
93/// ```
94/// logforth_bridge_log::setup_log_crate();
95/// logforth_core::builder().apply()
96/// ```
97pub fn setup_log_crate() {
98    try_setup_log_crate().expect(
99        "logforth::bridge::setup_log_crate must be called before the log crate global logger initialized",
100    )
101}