Files
extism/runtime/examples/log_callback.rs
zach 2a24d13c9b refactor!: use tracing crate for logging, add extism_log_callback function (#578)
- Uses `tracing` instead of `log` crate
- Uses `tracing-subscriber` instead of `fern`
- This allows us to automatically capture `log` events using
`tracing-subscriber`
- Breaking: Makes `extism::set_log_file` private and only used through
the C API, Rust users should use `tracing-subscriber` to determine which
filters/levels to log.
- Adds `extism::set_log_callback` function to set a callback that can be
used for custom logging from Rust.
- Adds `bool extism_log_custom(const char *level)` and
`extism_log_drain(void (*fn)(const char *s, size_t length)` to the C API
to enable custom sinks in other SDKs
2023-11-16 10:35:22 -08:00

31 lines
735 B
Rust

use extism::*;
static LOGS: std::sync::Mutex<Vec<String>> = std::sync::Mutex::new(Vec::new());
fn handle_logs(msg: &str) {
LOGS.lock().unwrap().push(msg.to_string())
}
fn main() {
set_log_callback(handle_logs, "extism=trace").unwrap();
let url = Wasm::file("../wasm/code.wasm");
let manifest = Manifest::new([url]);
let mut plugin = PluginBuilder::new(manifest)
.with_wasi(true)
.build()
.unwrap();
for _ in 0..5 {
let res = plugin
.call::<&str, &str>("count_vowels", "Hello, world!")
.unwrap();
tracing::info!("{}", res);
}
println!("Dumping logs");
for line in LOGS.lock().unwrap().iter() {
print!("{}", line);
}
}