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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use crate::HandlerResult;
use log::{Metadata, Record};
use std::sync::{Arc, RwLock};
use wapc_guest::host_call;
use wascc_codec::logging::*;
use wascc_codec::serialize;
pub const CAPID_LOGGING: &str = "wascc:logging";
const ERROR: u32 = 1;
const WARN: u32 = 2;
const INFO: u32 = 3;
const DEBUG: u32 = 4;
const TRACE: u32 = 5;
lazy_static! {
static ref CURRENT_BINDING: Arc<RwLock<String>> =
{ Arc::new(RwLock::new("default".to_string())) };
}
static LOGGER: AutomaticLoggerHostBinding = AutomaticLoggerHostBinding {};
#[allow(dead_code)]
#[doc(hidden)]
pub fn ensure_logger() {
match log::set_logger(&LOGGER) {
Ok(_) => {}
Err(_) => {}
}
log::set_max_level(log::LevelFilter::Trace);
}
pub struct AutomaticLoggerHostBinding {}
impl Default for AutomaticLoggerHostBinding {
fn default() -> Self {
AutomaticLoggerHostBinding {}
}
}
fn set_binding(binding: &str) {
*CURRENT_BINDING.write().unwrap() = binding.to_string();
}
pub fn host(binding: &str) -> AutomaticLoggerHostBinding {
set_binding(binding);
AutomaticLoggerHostBinding {}
}
pub fn default() -> AutomaticLoggerHostBinding {
set_binding("default");
AutomaticLoggerHostBinding {}
}
impl log::Log for AutomaticLoggerHostBinding {
fn enabled(&self, _metadata: &Metadata) -> bool {
true
}
fn log(&self, record: &Record) {
if self.enabled(record.metadata()) {
let l = WriteLogRequest {
level: record.level() as _,
body: format!("{}", record.args()),
};
self._log(l)
}
}
fn flush(&self) {}
}
impl AutomaticLoggerHostBinding {
fn _log(&self, req: WriteLogRequest) {
let _ = host_call(
&CURRENT_BINDING.read().unwrap(),
CAPID_LOGGING,
OP_LOG,
&serialize(req).unwrap(),
);
}
pub fn log(&self, level: u32, body: &str) -> HandlerResult<()> {
let l = WriteLogRequest {
level: level,
body: body.to_string(),
};
let _ = host_call(
&CURRENT_BINDING.read().unwrap(),
CAPID_LOGGING,
OP_LOG,
&serialize(l)?,
);
Ok(())
}
pub fn error(&self, body: &str) -> HandlerResult<()> {
let l = WriteLogRequest {
level: ERROR,
body: body.to_string(),
};
let _ = host_call(
&CURRENT_BINDING.read().unwrap(),
CAPID_LOGGING,
OP_LOG,
&serialize(l)?,
);
Ok(())
}
pub fn warn(&self, body: &str) -> HandlerResult<()> {
let l = WriteLogRequest {
level: WARN,
body: body.to_string(),
};
let _ = host_call(
&CURRENT_BINDING.read().unwrap(),
CAPID_LOGGING,
OP_LOG,
&serialize(l)?,
);
Ok(())
}
pub fn info(&self, body: &str) -> HandlerResult<()> {
let l = WriteLogRequest {
level: INFO,
body: body.to_string(),
};
let _ = host_call(
&CURRENT_BINDING.read().unwrap(),
CAPID_LOGGING,
OP_LOG,
&serialize(l)?,
);
Ok(())
}
pub fn debug(&self, body: &str) -> HandlerResult<()> {
let l = WriteLogRequest {
level: DEBUG,
body: body.to_string(),
};
let _ = host_call(
&CURRENT_BINDING.read().unwrap(),
CAPID_LOGGING,
OP_LOG,
&serialize(l)?,
);
Ok(())
}
pub fn trace(&self, body: &str) -> HandlerResult<()> {
let l = WriteLogRequest {
level: TRACE,
body: body.to_string(),
};
let _ = host_call(
&CURRENT_BINDING.read().unwrap(),
CAPID_LOGGING,
OP_LOG,
&serialize(l)?,
);
Ok(())
}
}