Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,4 @@ msgpack = ["extism-convert/msgpack"]
protobuf = ["extism-convert/protobuf"]

[workspace]
members = [
".",
"derive"
]
members = [".", "derive"]
28 changes: 3 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#[cfg(target_arch = "wasm32")]
pub use std::arch::wasm32::v128;

mod macros;

pub mod extism;
mod logging;
mod macros;
pub mod memory;
mod to_memory;

Expand All @@ -21,8 +21,8 @@ pub mod http;

pub use anyhow::Error;
pub use extism_convert::*;
pub use extism_convert::{FromBytes, FromBytesOwned, ToBytes};
pub use extism_pdk_derive::{host_fn, plugin_fn, shared_fn};
pub use logging::*;
pub use memory::{ManagedMemory, Memory, MemoryPointer};
pub use to_memory::ToMemory;

Expand All @@ -40,28 +40,6 @@ pub type FnResult<T> = Result<T, WithReturnCode<Error>>;
/// The return type of a `shared_fn`
pub type SharedFnResult<T> = Result<T, Error>;

/// Logging levels
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LogLevel {
Trace,
Debug,
Info,
Warn,
Error,
}

impl LogLevel {
pub const fn to_int(self) -> i32 {
match self {
LogLevel::Trace => 0,
LogLevel::Debug => 1,
LogLevel::Info => 2,
LogLevel::Warn => 3,
LogLevel::Error => 4,
}
}
}

/// Re-export of `serde_json`
pub use serde_json as json;

Expand Down
41 changes: 41 additions & 0 deletions src/logging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use crate::{Error, Memory};
use extism_convert::TracingEvent;

/// Logging levels
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LogLevel {
Trace,
Debug,
Info,
Warn,
Error,
}

impl LogLevel {
pub const fn to_int(self) -> i32 {
match self {
LogLevel::Trace => 0,
LogLevel::Debug => 1,
LogLevel::Info => 2,
LogLevel::Warn => 3,
LogLevel::Error => 4,
}
}
}

/// Log a message string.
pub fn log(level: LogLevel, message: String) -> Result<(), Error> {
let current_level = unsafe { crate::extism::get_log_level() };

if level.to_int() >= current_level && current_level != i32::MAX {
let memory = Memory::from_bytes(&message)?;
memory.log(level);
}

Ok(())
}

/// Log a `tracing` event.
pub fn log_event(level: LogLevel, event: TracingEvent) -> Result<(), Error> {
log(level, serde_json::to_string(&event)?)
}
7 changes: 1 addition & 6 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
#[macro_export]
macro_rules! log {
($lvl:expr, $($arg:tt)+) => {{
let level = unsafe { $crate::extism::get_log_level() };
if $lvl.to_int() >= level && level != i32::MAX {
let fmt = format!($($arg)+);
let memory = $crate::Memory::from_bytes(&fmt).unwrap();
memory.log($lvl)
}
$crate::log($lvl, format!($($arg)+)).unwrap();
}}
}

Expand Down
Loading