refactor(core): avoid debug build warnings without `--dbg-console`
What changed, and why it matters
This commit is a build-system cleanup. It makes sure that debug-only logging code is compiled only when both the 'debug' feature and the 'dbg_console' feature are enabled. Previously, enabling a debug build without the debug console produced compiler warnings because logging functions were defined but never used. There is no user-facing behavior change and no security fix.
No security action required. Treat as a normal build hygiene refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change gates the Rust ‘logging’ module and the MicroPython ‘trezorlog’ module on both feature = "debug" and feature = "dbg_console" / USE_DBG_CONSOLE. It also removes the runtime #[cfg(feature = "dbg_console")] blocks inside the logging functions, since the whole module is now excluded when the console is absent. This eliminates dead-code warnings in non-console debug builds.
Changed components
core/embed/rust/src/micropython/logging.rscore/embed/rust/src/micropython/mod.rscore/embed/rust/librust.hcore/embed/upymod/rustmods.ccore/src/trezor/log.pyInspect captured patch +25 / −38
diff --git a/core/embed/rust/librust.h b/core/embed/rust/librust.h
index 6ed3555f..340f18d5 100644
--- a/core/embed/rust/librust.h
+++ b/core/embed/rust/librust.h
@@ -14,7 +14,9 @@ extern mp_obj_module_t mp_module_trezorble;
#if !PYOPT
mp_obj_t ui_debug_layout_type();
+#ifdef USE_DBG_CONSOLE
extern mp_obj_module_t mp_module_trezorlog;
+#endif
#ifdef TREZOR_EMULATOR
extern mp_obj_module_t mp_module_coveragedata;
diff --git a/core/embed/rust/src/micropython/logging.rs b/core/embed/rust/src/micropython/logging.rs
index 46ca01bc..e485f7d1 100644
--- a/core/embed/rust/src/micropython/logging.rs
+++ b/core/embed/rust/src/micropython/logging.rs
@@ -1,6 +1,5 @@
use crate::micropython::{map::Map, module::Module, obj::Obj, qstr::Qstr};
-#[cfg(feature = "dbg_console")]
use crate::{
error::Error,
micropython::{buffer::StrBuffer, util},
@@ -8,7 +7,6 @@ use crate::{
util::logger::init_rust_logging,
};
-#[cfg(feature = "dbg_console")]
fn _log(level: LogLevel, args: &[Obj], kwargs: &Map) -> Result<Obj, Error> {
let [module, fmt, fmt_args @ ..] = args else {
return Err(Error::TypeError);
@@ -33,59 +31,44 @@ fn _log(level: LogLevel, args: &[Obj], kwargs: &Map) -> Result<Obj, Error> {
}
extern "C" fn py_debug(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj {
- #[cfg(feature = "dbg_console")]
- {
- let block = |args: &[Obj], kwargs: &Map| _log(LogLevel::Debug, args, kwargs);
- unsafe {
- util::try_with_args_and_kwargs(n_args, args, kwargs, block);
- }
+ let block = |args: &[Obj], kwargs: &Map| _log(LogLevel::Debug, args, kwargs);
+ unsafe {
+ util::try_with_args_and_kwargs(n_args, args, kwargs, block);
}
Obj::const_none()
}
extern "C" fn py_info(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj {
- #[cfg(feature = "dbg_console")]
- {
- let block = |args: &[Obj], kwargs: &Map| _log(LogLevel::Info, args, kwargs);
- unsafe {
- util::try_with_args_and_kwargs(n_args, args, kwargs, block);
- }
+ let block = |args: &[Obj], kwargs: &Map| _log(LogLevel::Info, args, kwargs);
+ unsafe {
+ util::try_with_args_and_kwargs(n_args, args, kwargs, block);
}
Obj::const_none()
}
extern "C" fn py_warning(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj {
- #[cfg(feature = "dbg_console")]
- {
- let block = |args: &[Obj], kwargs: &Map| _log(LogLevel::Warn, args, kwargs);
- unsafe {
- util::try_with_args_and_kwargs(n_args, args, kwargs, block);
- }
+ let block = |args: &[Obj], kwargs: &Map| _log(LogLevel::Warn, args, kwargs);
+ unsafe {
+ util::try_with_args_and_kwargs(n_args, args, kwargs, block);
}
Obj::const_none()
}
extern "C" fn py_error(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj {
- #[cfg(feature = "dbg_console")]
- {
- let block = |args: &[Obj], kwargs: &Map| _log(LogLevel::Error, args, kwargs);
- unsafe {
- util::try_with_args_and_kwargs(n_args, args, kwargs, block);
- }
+ let block = |args: &[Obj], kwargs: &Map| _log(LogLevel::Error, args, kwargs);
+ unsafe {
+ util::try_with_args_and_kwargs(n_args, args, kwargs, block);
}
Obj::const_none()
}
extern "C" fn py_init(level: Obj) -> Obj {
- #[cfg(feature = "dbg_console")]
- {
- let block = || {
- init_rust_logging(level.try_into()?);
- Ok(())
- };
- unsafe {
- util::try_or_raise(block);
- }
+ let block = || {
+ init_rust_logging(level.try_into()?);
+ Ok(())
+ };
+ unsafe {
+ util::try_or_raise(block);
}
Obj::const_none()
}
diff --git a/core/embed/rust/src/micropython/mod.rs b/core/embed/rust/src/micropython/mod.rs
index 9953d09b..b6ec68c6 100644
--- a/core/embed/rust/src/micropython/mod.rs
+++ b/core/embed/rust/src/micropython/mod.rs
@@ -19,7 +19,7 @@ pub mod simple_type;
pub mod typ;
pub mod util;
-#[cfg(feature = "debug")]
+#[cfg(all(feature = "debug", feature = "dbg_console"))]
pub mod logging;
#[cfg(test)]
diff --git a/core/embed/upymod/rustmods.c b/core/embed/upymod/rustmods.c
index e0659a68..111fd5c0 100644
--- a/core/embed/upymod/rustmods.c
+++ b/core/embed/upymod/rustmods.c
@@ -40,6 +40,6 @@ MP_REGISTER_MODULE(MP_QSTR_trezorble, mp_module_trezorble);
MP_REGISTER_MODULE(MP_QSTR_coveragedata, mp_module_coveragedata);
#endif
-#if !PYOPT
+#if !PYOPT && defined(USE_DBG_CONSOLE)
MP_REGISTER_MODULE(MP_QSTR_trezorlog, mp_module_trezorlog);
#endif
diff --git a/core/src/trezor/log.py b/core/src/trezor/log.py
index 308fc311..461e9316 100644
--- a/core/src/trezor/log.py
+++ b/core/src/trezor/log.py
@@ -1,6 +1,8 @@
import sys
from typing import TYPE_CHECKING
+from . import utils
+
if TYPE_CHECKING:
from trezorio import WireInterface
from typing import Any
@@ -10,7 +12,7 @@ def _no_op(name: str, msg: str, *args: Any, iface: WireInterface | None = None)
return None
-if __debug__:
+if __debug__ and utils.USE_DBG_CONSOLE:
from trezorlog import debug, error, info, init, warning # noqa: F401
_levels = [debug, info, warning, error]
Why this scored 12/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.