Skip to content

Commit

Permalink
Wired up logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-F-Bryan committed Jul 16, 2022
1 parent 4b87485 commit f81b5b4
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/wasm-shim/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ crate-type = ["cdylib", "rlib"]
anyhow = "1.0.58"
fj = { git = "https://github.com/hannobraun/Fornjot" }
fj-plugins = { version = "0.1.0", path = "../plugins" }
log = "0.4.17"
wasmtime = "0.38.1"
wit-bindgen-wasmtime = { git = "https://github.com/bytecodealliance/wit-bindgen" }
69 changes: 68 additions & 1 deletion crates/wasm-shim/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//! A wrapper plugin which makes the WebAssembly plugin pointed to by
//! `$WASM_BINARY` and makes it callable using the native ABI.
use std::{
fmt::{self, Display, Formatter},
sync::{Arc, Mutex},
Expand All @@ -8,6 +11,7 @@ use fj_plugins::{Model, PluginMetadata};
use wasmtime::{Engine, Linker, Module, Store};

wit_bindgen_wasmtime::import!("../../wit-files/guest.wit");
wit_bindgen_wasmtime::export!("../../wit-files/host.wit");

fj_plugins::register_plugin!(|host| {
let wasm_binary = std::env::var("WASM_BINARY")
Expand All @@ -18,9 +22,9 @@ fj_plugins::register_plugin!(|host| {

let engine = Engine::default();
let module = Module::new(&engine, &wasm).context("Unable to parse the WebAssembly module")?;

let mut store = Store::new(&engine, State::default());
let mut linker = Linker::new(&engine);
host::add_to_linker(&mut linker, |s: &mut State| &mut s.host)?;
let (guest, _instance) = guest::Guest::instantiate(&mut store, &module, &mut linker, |state| {
&mut state.guest_data
})
Expand Down Expand Up @@ -83,6 +87,69 @@ impl Model for WebAssemblyModel {
#[derive(Default)]
struct State {
guest_data: guest::GuestData,
host: Host,
}

#[derive(Default)]
struct Host;

impl host::Host for Host {
fn log_enabled(&mut self, metadata: host::LogMetadata<'_>) -> bool {
let metadata = log::Metadata::from(metadata);
log::logger().enabled(&metadata)
}

fn log(&mut self, metadata: host::LogMetadata<'_>, payload: host::LogRecord<'_>) {
// Note: we use the `log` crate for logging because `tracing` makes it
// incredibly hard to dynamically emit log messages that retain things
// like the line number and target.
//
// Luckily, tracing provide a compatibility layer where `log` records
// still make their way to a `tracing` subscriber.
//
// See also:
// - https://docs.rs/tracing/latest/tracing/#emitting-log-records
// - https://github.com/tokio-rs/tracing/issues/1047

let host::LogRecord {
message,
module_path,
file,
line,
} = payload;

log::logger().log(
&log::Record::builder()
.metadata(metadata.into())
.args(format_args!("{message}"))
.module_path(module_path)
.file(file)
.line(line)
.build(),
);
}
}

impl<'a> From<host::LogMetadata<'a>> for log::Metadata<'a> {
fn from(m: host::LogMetadata<'a>) -> Self {
let host::LogMetadata { level, target } = m;
log::Metadata::builder()
.level(level.into())
.target(target.into())
.build()
}
}

impl From<host::LogLevel> for log::Level {
fn from(level: host::LogLevel) -> Self {
match level {
host::LogLevel::Error => log::Level::Error,
host::LogLevel::Warn => log::Level::Warn,
host::LogLevel::Info => log::Level::Info,
host::LogLevel::Debug => log::Level::Debug,
host::LogLevel::Trace => log::Level::Trace,
}
}
}

impl Display for guest::Error {
Expand Down
2 changes: 1 addition & 1 deletion wit-files/host.wit
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ record log-record {
message: string,
module-path: option<string>,
file: option<string>,
line: option<string>,
line: option<u32>,
}

log-enabled: func(metadata: log-metadata) -> bool
Expand Down

0 comments on commit f81b5b4

Please sign in to comment.