Fix new log function options for zig

This commit is contained in:
Mitchell Hashimoto
2023-01-26 09:10:09 -08:00
parent f2b59353ab
commit e438539a14
2 changed files with 54 additions and 48 deletions

View File

@ -142,51 +142,53 @@ pub fn tracy_enabled() bool {
return options.tracy_enabled; return options.tracy_enabled;
} }
// Our log level is always at least info in every build mode. pub const std_options = struct {
pub const log_level: std.log.Level = switch (builtin.mode) { // Our log level is always at least info in every build mode.
.Debug => .debug, pub const log_level: std.log.Level = switch (builtin.mode) {
else => .info, .Debug => .debug,
}; else => .info,
};
// The function std.log will call. // The function std.log will call.
pub fn log( pub fn logFn(
comptime level: std.log.Level, comptime level: std.log.Level,
comptime scope: @TypeOf(.EnumLiteral), comptime scope: @TypeOf(.EnumLiteral),
comptime format: []const u8, comptime format: []const u8,
args: anytype, args: anytype,
) void { ) void {
// Stuff we can do before the lock // Stuff we can do before the lock
const level_txt = comptime level.asText(); const level_txt = comptime level.asText();
const prefix = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): "; const prefix = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";
// Lock so we are thread-safe // Lock so we are thread-safe
std.debug.getStderrMutex().lock(); std.debug.getStderrMutex().lock();
defer std.debug.getStderrMutex().unlock(); defer std.debug.getStderrMutex().unlock();
// On Mac, we use unified logging. To view this: // On Mac, we use unified logging. To view this:
// //
// sudo log stream --level debug --predicate 'subsystem=="com.mitchellh.ghostty"' // sudo log stream --level debug --predicate 'subsystem=="com.mitchellh.ghostty"'
// //
if (builtin.os.tag == .macos) { if (builtin.os.tag == .macos) {
// Convert our levels to Mac levels // Convert our levels to Mac levels
const mac_level: macos.os.LogType = switch (level) { const mac_level: macos.os.LogType = switch (level) {
.debug => .debug, .debug => .debug,
.info => .info, .info => .info,
.warn => .err, .warn => .err,
.err => .fault, .err => .fault,
}; };
// Initialize a logger. This is slow to do on every operation // Initialize a logger. This is slow to do on every operation
// but we shouldn't be logging too much. // but we shouldn't be logging too much.
const logger = macos.os.Log.create("com.mitchellh.ghostty", @tagName(scope)); const logger = macos.os.Log.create("com.mitchellh.ghostty", @tagName(scope));
defer logger.release(); defer logger.release();
logger.log(std.heap.c_allocator, mac_level, format, args); 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 { fn glfwErrorCallback(code: glfw.Error, desc: [:0]const u8) void {
std.log.warn("glfw error={} message={s}", .{ code, desc }); std.log.warn("glfw error={} message={s}", .{ code, desc });

View File

@ -4,17 +4,21 @@ const std = @import("std");
const builtin = @import("builtin"); const builtin = @import("builtin");
pub usingnamespace @import("os/wasm.zig"); pub usingnamespace @import("os/wasm.zig");
pub usingnamespace @import("os/wasm/log.zig");
pub usingnamespace @import("font/main.zig"); pub usingnamespace @import("font/main.zig");
pub usingnamespace @import("terminal/main.zig"); pub usingnamespace @import("terminal/main.zig");
pub usingnamespace @import("config.zig").Wasm; pub usingnamespace @import("config.zig").Wasm;
pub usingnamespace @import("App.zig").Wasm; pub usingnamespace @import("App.zig").Wasm;
// Set our log level. We try to get as much logging as possible but in pub const std_options = struct {
// ReleaseSmall mode where we're optimizing for space, we elevate the // Set our log level. We try to get as much logging as possible but in
// log level. // ReleaseSmall mode where we're optimizing for space, we elevate the
pub const log_level: std.log.Level = switch (builtin.mode) { // log level.
.Debug => .debug, pub const log_level: std.log.Level = switch (builtin.mode) {
.ReleaseSmall => .warn, .Debug => .debug,
else => .info, .ReleaseSmall => .warn,
else => .info,
};
// Set our log function
pub const logFn = @import("os/wasm/log.zig").log;
}; };