Skip to content

Commit

Permalink
Update error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
wks committed Nov 5, 2024
1 parent 86ce270 commit 7fe1407
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
13 changes: 9 additions & 4 deletions src/memory_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ 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 All @@ -38,7 +39,9 @@ use crate::vm::VMBinding;
/// supported. Currently we assume a binding will only need one MMTk instance. Note that GC is enabled by default and the binding should
/// implement `VMCollection::is_collection_enabled()` if it requires that the GC should be disabled at a particular time.
///
/// Note that this method will attempt to initialize a logger. If the VM would like to use its own logger, it should initialize the logger before calling this method.
/// This method will attempt to initialize the built-in `env_logger` if the Cargo feature "builtin_env_logger" is enabled (by default).
/// If the VM would like to use its own logger, it should disable the default feature "builtin_env_logger" in `Cargo.toml`.
///
/// Note that, to allow MMTk to do GC properly, `initialize_collection()` needs to be called after this call when
/// the VM's thread system is ready to spawn GC workers.
///
Expand All @@ -53,9 +56,11 @@ use crate::vm::VMBinding;
pub fn mmtk_init<VM: VMBinding>(builder: &MMTKBuilder) -> Box<MMTK<VM>> {
match crate::util::logger::try_init() {
Ok(_) => debug!("MMTk initialized the logger."),
Err(_) => debug!(
"MMTk failed to initialize the logger. Possibly a logger has been initialized by user."
),
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}")
}
#[cfg(all(feature = "perf_counter", target_os = "linux"))]
{
Expand Down
14 changes: 11 additions & 3 deletions src/util/logger.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
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),
}

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

0 comments on commit 7fe1407

Please sign in to comment.