From 5546fd73c81ca3937b00d383902abdb83f171244 Mon Sep 17 00:00:00 2001 From: George Joseph Date: Fri, 14 Feb 2025 07:56:55 -0700 Subject: [PATCH] Change default log level to warning. Ghostty's default log level is now "warning" unless one of the following conditions is met... * A "+action" is being run in which case it's "error". * There's no app_runtime (lib mode) in which case it's "error". * The GHOSTTY_LOG environment variable isn't empty in which case it's "info". * Ghostty was started from a desktop/launcher icon in which case it's "info". * A debug build is being run in which case it's "debug". The conditons are evaluated from top to bottom and the last one met wins. --- src/global.zig | 24 +++++++++++++----------- src/main_ghostty.zig | 18 +++++++++--------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/global.zig b/src/global.zig index d5a7af630..a22483fca 100644 --- a/src/global.zig +++ b/src/global.zig @@ -26,19 +26,13 @@ pub const GlobalState = struct { gpa: ?GPA, alloc: std.mem.Allocator, action: ?cli.Action, - logging: Logging, + log_level: std.log.Level, rlimits: ResourceLimits = .{}, /// The app resources directory, equivalent to zig-out/share when we build /// from source. This is null if we can't detect it. resources_dir: ?[]const u8, - /// Where logging should go - pub const Logging = union(enum) { - disabled: void, - stderr: void, - }; - /// Initialize the global state. pub fn init(self: *GlobalState) !void { // const start = try std.time.Instant.now(); @@ -56,7 +50,7 @@ pub const GlobalState = struct { .gpa = null, .alloc = undefined, .action = null, - .logging = .{ .stderr = {} }, + .log_level = .warn, .rlimits = .{}, .resources_dir = null, }; @@ -92,11 +86,11 @@ pub const GlobalState = struct { // If we have an action executing, we disable logging by default // since we write to stderr we don't want logs messing up our // output. - if (self.action != null) self.logging = .{ .disabled = {} }; + if (self.action != null) self.log_level = .err; // For lib mode we always disable stderr logging by default. if (comptime build_config.app_runtime == .none) { - self.logging = .{ .disabled = {} }; + self.log_level = .err; } // I don't love the env var name but I don't have it in my heart @@ -107,10 +101,18 @@ pub const GlobalState = struct { if ((try internal_os.getenv(self.alloc, "GHOSTTY_LOG"))) |v| { defer v.deinit(self.alloc); if (v.value.len > 0) { - self.logging = .{ .stderr = {} }; + self.log_level = .info; } } + if (internal_os.launchedFromDesktop()) { + self.log_level = .info; + } + + if (builtin.mode == .Debug) { + self.log_level = .debug; + } + // Setup our signal handlers before logging initSignals(); diff --git a/src/main_ghostty.zig b/src/main_ghostty.zig index 9efe8d9b0..d485e88dc 100644 --- a/src/main_ghostty.zig +++ b/src/main_ghostty.zig @@ -119,6 +119,12 @@ fn logFn( comptime format: []const u8, args: anytype, ) void { + + // If we're not logging messages at this level, we can just stop now. + if (@intFromEnum(level) > @intFromEnum(state.log_level)) { + return; + } + // Stuff we can do before the lock const level_txt = comptime level.asText(); const prefix = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): "; @@ -147,15 +153,9 @@ fn logFn( logger.log(std.heap.c_allocator, mac_level, format, args); } - switch (state.logging) { - .disabled => {}, - - .stderr => { - // 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; } pub const std_options: std.Options = .{