sim: Allow Rust logging level to control C logging
Change the C logging code, when in the simulator, to query what the rust
logging level is set to. This allows the level of logging from the C
code to be set through the environment. For example
RUST_LOG=bootsim=info cargo run --release runall
will enable logging at the "info" level for all of the C code as well as
the simulator code. The C code's logging can be selected specifically
by using bootsim::api instead of just bootsim in the above.
diff --git a/sim/src/api.rs b/sim/src/api.rs
index ec502a2..21806ce 100644
--- a/sim/src/api.rs
+++ b/sim/src/api.rs
@@ -2,6 +2,7 @@
use flash::{Result, Flash};
use libc;
+use log::LogLevel;
use std::slice;
// This isn't meant to call directly, but by a wrapper.
@@ -35,3 +36,25 @@
},
}
}
+
+/// Called by C code to determine if we should log at this level. Levels are defined in
+/// bootutil/bootutil_log.h. This makes the logging from the C code controlled by bootsim::api, so
+/// for example, it can be enabled with something like:
+/// RUST_LOG=bootsim::api=info cargo run --release runall
+/// or
+/// RUST_LOG=bootsim=info cargo run --release runall
+#[no_mangle]
+pub extern fn sim_log_enabled(level: libc::c_int) -> libc::c_int {
+ let res = match level {
+ 1 => log_enabled!(LogLevel::Error),
+ 2 => log_enabled!(LogLevel::Warn),
+ 3 => log_enabled!(LogLevel::Info),
+ 4 => log_enabled!(LogLevel::Trace),
+ _ => false,
+ };
+ if res {
+ 1
+ } else {
+ 0
+ }
+}