mmtk/util/logger.rs
1//! This module provides a built-in logger implementation.
2//!
3//! The built-in logger implementation uses the `env_logger` crate. It is enabled by the Cargo
4//! feature "builtin_env_logger" which is enabled by default. When enabled, it will be initialized
5//! in [`crate::memory_manager::mmtk_init`] and will show logs of levels INFO or lower (the lower,
6//! the more important).
7//!
8//! This provides convenient out-of-the-box experience for binding developers so that they can see
9//! logs when using MMTk without configuration, and can easily configure log levels from environment
10//! variables. Some bindings may wish to choose a different implementation, or implement their own
11//! logging implementations to integrate with the existing logging frameworks of their VMs. In such
12//! cases, the binding can disable the Cargo feature "builtin_env_logger" and register their own
13//! implementations with the `log` crate.
14
15/// Attempt to init a env_logger for MMTk.
16/// Does nothing if the "builtin_env_logger" feature is disabled.
17pub(crate) fn try_init() {
18 cfg_if::cfg_if! {
19 if #[cfg(feature = "builtin_env_logger")] {
20 let result = env_logger::try_init_from_env(
21 // By default, show info level logging.
22 env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"),
23 );
24
25 match result {
26 Ok(()) => {
27 debug!("MMTk initialized the logger.");
28 }
29 Err(e) => {
30 // Currently `log::SetLoggerError` can only be raised for one reason: the logger has already been initialized.
31 debug!("MMTk failed to initialize the built-in env_logger: {e}");
32 }
33 }
34 } else {
35 debug!("MMTk didn't initialize the built-in env_logger. The Cargo feature \"builtin_env_logger\" is not enabled.");
36 }
37 }
38}