#![doc(html_logo_url = "https://avatars2.githubusercontent.com/u/52050279?s=200&v=4")]
mod generated;
#[allow(unused_imports)]
pub use generated::*;
pub const OP_LOG: &str = "WriteLog";
#[cfg(feature = "guest")]
#[doc(hidden)]
static LOG_LEVELS: [&str; 5] = ["error", "warn", "info", "debug", "trace"];
#[cfg(feature = "guest")]
impl Host {
pub fn write_log(&self, target: &str, level: &str, text: &str) -> HandlerResult<()> {
let log_level = if LOG_LEVELS.contains(&level.to_ascii_lowercase().as_str()) {
level
} else {
"info"
};
self._write_log(target.to_string(), log_level.to_string(), text.to_string())
}
}
#[cfg(feature = "guest")]
use lazy_static::lazy_static;
#[cfg(feature = "guest")]
use log::{Metadata, Record};
#[cfg(feature = "guest")]
use std::sync::{Arc, RwLock};
#[cfg(feature = "guest")]
use wapc_guest::HandlerResult;
#[cfg(feature = "guest")]
lazy_static! {
static ref CURRENT_BINDING: Arc<RwLock<String>> = Arc::new(RwLock::new("default".to_string()));
}
#[cfg(feature = "guest")]
static LOGGER: Host = Host {};
#[cfg(feature = "guest")]
pub fn enable_macros() {
if log::set_logger(&LOGGER).is_ok() {};
log::set_max_level(log::LevelFilter::Trace);
}
#[cfg(feature = "guest")]
fn set_binding(binding: &str) {
*CURRENT_BINDING.write().unwrap() = binding.to_string();
}
#[cfg(feature = "guest")]
impl log::Log for Host {
fn enabled(&self, _metadata: &Metadata) -> bool {
true
}
fn log(&self, record: &Record) {
use log::Level::*;
let level = match record.level() {
Error => "error",
Warn => "warn",
Info => "info",
Debug => "debug",
Trace => "trace",
};
let _ = self._write_log(
record.target().to_string(),
level.to_string(),
format!("{}", record.args()),
);
}
fn flush(&self) {}
}