1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#[cfg(feature = "logging")]
pub mod logging {
use log::{Level, Metadata, Record, SetLoggerError};
// 🌟 Imports the official WASIp2 system logging interfaces
use wasi::logging::logging::{log as wasi_log, Level as WasiLevel};
/// Represents a thread-safe, zero-allocation logger routing Rust `log` macros to WASIp2.
///
/// Intercepts diagnostics generated via standard macro gates (such as [`log::info!`] or
/// [`log::error!`]) and dynamically marshals them into the host's native telemetry pipeline
/// through the [`wasi:logging/logging`] system interface.
struct SololcLogger;
impl log::Log for SololcLogger {
/// Evaluates whether a log record with the provided metadata should be processed.
///
/// Returns `true` universally, deferring severity filtering to the host environment.
fn enabled(&self, _metadata: &Metadata) -> bool {
true
}
/// Forwards a diagnostic [`Record`] directly to the WASIp2 system stream.
///
/// Translates Rust's [`Level`] enum representations into native WASI [`WasiLevel`]
/// components prior to dispatching.
fn log(&self, record: &Record) {
let level = match record.level() {
Level::Error => WasiLevel::Error,
Level::Warn => WasiLevel::Warn,
Level::Info => WasiLevel::Info,
Level::Debug => WasiLevel::Debug,
Level::Trace => WasiLevel::Trace,
};
// Dispatches directly to the host's WASIp2 standard logger
wasi_log(level, record.target(), &format!("{}", record.args()));
}
/// Flushes any buffered log entries.
///
/// Performs no operations (no-op) as the underlying WASIp2 system logging
/// interface is stream-immediate.
fn flush(&self) {}
}
/// Holds the static singleton instance of the active logger.
static LOGGER: SololcLogger = SololcLogger;
/// Initializes the global logger singleton and registers it with the standard `log` library.
///
/// Configures [`LOGGER`] as the primary collector for all telemetry dispatches and
/// applies the specified maximum filter severity.
///
/// # Errors
///
/// Returns a [`SetLoggerError`] if another logging implementation has already been
/// initialized as the global singleton for the executing process.
///
/// # Examples
///
/// ```no_run
/// use sololc_webhook_sdk::logging;
/// use log::LevelFilter;
///
/// # fn run() -> Result<(), log::SetLoggerError> {
/// logging::init(LevelFilter::Info)?;
/// log::info!("WASI-HTTP telemetry initialized successfully.");
/// # Ok(())
/// # }
/// ```
pub fn init(max_level: log::LevelFilter) -> Result<(), SetLoggerError> {
log::set_logger(&LOGGER)?;
log::set_max_level(max_level);
Ok(())
}
}