1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#![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) {}
}