wasm_runner_sdk/
log.rs

1//! Logging utilities for WASM modules.
2
3use crate::abi;
4
5/// Log level enum.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum Level {
8    Info,
9    Warn,
10    Error,
11}
12
13impl Level {
14    fn as_i32(self) -> i32 {
15        match self {
16            Level::Info => abi::LOG_INFO,
17            Level::Warn => abi::LOG_WARN,
18            Level::Error => abi::LOG_ERROR,
19        }
20    }
21}
22
23/// Logs a message at the given level.
24pub fn log(level: Level, message: &str) {
25    unsafe {
26        abi::log(level.as_i32(), message.as_ptr(), message.len() as i32);
27    }
28}
29
30/// Logs an info message.
31pub fn info(message: &str) {
32    log(Level::Info, message);
33}
34
35/// Logs a warning message.
36pub fn warn(message: &str) {
37    log(Level::Warn, message);
38}
39
40/// Logs an error message.
41pub fn error(message: &str) {
42    log(Level::Error, message);
43}
44
45/// Logs a formatted info message.
46#[macro_export]
47macro_rules! log_info {
48    ($($arg:tt)*) => {
49        $crate::log::info(&format!($($arg)*))
50    };
51}
52
53/// Logs a formatted warning message.
54#[macro_export]
55macro_rules! log_warn {
56    ($($arg:tt)*) => {
57        $crate::log::warn(&format!($($arg)*))
58    };
59}
60
61/// Logs a formatted error message.
62#[macro_export]
63macro_rules! log_error {
64    ($($arg:tt)*) => {
65        $crate::log::error(&format!($($arg)*))
66    };
67}