Skip to content

Commit

Permalink
Move error checking into logger::try_init
Browse files Browse the repository at this point in the history
memory_manager::mmtk_init simply calls `try_init()` now.
  • Loading branch information
wks committed Nov 6, 2024
1 parent 17e6441 commit 6967a58
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
8 changes: 3 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,9 @@ harness = false
[features]
default = ["builtin_env_logger"]

# Built-in env_logger. This feature is enabled by default. mmtk-core provides `env_logger` as the
# default logging implementation which is initializes when initializing the MMTK instance. This is
# convenient for new binding developers because they can always see some logs after MMTk is
# initialized without any compile-time configuration. Bindings that want to bring their own logger
# implementations can disable this feature so that mmtk-core won't depend on the `env_logger` crate.
# Built-in env_logger. This feature is enabled by default.
# The user can disable this default feature to remove `env_logger` from the dependencies.
# See `crate::util::logger` for more details.
builtin_env_logger = []

# This feature is only supported on x86-64 for now
Expand Down
10 changes: 1 addition & 9 deletions src/memory_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use crate::scheduler::{GCWork, GCWorker};
use crate::util::alloc::allocators::AllocatorSelector;
use crate::util::constants::{LOG_BYTES_IN_PAGE, MIN_OBJECT_SIZE};
use crate::util::heap::layout::vm_layout::vm_layout;
use crate::util::logger::LoggerError;
use crate::util::opaque_pointer::*;
use crate::util::{Address, ObjectReference};
use crate::vm::slot::MemorySlice;
Expand Down Expand Up @@ -54,14 +53,7 @@ use crate::vm::VMBinding;
/// Arguments:
/// * `builder`: The reference to a MMTk builder.
pub fn mmtk_init<VM: VMBinding>(builder: &MMTKBuilder) -> Box<MMTK<VM>> {
match crate::util::logger::try_init() {
Ok(_) => debug!("MMTk initialized the logger."),
Err(LoggerError::NoBuiltinLogger) =>
debug!("MMTk didn't initialize the built-in env_logger. The Cargo feature \"builtin_env_logger\" is not enabled."),
Err(LoggerError::SetLoggerError(e)) =>
// Currently `log::SetLoggerError` can only be raised for one reason: the logger has already been initialized.
debug!("MMTk failed to initialize the built-in env_logger: {e}")
}
crate::util::logger::try_init();
#[cfg(all(feature = "perf_counter", target_os = "linux"))]
{
use std::fs::File;
Expand Down
42 changes: 28 additions & 14 deletions src/util/logger.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
use log::SetLoggerError;

/// Failure of setting logger.
pub(crate) enum LoggerError {
/// The user didn't enable the "builtin_env_logger" feature.
NoBuiltinLogger,
/// Error happened while setting the logger.
SetLoggerError(SetLoggerError),
}
//! This module provides a built-in logger implementation.
//!
//! The built-in logger implementation uses the `env_logger` crate. It is enabled by the Cargo
//! feature "builtin_env_logger" which is enabled by default. When enabled, it will be initialized
//! in [`crate::memory_manager::mmtk_init`] and will show logs of levels INFO or lower (the lower,
//! the more important).
//!
//! This provides convenient out-of-the-box experience for binding developers so that they can see
//! logs when using MMTk without configuration, and can easily configure log levels from environment
//! variables. Some bindings may wish to choose a different implementation, or implement their own
//! logging implementations to integrate with the existing logging frameworks of their VMs. In such
//! cases, the binding can disable the Cargo feature "builtin_env_logger" and register their own
//! implementations with the `log` crate.

/// Attempt to init a env_logger for MMTk.
/// Does nothing if the "builtin_env_logger" feature is disabled.
pub(crate) fn try_init() -> Result<(), LoggerError> {
pub(crate) fn try_init() {
cfg_if::cfg_if! {
if #[cfg(feature = "builtin_env_logger")] {
env_logger::try_init_from_env(
// By default, use info level logging.
let result = env_logger::try_init_from_env(
// By default, show info level logging.
env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"),
).map_err(LoggerError::SetLoggerError)
);

match result {
Ok(()) => {
debug!("MMTk initialized the logger.");
}
Err(e) => {
// Currently `log::SetLoggerError` can only be raised for one reason: the logger has already been initialized.
debug!("MMTk failed to initialize the built-in env_logger: {e}");
}
}
} else {
Err(LoggerError::NoBuiltinLogger)
debug!("MMTk didn't initialize the built-in env_logger. The Cargo feature \"builtin_env_logger\" is not enabled.");
}
}
}

0 comments on commit 6967a58

Please sign in to comment.