1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0
// Copyright 2021 Conflux Foundation. All rights reserved.
// Conflux is free software and distributed under GNU General Public License.
// See http://www.gnu.org/licenses/
//! Global logger definition and functions
use crate::{counters::STRUCT_LOG_COUNT, Event, Metadata};
use once_cell::sync::OnceCell;
use std::sync::Arc;
/// The global `Logger`
static LOGGER: OnceCell<Arc<dyn Logger>> = OnceCell::new();
/// A trait encapsulating the operations required of a logger.
pub trait Logger: Sync + Send + 'static {
/// Determines if an event with the specified metadata would be logged
fn enabled(&self, metadata: &Metadata) -> bool;
/// Record an event
fn record(&self, event: &Event);
/// Flush any buffered events
fn flush(&self);
}
/// Record a logging event to the global `Logger`
pub(crate) fn dispatch(event: &Event) {
if let Some(logger) = LOGGER.get() {
STRUCT_LOG_COUNT.inc();
logger.record(event)
}
}
/// Check if the global `Logger` is enabled
pub(crate) fn enabled(metadata: &Metadata) -> bool {
LOGGER
.get()
.map(|logger| logger.enabled(metadata))
.unwrap_or(false)
}
/// Sets the global `Logger` exactly once
pub fn set_global_logger(logger: Arc<dyn Logger>) {
if LOGGER.set(logger).is_err() {
eprintln!("Global logger has already been set");
}
}
/// Flush the global `Logger`
pub fn flush() {
if let Some(logger) = LOGGER.get() {
logger.flush();
}
}