From bcd88619c6d34c8c3e3899421565dfd1d6386078 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 15 Sep 2023 12:15:12 -0700 Subject: [PATCH] can enable logging for CLI actions with GHOSTTY_LOG env var --- src/main.zig | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/src/main.zig b/src/main.zig index 3716d698e..936c167d7 100644 --- a/src/main.zig +++ b/src/main.zig @@ -126,14 +126,15 @@ pub const std_options = struct { logger.log(std.heap.c_allocator, mac_level, format, args); } - // If we have an action executing, we don't log to stderr. - // TODO(mitchellh): flag to enable logs - // TODO(mitchellh): flag to send logs to file - if (state.action != null) return; + switch (state.logging) { + .disabled => {}, - // Always try default to send to stderr - 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; + }, + } } }; @@ -147,6 +148,13 @@ pub const GlobalState = struct { alloc: std.mem.Allocator, tracy: if (tracy.enabled) ?tracy.Allocator(null) else void, action: ?cli_action.Action, + logging: Logging, + + /// Where logging should go + pub const Logging = union(enum) { + disabled: void, + stderr: void, + }; pub fn init(self: *GlobalState) !void { // Initialize ourself to nothing so we don't have any extra state. @@ -157,6 +165,7 @@ pub const GlobalState = struct { .alloc = undefined, .tracy = undefined, .action = null, + .logging = .{ .stderr = {} }, }; errdefer self.deinit(); @@ -194,6 +203,22 @@ pub const GlobalState = struct { // We first try to parse any action that we may be executing. self.action = try cli_action.Action.detectCLI(self.alloc); + // 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 = {} }; + + // I don't love the env var name but I don't have it in my heart + // to parse CLI args 3 times (once for actions, once for config, + // maybe once for logging) so for now this is an easy way to do + // this. Env vars are useful for logging too because they are + // easy to set. + if (std.os.getenv("GHOSTTY_LOG")) |v| { + if (v.len > 0) { + self.logging = .{ .stderr = {} }; + } + } + // Output some debug information right away std.log.info("ghostty version={s}", .{build_config.version_string}); std.log.info("runtime={}", .{build_config.app_runtime});