deno.land / x / wasm@wasmer-sdk-v0.6.0 / src / logging.rs

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
use std::io::{ErrorKind, Write};
use tokio::sync::mpsc;use tracing_subscriber::{ fmt::{format::FmtSpan, MakeWriter}, EnvFilter,};use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
/// Initialize the logger used by `@wasmer/wasix`.////// This function can only be called once. Subsequent calls will raise an/// exception.////// ## Filtering Logs////// The `filter` string can be used to tweak logging verbosity, both globally/// or on a per-module basis, and follows [the `$RUST_LOG` format][format].////// Some examples:/// - `off` - turn off all logs/// - `error`, `warn`, `info`, `debug`, `trace` - set the global log level/// - `wasmer_wasix` - enable logs for `wasmer_wasix`/// - `info,wasmer_js::package_loader=trace` - set the global log level to/// `info` and set `wasmer_js::package_loader` to `trace`/// - `wasmer_js=debug/flush` - turns on debug logging for/// `wasmer_js` where the log message includes `flush`/// - `warn,wasmer=info,wasmer_wasix::syscalls::wasi=trace` - directives can be/// mixed arbitrarily////// When no `filter` string is provided, a useful default will be used.////// [format]: https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#directives#[wasm_bindgen(js_name = "initializeLogger")]pub fn initialize_logger(filter: Option<String>) -> Result<(), crate::utils::Error> { let max_level = tracing::level_filters::STATIC_MAX_LEVEL .into_level() .unwrap_or(tracing::Level::ERROR);
let filter = EnvFilter::builder() .with_regex(false) .with_default_directive(max_level.into()) .parse_lossy(filter.unwrap_or_else(|| crate::DEFAULT_RUST_LOG.join(",")));
tracing_subscriber::fmt::fmt() .with_writer(ConsoleLogger::spawn()) .with_env_filter(filter) .with_span_events(FmtSpan::CLOSE) .without_time() .try_init() .map_err(|e| anyhow::anyhow!(e))?;
Ok(())}
/// A [`std::io::Write`] implementation which will pass all messages to the main/// thread for logging with [`web_sys::console`].////// This is useful when using Web Workers for concurrency because their/// `console.log()` output isn't normally captured by test runners.#[derive(Debug)]struct ConsoleLogger { buffer: Vec<u8>, sender: mpsc::UnboundedSender<String>,}
impl ConsoleLogger { fn spawn() -> impl for<'w> MakeWriter<'w> + 'static { let (sender, mut receiver) = mpsc::unbounded_channel();
wasm_bindgen_futures::spawn_local(async move { while let Some(msg) = receiver.recv().await { let js_string = JsValue::from(msg); web_sys::console::log_1(&js_string); } });
move || ConsoleLogger { buffer: Vec::new(), sender: sender.clone(), } }}
impl Write for ConsoleLogger { fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { self.buffer.extend(buf); Ok(buf.len()) }
fn flush(&mut self) -> std::io::Result<()> { let buffer = std::mem::take(&mut self.buffer);
let text = String::from_utf8(buffer) .map_err(|e| std::io::Error::new(ErrorKind::InvalidInput, e))?;
self.sender .send(text) .map_err(|e| std::io::Error::new(ErrorKind::BrokenPipe, e))?;
Ok(()) }}
impl Drop for ConsoleLogger { fn drop(&mut self) { if !self.buffer.is_empty() { let _ = self.flush(); } }}
wasm

Version Info

Tagged at
4 months ago