-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wasmlog.zig
26 lines (22 loc) · 836 Bytes
/
wasmlog.zig
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
// Log WebAssembly Messages from Zig to JavaScript Console
// From https://github.com/daneelsan/zig-wasm-logger/blob/master/JS.zig
const std = @import("std");
const Imports = struct {
extern fn jsConsoleLogWrite(ptr: [*]const u8, len: usize) void;
extern fn jsConsoleLogFlush() void;
};
pub const Console = struct {
pub const Logger = struct {
pub const Error = error{};
pub const Writer = std.io.Writer(void, Error, write);
fn write(_: void, bytes: []const u8) Error!usize {
Imports.jsConsoleLogWrite(bytes.ptr, bytes.len);
return bytes.len;
}
};
const logger = Logger.Writer{ .context = {} };
pub fn log(comptime format: []const u8, args: anytype) void {
logger.print(format, args) catch return;
Imports.jsConsoleLogFlush();
}
};