diff --git a/Sources/AppDelegate.swift b/Sources/AppDelegate.swift index 2417128d52e7..b9ce024b7cfe 100644 --- a/Sources/AppDelegate.swift +++ b/Sources/AppDelegate.swift @@ -866,6 +866,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent private var browserAddressBarBlurObserver: NSObjectProtocol? private var browserWebViewFirstResponderObserver: NSObjectProtocol? let updateLog = UpdateLogStore() + let focusLog = FocusLogStore() private lazy var updateController = UpdateController(log: updateLog) private lazy var titlebarAccessoryController = UpdateTitlebarAccessoryController(updateLog: updateLog) private let windowDecorationsController = WindowDecorationsController() @@ -1123,7 +1124,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent let mainWindowNumber = NSApp.mainWindow?.windowNumber ?? -1 let ws = workspaceId.map { String($0.uuidString.prefix(8)) } ?? "nil" let wd = workingDirectory.map { String($0.prefix(120)) } ?? "-" - FocusLogStore.shared.append( + focusLog.append( "cmdn.route phase=\(phase) src=\(source) reason=\(reason) eventWin=\(eventWindowNumber) eventNum=\(eventNumber) keyCode=\(eventKeyCode) chars=\(eventChars) keyWin=\(keyWindowNumber) mainWin=\(mainWindowNumber) activeTM=\(pointerString(tabManager)) chosen={\(summarizeContextForWorkspaceRouting(chosenContext))} ws=\(ws) wd=\(wd) contexts=[\(summarizeAllContextsForWorkspaceRouting())]" ) } @@ -7817,7 +7818,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent private func synchronizeShortcutRoutingContext(event: NSEvent) -> Bool { guard let context = preferredMainWindowContextForShortcutRouting(event: event) else { #if DEBUG - FocusLogStore.shared.append( + focusLog.append( "shortcut.route reason=no_context_no_fallback eventWin=\(event.windowNumber) keyCode=\(event.keyCode)" ) #endif @@ -7841,7 +7842,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent } #if DEBUG - FocusLogStore.shared.append( + focusLog.append( "shortcut.route reason=sync activeTM=\(pointerString(tabManager)) chosen={\(summarizeContextForWorkspaceRouting(context))}" ) #endif @@ -8590,12 +8591,12 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent pasteboard.setString(payload, forType: .string) } @objc func copyFocusLogs(_ sender: Any?) { - let logText = FocusLogStore.shared.snapshot() + let logText = focusLog.snapshot() let payload: String if logText.isEmpty { - payload = "No focus logs captured.\nLog file: \(FocusLogStore.shared.logPath())" + payload = "No focus logs captured.\nLog file: \(focusLog.logPath())" } else { - payload = logText + "\nLog file: \(FocusLogStore.shared.logPath())" + payload = logText + "\nLog file: \(focusLog.logPath())" } let pasteboard = NSPasteboard.general pasteboard.clearContents() diff --git a/Sources/GhosttyTerminalView.swift b/Sources/GhosttyTerminalView.swift index f8c664a7a319..80a09b63bb1c 100644 --- a/Sources/GhosttyTerminalView.swift +++ b/Sources/GhosttyTerminalView.swift @@ -7488,8 +7488,10 @@ class GhosttyNSView: NSView, NSUserInterfaceValidations { fileprivate static func focusLog(_ message: String) { guard focusDebugEnabled else { return } - FocusLogStore.shared.append(message) + AppDelegate.shared?.focusLog.append(message) + #if DEBUG NSLog("[FOCUSDBG] %@", message) + #endif } weak var terminalSurface: TerminalSurface? diff --git a/Sources/Update/UpdateLogStore.swift b/Sources/Update/UpdateLogStore.swift index 6611a9800af8..fa5189ae4fed 100644 --- a/Sources/Update/UpdateLogStore.swift +++ b/Sources/Update/UpdateLogStore.swift @@ -66,16 +66,17 @@ final class UpdateLogStore: UpdateLogging, @unchecked Sendable { } } -final class FocusLogStore { - static let shared = FocusLogStore() - +// @unchecked Sendable: all mutable state (`entries`) is confined to the serial `queue`; the other +// stored properties are immutable. Owned and injected by `AppDelegate` (see `AppDelegate.focusLog`) +// rather than self-vending a global, so its lifecycle has a single composition root. +final class FocusLogStore: @unchecked Sendable { private let queue = DispatchQueue(label: "cmux.focus.log") private var entries: [String] = [] private let maxEntries = 400 private let logURL: URL private let formatter: ISO8601DateFormatter - private init() { + init() { formatter = ISO8601DateFormatter() formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds] let logsDir = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first diff --git a/Sources/Workspace.swift b/Sources/Workspace.swift index 1690686dac1d..5f252c99aae2 100644 --- a/Sources/Workspace.swift +++ b/Sources/Workspace.swift @@ -16043,7 +16043,7 @@ final class Workspace: Identifiable, ObservableObject { let pane = bonsplitController.focusedPaneId?.id.uuidString.prefix(5) ?? "nil" let triggerLabel = trigger == .terminalFirstResponder ? "firstResponder" : "standard" cmuxDebugLog("focus.panel panel=\(panelId.uuidString.prefix(5)) pane=\(pane) trigger=\(triggerLabel)") - FocusLogStore.shared.append( + AppDelegate.shared?.focusLog.append( "Workspace.focusPanel panelId=\(panelId.uuidString) focusedPane=\(pane) trigger=\(triggerLabel)" ) #endif @@ -18661,7 +18661,7 @@ extension Workspace: BonsplitDelegate { // When a pane is focused, focus its selected tab's panel guard let tab = controller.selectedTab(inPane: pane) else { return } #if DEBUG - FocusLogStore.shared.append( + AppDelegate.shared?.focusLog.append( "Workspace.didFocusPane paneId=\(pane.id.uuidString) tabId=\(tab.id) focusedPane=\(controller.focusedPaneId?.id.uuidString ?? "nil")" ) #endif