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.
This commit is contained in:
George Joseph
2025-02-14 07:56:55 -07:00
parent 38908e0126
commit 5546fd73c8
2 changed files with 22 additions and 20 deletions

View File

@ -26,19 +26,13 @@ pub const GlobalState = struct {
gpa: ?GPA, gpa: ?GPA,
alloc: std.mem.Allocator, alloc: std.mem.Allocator,
action: ?cli.Action, action: ?cli.Action,
logging: Logging, log_level: std.log.Level,
rlimits: ResourceLimits = .{}, rlimits: ResourceLimits = .{},
/// The app resources directory, equivalent to zig-out/share when we build /// The app resources directory, equivalent to zig-out/share when we build
/// from source. This is null if we can't detect it. /// from source. This is null if we can't detect it.
resources_dir: ?[]const u8, resources_dir: ?[]const u8,
/// Where logging should go
pub const Logging = union(enum) {
disabled: void,
stderr: void,
};
/// Initialize the global state. /// Initialize the global state.
pub fn init(self: *GlobalState) !void { pub fn init(self: *GlobalState) !void {
// const start = try std.time.Instant.now(); // const start = try std.time.Instant.now();
@ -56,7 +50,7 @@ pub const GlobalState = struct {
.gpa = null, .gpa = null,
.alloc = undefined, .alloc = undefined,
.action = null, .action = null,
.logging = .{ .stderr = {} }, .log_level = .warn,
.rlimits = .{}, .rlimits = .{},
.resources_dir = null, .resources_dir = null,
}; };
@ -92,11 +86,11 @@ pub const GlobalState = struct {
// If we have an action executing, we disable logging by default // If we have an action executing, we disable logging by default
// since we write to stderr we don't want logs messing up our // since we write to stderr we don't want logs messing up our
// output. // 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. // For lib mode we always disable stderr logging by default.
if (comptime build_config.app_runtime == .none) { 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 // 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| { if ((try internal_os.getenv(self.alloc, "GHOSTTY_LOG"))) |v| {
defer v.deinit(self.alloc); defer v.deinit(self.alloc);
if (v.value.len > 0) { 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 // Setup our signal handlers before logging
initSignals(); initSignals();

View File

@ -119,6 +119,12 @@ fn logFn(
comptime format: []const u8, comptime format: []const u8,
args: anytype, args: anytype,
) void { ) 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 // 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) ++ "): ";
@ -147,15 +153,9 @@ fn logFn(
logger.log(std.heap.c_allocator, mac_level, format, args); logger.log(std.heap.c_allocator, mac_level, format, args);
} }
switch (state.logging) { // Always try default to send to stderr
.disabled => {}, const stderr = std.io.getStdErr().writer();
nosuspend stderr.print(level_txt ++ prefix ++ format ++ "\n", args) catch return;
.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;
},
}
} }
pub const std_options: std.Options = .{ pub const std_options: std.Options = .{