Skip to content

Commit 0df5658

Browse files
committed
Add a Credman logger
It's compile time stripped unless we enable with `--no-default-features --features logging` during cargo build
1 parent a2b4d82 commit 0df5658

File tree

7 files changed

+109
-1
lines changed

7 files changed

+109
-1
lines changed

matcher-rs/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

matcher-rs/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ crate-type = ["cdylib", "rlib"]
99

1010
[dependencies]
1111
nanoserde = "0.1"
12+
log = "0.4"
13+
14+
[features]
15+
default = ["static_log_off"]
16+
static_log_off = ["log/max_level_off"]
17+
logging = []
1218

1319
[dev-dependencies]
1420

matcher-rs/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ cargo +nightly build \
1212

1313
# 2. Further shrink using wasm-opt (if available)
1414
for wasm in target/wasm32-unknown-unknown/release/*.wasm; do
15-
wasm-opt -Oz --strip-debug --enable-bulk-memory --enable-sign-ext "$wasm" -o "$wasm"
15+
wasm-opt -Oz --strip-debug --enable-bulk-memory --enable-sign-ext --enable-nontrapping-float-to-int "$wasm" -o "$wasm"
1616
done
1717

matcher-rs/src/bin/issuance.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use matcher_rs::{credman::CredmanApiImpl, issuance::issuance_main};
22

33
fn main() {
4+
matcher_rs::logger::init();
45
issuance_main(&mut CredmanApiImpl {}).unwrap();
56
}
67

matcher-rs/src/bindings.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,6 @@ unsafe extern "C" {
139139
set_id: *const c_char,
140140
set_index: i32,
141141
);
142+
pub fn fd_write(fd: i32, iovs_ptr: *const c_void, iovs_len: i32, nwritten_ptr: *mut i32)
143+
-> i32;
142144
}

matcher-rs/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ pub mod bindings;
22
pub mod credman;
33
pub mod issuance;
44
pub mod issuance_matcher;
5+
pub mod logger;
56
pub mod openid4vci;

matcher-rs/src/logger.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
//! WASM Logging implementation using `fd_write`.
2+
//!
3+
//! Logging is stripped by default at compile time to minimize the WASM binary size,
4+
//! as the `log` crate and string formatting (fmt) significantly increase the footprint.
5+
//!
6+
//! To enable logging during development:
7+
//! Use `--no-default-features --features logging` when building.
8+
//!
9+
//! Example:
10+
//! cargo build --release --no-default-features --features logging
11+
12+
#[cfg(feature = "logging")]
13+
use crate::bindings::fd_write;
14+
#[cfg(feature = "logging")]
15+
use log::{Metadata, Record};
16+
#[cfg(feature = "logging")]
17+
use std::os::raw::c_void;
18+
19+
#[cfg(feature = "logging")]
20+
struct WasmLogger;
21+
22+
#[cfg(feature = "logging")]
23+
#[repr(C)]
24+
struct Ciovec {
25+
buf: *const u8,
26+
buf_len: u32,
27+
}
28+
29+
#[cfg(feature = "logging")]
30+
impl log::Log for WasmLogger {
31+
fn enabled(&self, _metadata: &Metadata) -> bool {
32+
true
33+
}
34+
35+
fn log(&self, record: &Record) {
36+
if self.enabled(record.metadata()) {
37+
let msg = format!("{}: {}\n", record.level(), record.args());
38+
let ciovec = Ciovec {
39+
buf: msg.as_ptr(),
40+
buf_len: msg.len() as u32,
41+
};
42+
let mut nwritten = 0;
43+
unsafe {
44+
fd_write(
45+
1, // stdout
46+
&ciovec as *const _ as *const c_void,
47+
1,
48+
&mut nwritten,
49+
);
50+
}
51+
}
52+
}
53+
54+
fn flush(&self) {}
55+
}
56+
57+
#[cfg(feature = "logging")]
58+
static LOGGER: WasmLogger = WasmLogger;
59+
60+
#[cfg(feature = "logging")]
61+
pub fn init() {
62+
log::set_logger(&LOGGER)
63+
.map(|()| log::set_max_level(log::LevelFilter::Trace))
64+
.ok();
65+
66+
std::panic::set_hook(Box::new(|panic_info| {
67+
let msg = panic_info.payload_as_str().unwrap_or("unknown");
68+
let formatted = match panic_info.location() {
69+
Some(loc) => format!("Panic at {}: {}\n", loc, msg),
70+
None => format!("Panic: {}\n", msg),
71+
};
72+
let ciovec = Ciovec {
73+
buf: formatted.as_ptr(),
74+
buf_len: formatted.len() as u32,
75+
};
76+
let mut nwritten = 0;
77+
unsafe {
78+
fd_write(
79+
1, // stdout
80+
&ciovec as *const _ as *const c_void,
81+
1,
82+
&mut nwritten,
83+
);
84+
}
85+
}));
86+
}
87+
88+
#[cfg(not(feature = "logging"))]
89+
pub fn init() {
90+
log::set_max_level(log::LevelFilter::Off);
91+
}

0 commit comments

Comments
 (0)