Skip to content

Commit 3575521

Browse files
authored
Make env_logger an optional dependency (#1226)
Now the built-in `env_logger` is guarded behind a Cargo feature "builtin_env_logger". It is a default feature, but can be disabled in Cargo.toml by setting `dependencies.mmtk.default-features = false`. In this way, VM bindings that want to implement its own logger can remove the `env_logger` crate from its dependencies. Fixes: #744
1 parent 3830168 commit 3575521

File tree

3 files changed

+46
-15
lines changed

3 files changed

+46
-15
lines changed

Cargo.toml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ crossbeam = "0.8.1"
2828
delegate = "0.12.0"
2929
downcast-rs = "1.1.1"
3030
enum-map = "2.4.2"
31-
env_logger = "0.11.3"
31+
env_logger = { version = "0.11.3", optional = true }
3232
is-terminal = "0.4.7"
3333
itertools = "0.12.0"
3434
jemalloc-sys = { version = "0.5.3", features = ["disable_initial_exec_tls"], optional = true }
@@ -66,7 +66,12 @@ name = "main"
6666
harness = false
6767

6868
[features]
69-
default = []
69+
default = ["builtin_env_logger"]
70+
71+
# Built-in env_logger. This feature is enabled by default.
72+
# The user can disable this default feature to remove `env_logger` from the dependencies.
73+
# See `crate::util::logger` for more details.
74+
builtin_env_logger = ["dep:env_logger"]
7075

7176
# This feature is only supported on x86-64 for now
7277
# It's manually added to CI scripts

src/memory_manager.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ use crate::vm::VMBinding;
3838
/// supported. Currently we assume a binding will only need one MMTk instance. Note that GC is enabled by default and the binding should
3939
/// implement `VMCollection::is_collection_enabled()` if it requires that the GC should be disabled at a particular time.
4040
///
41-
/// 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.
41+
/// This method will attempt to initialize the built-in `env_logger` if the Cargo feature "builtin_env_logger" is enabled (by default).
42+
/// If the VM would like to use its own logger, it should disable the default feature "builtin_env_logger" in `Cargo.toml`.
43+
///
4244
/// Note that, to allow MMTk to do GC properly, `initialize_collection()` needs to be called after this call when
4345
/// the VM's thread system is ready to spawn GC workers.
4446
///
@@ -51,12 +53,7 @@ use crate::vm::VMBinding;
5153
/// Arguments:
5254
/// * `builder`: The reference to a MMTk builder.
5355
pub fn mmtk_init<VM: VMBinding>(builder: &MMTKBuilder) -> Box<MMTK<VM>> {
54-
match crate::util::logger::try_init() {
55-
Ok(_) => debug!("MMTk initialized the logger."),
56-
Err(_) => debug!(
57-
"MMTk failed to initialize the logger. Possibly a logger has been initialized by user."
58-
),
59-
}
56+
crate::util::logger::try_init();
6057
#[cfg(all(feature = "perf_counter", target_os = "linux"))]
6158
{
6259
use std::fs::File;

src/util/logger.rs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,38 @@
1-
use log::{self, SetLoggerError};
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.
214
315
/// Attempt to init a env_logger for MMTk.
4-
pub fn try_init() -> Result<(), SetLoggerError> {
5-
env_logger::try_init_from_env(
6-
// By default, use info level logging.
7-
env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"),
8-
)
16+
/// Does nothing if the "builtin_env_logger" feature is disabled.
17+
pub(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+
}
938
}

0 commit comments

Comments
 (0)