From e438539a145b49c85019f600dfa78230411b1125 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 26 Jan 2023 09:10:09 -0800 Subject: [PATCH] Fix new log function options for zig --- src/main.zig | 82 ++++++++++++++++++++++++----------------------- src/main_wasm.zig | 20 +++++++----- 2 files changed, 54 insertions(+), 48 deletions(-) diff --git a/src/main.zig b/src/main.zig index 8a74c0852..89f7e9ef4 100644 --- a/src/main.zig +++ b/src/main.zig @@ -142,51 +142,53 @@ pub fn tracy_enabled() bool { return options.tracy_enabled; } -// Our log level is always at least info in every build mode. -pub const log_level: std.log.Level = switch (builtin.mode) { - .Debug => .debug, - else => .info, -}; +pub const std_options = struct { + // Our log level is always at least info in every build mode. + pub const log_level: std.log.Level = switch (builtin.mode) { + .Debug => .debug, + else => .info, + }; -// 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 { - // Stuff we can do before the lock - const level_txt = comptime level.asText(); - const prefix = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): "; + // The function std.log will call. + pub fn logFn( + comptime level: std.log.Level, + comptime scope: @TypeOf(.EnumLiteral), + comptime format: []const u8, + args: anytype, + ) void { + // Stuff we can do before the lock + const level_txt = comptime level.asText(); + const prefix = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): "; - // Lock so we are thread-safe - std.debug.getStderrMutex().lock(); - defer std.debug.getStderrMutex().unlock(); + // Lock so we are thread-safe + std.debug.getStderrMutex().lock(); + defer std.debug.getStderrMutex().unlock(); - // On Mac, we use unified logging. To view this: - // - // sudo log stream --level debug --predicate 'subsystem=="com.mitchellh.ghostty"' - // - if (builtin.os.tag == .macos) { - // Convert our levels to Mac levels - const mac_level: macos.os.LogType = switch (level) { - .debug => .debug, - .info => .info, - .warn => .err, - .err => .fault, - }; + // On Mac, we use unified logging. To view this: + // + // sudo log stream --level debug --predicate 'subsystem=="com.mitchellh.ghostty"' + // + if (builtin.os.tag == .macos) { + // Convert our levels to Mac levels + const mac_level: macos.os.LogType = switch (level) { + .debug => .debug, + .info => .info, + .warn => .err, + .err => .fault, + }; - // Initialize a logger. This is slow to do on every operation - // but we shouldn't be logging too much. - const logger = macos.os.Log.create("com.mitchellh.ghostty", @tagName(scope)); - defer logger.release(); - logger.log(std.heap.c_allocator, mac_level, format, args); + // Initialize a logger. This is slow to do on every operation + // but we shouldn't be logging too much. + const logger = macos.os.Log.create("com.mitchellh.ghostty", @tagName(scope)); + defer logger.release(); + logger.log(std.heap.c_allocator, mac_level, format, args); + } + + // Always try default to send to stderr + const stderr = std.io.getStdErr().writer(); + nosuspend stderr.print(level_txt ++ prefix ++ format ++ "\n", args) catch return; } - - // Always try default to send to stderr - const stderr = std.io.getStdErr().writer(); - nosuspend stderr.print(level_txt ++ prefix ++ format ++ "\n", args) catch return; -} +}; fn glfwErrorCallback(code: glfw.Error, desc: [:0]const u8) void { std.log.warn("glfw error={} message={s}", .{ code, desc }); diff --git a/src/main_wasm.zig b/src/main_wasm.zig index cacd4f196..b275b4626 100644 --- a/src/main_wasm.zig +++ b/src/main_wasm.zig @@ -4,17 +4,21 @@ const std = @import("std"); const builtin = @import("builtin"); pub usingnamespace @import("os/wasm.zig"); -pub usingnamespace @import("os/wasm/log.zig"); pub usingnamespace @import("font/main.zig"); pub usingnamespace @import("terminal/main.zig"); pub usingnamespace @import("config.zig").Wasm; pub usingnamespace @import("App.zig").Wasm; -// Set our log level. We try to get as much logging as possible but in -// ReleaseSmall mode where we're optimizing for space, we elevate the -// log level. -pub const log_level: std.log.Level = switch (builtin.mode) { - .Debug => .debug, - .ReleaseSmall => .warn, - else => .info, +pub const std_options = struct { + // Set our log level. We try to get as much logging as possible but in + // ReleaseSmall mode where we're optimizing for space, we elevate the + // log level. + pub const log_level: std.log.Level = switch (builtin.mode) { + .Debug => .debug, + .ReleaseSmall => .warn, + else => .info, + }; + + // Set our log function + pub const logFn = @import("os/wasm/log.zig").log; };