diff options
| author | Can Celik <ogulcancelik@gmail.com> | 2026-04-04 19:22:04 +0300 |
|---|---|---|
| committer | Can Celik <ogulcancelik@gmail.com> | 2026-04-05 01:23:06 +0300 |
| commit | 41dd733ca7123c9ac4bfc6c781697a660e95b052 (patch) | |
| tree | 3fc91815ca139ef2e0b281bba0abda4ef15175f4 /vendor/libghostty-vt/src/os/wasm/log.zig | |
| parent | 55c138263e190a1e79d4b997bfb9397306c0ec1f (diff) | |
feat: finish ghostty pane backend integration
Diffstat (limited to 'vendor/libghostty-vt/src/os/wasm/log.zig')
| -rw-r--r-- | vendor/libghostty-vt/src/os/wasm/log.zig | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/vendor/libghostty-vt/src/os/wasm/log.zig b/vendor/libghostty-vt/src/os/wasm/log.zig new file mode 100644 index 00000000..faa885c6 --- /dev/null +++ b/vendor/libghostty-vt/src/os/wasm/log.zig @@ -0,0 +1,45 @@ +const std = @import("std"); +const wasm = @import("../wasm.zig"); + +// Use the correct implementation +pub const log = Freestanding.log; + +/// Freestanding implementation calls an extern "log" function. +pub const Freestanding = struct { + // The function std.log will call. + pub fn log( + comptime level: std.log.Level, + comptime scope: @TypeOf(.EnumLiteral), + comptime format: []const u8, + args: anytype, + ) void { + // The buffer for putting our log message. We try to use a stack-allocated + // buffer first because we want to avoid allocation. If we are logging + // an error DUE to an OOM, allocating will of course fail and we'd like + // to see the error message so we prefer to use this. + var buf: [2048]u8 = undefined; + + // Build the string + const level_txt = comptime level.asText(); + const prefix = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): "; + const txt = level_txt ++ prefix ++ format; + + // Format. We attempt to use a stack-allocated string first and if that + // fails we'll try to allocate. + var allocated: bool = false; + const str = nosuspend std.fmt.bufPrint(&buf, txt, args) catch str: { + allocated = true; + break :str std.fmt.allocPrint(wasm.alloc, txt, args) catch return; + }; + defer if (allocated) wasm.alloc.free(str); + + // Send it over to the JS side + JS.log(str.ptr, str.len); + } + + // We wrap our externs in this namespace so we can reuse symbols, otherwise + // "log" would collide. + const JS = struct { + extern "env" fn log(ptr: [*]const u8, len: usize) void; + }; +}; |
