Skip to content

Commit 89a7e6c

Browse files
committed
feat: add a feature to statically disable debugging in the sdk
When the debug feature is disabled, most actor debugging logic should get statically compiled out. I've left it enabled by default for now, but we should consider disabling it by default for the final release. But if we do that, we need _some_ way to re-execute actors with debugging enabled.
1 parent 664ab0b commit 89a7e6c

File tree

3 files changed

+54
-33
lines changed

3 files changed

+54
-33
lines changed

sdk/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ cid = { version = "0.8.2", default-features = false }
1515
fvm_shared = { version = "0.5.1", path = "../shared" }
1616
## num-traits; disabling default features makes it play nice with no_std.
1717
num-traits = { version = "0.2.14", default-features = false }
18-
lazy_static = "1.4.0"
18+
lazy_static = { version = "1.4.0", optional = true }
1919
log = "0.4.14"
2020
thiserror = "1.0.30"
2121
fvm_ipld_encoding = { version = "0.1", path = "../ipld/encoding" }
22+
23+
[features]
24+
default = ["debug"]
25+
debug = ["lazy_static"]

sdk/src/debug.rs

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,64 @@
1-
use lazy_static::lazy_static;
1+
pub use inner::*;
22

3-
use crate::sys;
3+
#[cfg(not(feature = "debug"))]
4+
mod inner {
5+
#[inline(always)]
6+
pub fn init_logging() {}
47

5-
lazy_static! {
6-
/// Lazily memoizes if debug mode is enabled.
7-
static ref DEBUG_ENABLED: bool = unsafe { sys::debug::enabled().unwrap() >= 0 };
8-
}
9-
10-
/// Logs a message on the node.
11-
#[inline]
12-
pub fn log(msg: String) {
13-
unsafe {
14-
sys::debug::log(msg.as_ptr(), msg.len() as u32).unwrap();
8+
#[inline(always)]
9+
pub fn enabled() {
10+
false
1511
}
12+
#[inline(always)]
13+
pub fn log(_: String) {}
1614
}
1715

18-
/// Returns whether debug mode is enabled.
19-
#[inline(always)]
20-
fn enabled() -> bool {
21-
*DEBUG_ENABLED
22-
}
16+
#[cfg(feature = "debug")]
17+
mod inner {
18+
use lazy_static::lazy_static;
2319

24-
/// Logger is a debug-only logger that uses the FVM syscalls.
25-
pub struct Logger;
20+
use crate::sys;
2621

27-
impl log::Log for Logger {
28-
fn enabled(&self, _: &log::Metadata) -> bool {
29-
// TODO: per-level?
30-
enabled()
22+
lazy_static! {
23+
/// Lazily memoizes if debug mode is enabled.
24+
static ref DEBUG_ENABLED: bool = unsafe { sys::debug::enabled().unwrap() >= 0 };
3125
}
3226

33-
fn log(&self, record: &log::Record) {
27+
/// Logs a message on the node.
28+
#[inline]
29+
pub fn log(msg: String) {
30+
unsafe {
31+
sys::debug::log(msg.as_ptr(), msg.len() as u32).unwrap();
32+
}
33+
}
34+
/// Initialize logging if debuggig is enabled.
35+
pub fn init_logging() {
3436
if enabled() {
35-
log(format!("[{}] {}", record.level(), record.args()));
37+
log::set_logger(&Logger).expect("failed to enable logging");
3638
}
3739
}
3840

39-
fn flush(&self) {}
40-
}
41+
/// Returns whether debug mode is enabled.
42+
#[inline(always)]
43+
pub fn enabled() -> bool {
44+
*DEBUG_ENABLED
45+
}
46+
47+
/// Logger is a debug-only logger that uses the FVM syscalls.
48+
struct Logger;
49+
50+
impl log::Log for Logger {
51+
fn enabled(&self, _: &log::Metadata) -> bool {
52+
// TODO: per-level?
53+
enabled()
54+
}
55+
56+
fn log(&self, record: &log::Record) {
57+
if enabled() {
58+
log(format!("[{}] {}", record.level(), record.args()));
59+
}
60+
}
4161

42-
/// Initialize logging if debuggig is enabled.
43-
pub fn init_logging() {
44-
if enabled() {
45-
log::set_logger(&Logger).expect("failed to enable logging");
62+
fn flush(&self) {}
4663
}
4764
}

sdk/src/sys/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub use fvm_shared::sys::TokenAmount;
44

55
pub mod actor;
66
pub mod crypto;
7-
//#[cfg(feature = "debug")]
7+
#[cfg(feature = "debug")]
88
pub mod debug;
99
pub mod gas;
1010
pub mod ipld;

0 commit comments

Comments
 (0)