Skip to content

Commit

Permalink
Update logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanbaird committed Sep 22, 2024
1 parent 2996468 commit 361144b
Show file tree
Hide file tree
Showing 23 changed files with 174 additions and 188 deletions.
23 changes: 11 additions & 12 deletions Ice/Bridging/Bridging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//

import Cocoa
import OSLog

/// A namespace for bridged functionality.
enum Bridging { }
Expand All @@ -25,7 +24,7 @@ extension Bridging {
value as CFTypeRef
)
if result != .success {
logError(to: .bridging, "CGSSetConnectionProperty failed with error \(result.logString)")
Logger.bridging.error("CGSSetConnectionProperty failed with error \(result.logString)")
}
}

Expand All @@ -42,7 +41,7 @@ extension Bridging {
&value
)
if result != .success {
logError(to: .bridging, "CGSCopyConnectionProperty failed with error \(result.logString)")
Logger.bridging.error("CGSCopyConnectionProperty failed with error \(result.logString)")
}
return value?.takeRetainedValue()
}
Expand All @@ -60,7 +59,7 @@ extension Bridging {
var rect = CGRect.zero
let result = CGSGetScreenRectForWindow(CGSMainConnectionID(), windowID, &rect)
guard result == .success else {
logError(to: .bridging, "CGSGetScreenRectForWindow failed with error \(result.logString)")
Logger.bridging.error("CGSGetScreenRectForWindow failed with error \(result.logString)")
return nil
}
return rect
Expand All @@ -73,7 +72,7 @@ extension Bridging {
var count: Int32 = 0
let result = CGSGetWindowCount(CGSMainConnectionID(), 0, &count)
if result != .success {
logError(to: .bridging, "CGSGetWindowCount failed with error \(result.logString)")
Logger.bridging.error("CGSGetWindowCount failed with error \(result.logString)")
}
return Int(count)
}
Expand All @@ -82,7 +81,7 @@ extension Bridging {
var count: Int32 = 0
let result = CGSGetOnScreenWindowCount(CGSMainConnectionID(), 0, &count)
if result != .success {
logError(to: .bridging, "CGSGetOnScreenWindowCount failed with error \(result.logString)")
Logger.bridging.error("CGSGetOnScreenWindowCount failed with error \(result.logString)")
}
return Int(count)
}
Expand All @@ -99,7 +98,7 @@ extension Bridging {
&realCount
)
guard result == .success else {
logError(to: .bridging, "CGSGetWindowList failed with error \(result.logString)")
Logger.bridging.error("CGSGetWindowList failed with error \(result.logString)")
return []
}
return [CGWindowID](list[..<Int(realCount)])
Expand All @@ -117,7 +116,7 @@ extension Bridging {
&realCount
)
guard result == .success else {
logError(to: .bridging, "CGSGetOnScreenWindowList failed with error \(result.logString)")
Logger.bridging.error("CGSGetOnScreenWindowList failed with error \(result.logString)")
return []
}
return [CGWindowID](list[..<Int(realCount)])
Expand All @@ -135,7 +134,7 @@ extension Bridging {
&realCount
)
guard result == .success else {
logError(to: .bridging, "CGSGetProcessMenuBarWindowList failed with error \(result.logString)")
Logger.bridging.error("CGSGetProcessMenuBarWindowList failed with error \(result.logString)")
return []
}
return [CGWindowID](list[..<Int(realCount)])
Expand Down Expand Up @@ -219,11 +218,11 @@ extension Bridging {
case .visibleSpaces: .allVisibleSpaces
}
guard let spaces = CGSCopySpacesForWindows(CGSMainConnectionID(), mask, [windowID] as CFArray) else {
logError(to: .bridging, "CGSCopySpacesForWindows failed")
Logger.bridging.error("CGSCopySpacesForWindows failed")
return []
}
guard let spaceIDs = spaces.takeRetainedValue() as? [CGSSpaceID] else {
logError(to: .bridging, "CGSCopySpacesForWindows returned array of unexpected type")
Logger.bridging.error("CGSCopySpacesForWindows returned array of unexpected type")
return []
}
return spaceIDs
Expand Down Expand Up @@ -262,7 +261,7 @@ extension Bridging {
var psn = ProcessSerialNumber()
let result = GetProcessForPID(pid, &psn)
guard result == noErr else {
logError(to: .bridging, "GetProcessForPID failed with error \(result)")
Logger.bridging.error("GetProcessForPID failed with error \(result)")
return .unknown
}
if CGSEventIsAppUnresponsive(CGSMainConnectionID(), &psn) {
Expand Down
3 changes: 1 addition & 2 deletions Ice/ControlItem/ControlItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import Cocoa
import Combine
import OSLog

/// A status item that controls a section in the menu bar.
@MainActor
Expand Down Expand Up @@ -548,7 +547,7 @@ final class ControlItem {
/// Assigns the app state to the control item.
func assignAppState(_ appState: AppState) {
guard self.appState == nil else {
logWarning(to: .controlItem, "Multiple attempts made to assign app state")
Logger.controlItem.warning("Multiple attempts made to assign app state")
return
}
self.appState = appState
Expand Down
5 changes: 2 additions & 3 deletions Ice/Events/EventManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import Cocoa
import Combine
import OSLog

/// Manager for the various event monitors maintained by the app.
@MainActor
Expand Down Expand Up @@ -237,7 +236,7 @@ extension EventManager {
// If all the above checks have passed, hide.
shownSection.hide()
} catch {
logError(to: .eventManager, "ERROR: \(error)")
Logger.eventManager.error("ERROR: \(error)")
}
}
}
Expand Down Expand Up @@ -385,7 +384,7 @@ extension EventManager {
hiddenSection.hide()
}
} catch {
logError(to: .eventManager, "ERROR: \(error)")
Logger.eventManager.error("ERROR: \(error)")
}
}
}
Expand Down
8 changes: 1 addition & 7 deletions Ice/Hotkeys/Hotkey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//

import Combine
import OSLog

/// A combination of a key and modifiers that can be used to
/// trigger actions on system-wide key-up or key-down events.
Expand Down Expand Up @@ -91,7 +90,7 @@ extension Hotkey {
return
}
guard let appState else {
logError(to: .hotkey, "Error invalidating hotkey: Missing AppState")
Logger.error("Error invalidating hotkey: Missing AppState")
return
}
defer {
Expand Down Expand Up @@ -141,8 +140,3 @@ extension Hotkey: Hashable {
hasher.combine(action)
}
}

// MARK: - Logger
private extension Logger {
static let hotkey = Logger(category: "Hotkey")
}
17 changes: 8 additions & 9 deletions Ice/Hotkeys/HotkeyRegistry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import Carbon.HIToolbox
import Cocoa
import Combine
import OSLog

/// An object that manages the registration, storage, and unregistration of hotkeys.
final class HotkeyRegistry {
Expand Down Expand Up @@ -130,21 +129,21 @@ final class HotkeyRegistry {
}

guard let keyCombination = hotkey.keyCombination else {
logError(to: .hotkeyRegistry, "Hotkey does not have a valid key combination")
Logger.hotkeyRegistry.error("Hotkey does not have a valid key combination")
return nil
}

var status = installIfNeeded()

guard status == noErr else {
logError(to: .hotkeyRegistry, "Hotkey event handler installation failed with status \(status)")
Logger.hotkeyRegistry.error("Hotkey event handler installation failed with status \(status)")
return nil
}

let id = Context.currentID

guard registrations[id] == nil else {
logError(to: .hotkeyRegistry, "Hotkey already registered for id \(id)")
Logger.hotkeyRegistry.error("Hotkey already registered for id \(id)")
return nil
}

Expand All @@ -160,12 +159,12 @@ final class HotkeyRegistry {
)

guard status == noErr else {
logError(to: .hotkeyRegistry, "Hotkey registration failed with status \(status)")
Logger.hotkeyRegistry.error("Hotkey registration failed with status \(status)")
return nil
}

guard let hotKeyRef else {
logError(to: .hotkeyRegistry, "Hotkey registration failed due to invalid EventHotKeyRef")
Logger.hotkeyRegistry.error("Hotkey registration failed due to invalid EventHotKeyRef")
return nil
}

Expand All @@ -186,12 +185,12 @@ final class HotkeyRegistry {
/// its registration in an inactive state.
private func retainedUnregister(_ id: UInt32) {
guard let registration = registrations[id] else {
logError(to: .hotkeyRegistry, "No registered key combination for id \(id)")
Logger.hotkeyRegistry.error("No registered key combination for id \(id)")
return
}
let status = UnregisterEventHotKey(registration.hotKeyRef)
guard status == noErr else {
logError(to: .hotkeyRegistry, "Hotkey unregistration failed with status \(status)")
Logger.hotkeyRegistry.error("Hotkey unregistration failed with status \(status)")
return
}
registration.hotKeyRef = nil
Expand Down Expand Up @@ -237,7 +236,7 @@ final class HotkeyRegistry {
let hotKeyRef
else {
registrations.removeValue(forKey: registration.hotKeyID.id)
logError(to: .hotkeyRegistry, "Hotkey registration failed with status \(status)")
Logger.hotkeyRegistry.error("Hotkey registration failed with status \(status)")
continue
}

Expand Down
10 changes: 2 additions & 8 deletions Ice/Hotkeys/KeyCombination.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import Carbon.HIToolbox
import Cocoa
import OSLog

struct KeyCombination: Hashable {
let key: KeyCode
Expand All @@ -32,11 +31,11 @@ private func getSystemReservedKeyCombinations() -> [KeyCombination] {
let status = CopySymbolicHotKeys(&symbolicHotkeys)

guard status == noErr else {
logError(to: .keyCombination, "CopySymbolicHotKeys returned invalid status: \(status)")
Logger.error("CopySymbolicHotKeys returned invalid status: \(status)")
return []
}
guard let reservedHotkeys = symbolicHotkeys?.takeRetainedValue() as? [[String: Any]] else {
logError(to: .keyCombination, "Failed to serialize symbolic hotkeys")
Logger.error("Failed to serialize symbolic hotkeys")
return []
}

Expand Down Expand Up @@ -84,8 +83,3 @@ extension KeyCombination: Codable {
try container.encode(modifiers.rawValue)
}
}

// MARK: - Logger
private extension Logger {
static let keyCombination = Logger(category: "KeyCombination")
}
11 changes: 5 additions & 6 deletions Ice/Main/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Ice
//

import OSLog
import SwiftUI

@MainActor
Expand All @@ -14,7 +13,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate {

func applicationWillFinishLaunching(_ notification: Notification) {
guard let appState else {
logWarning(to: .appDelegate, "Missing app state in applicationWillFinishLaunching")
Logger.appDelegate.warning("Missing app state in applicationWillFinishLaunching")
return
}

Expand All @@ -30,7 +29,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate {

func applicationDidFinishLaunching(_ notification: Notification) {
guard let appState else {
logWarning(to: .appDelegate, "Missing app state in applicationDidFinishLaunching")
Logger.appDelegate.warning("Missing app state in applicationDidFinishLaunching")
return
}

Expand All @@ -57,7 +56,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
permissionsWindow.center()
permissionsWindow.makeKeyAndOrderFront(nil)
} else {
logError(to: .appDelegate, "Failed to open permissions window")
Logger.appDelegate.error("Failed to open permissions window")
}
}
}
Expand All @@ -78,7 +77,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
/// Assigns the app state to the delegate.
func assignAppState(_ appState: AppState) {
guard self.appState == nil else {
logWarning(to: .appDelegate, "Multiple attempts made to assign app state")
Logger.appDelegate.warning("Multiple attempts made to assign app state")
return
}
self.appState = appState
Expand All @@ -90,7 +89,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
let appState,
let settingsWindow = appState.settingsWindow
else {
logError(to: .appDelegate, "Failed to open settings window")
Logger.appDelegate.error("Failed to open settings window")
return
}
// Small delay makes this more reliable.
Expand Down
7 changes: 3 additions & 4 deletions Ice/Main/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//

import Combine
import OSLog
import SwiftUI

/// The model for app-wide state.
Expand Down Expand Up @@ -127,7 +126,7 @@ final class AppState: ObservableObject {
}
.store(in: &c)
} else {
logWarning(to: .appState, "No settings window!")
Logger.appState.warning("No settings window!")
}

Publishers.Merge(
Expand Down Expand Up @@ -185,7 +184,7 @@ final class AppState: ObservableObject {
/// Assigns the app delegate to the app state.
func assignAppDelegate(_ appDelegate: AppDelegate) {
guard self.appDelegate == nil else {
logWarning(to: .appState, "Multiple attempts made to assign app delegate")
Logger.appState.warning("Multiple attempts made to assign app delegate")
return
}
self.appDelegate = appDelegate
Expand All @@ -212,7 +211,7 @@ final class AppState: ObservableObject {
activate()
} else {
Context.hasActivated[self] = true
logDebug(to: .appState, "First time activating app, so going through Dock")
Logger.appState.debug("First time activating app, so going through Dock")
// Hack to make sure the app properly activates for the first time.
NSRunningApplication.runningApplications(withBundleIdentifier: "com.apple.dock").first?.activate()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
Expand Down
5 changes: 2 additions & 3 deletions Ice/MenuBar/Appearance/MenuBarAppearanceManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import Cocoa
import Combine
import OSLog

/// A manager for the appearance of the menu bar.
@MainActor
Expand Down Expand Up @@ -43,7 +42,7 @@ final class MenuBarAppearanceManager: ObservableObject {
do {
configuration = try .migrate(encoder: encoder, decoder: decoder)
} catch {
logError(to: .appearanceManager, "Error decoding configuration: \(error)")
Logger.appearanceManager.error("Error decoding configuration: \(error)")
}
}

Expand All @@ -70,7 +69,7 @@ final class MenuBarAppearanceManager: ObservableObject {
.encode(encoder: encoder)
.sink { completion in
if case .failure(let error) = completion {
logError(to: .appearanceManager, "Error encoding configuration: \(error)")
Logger.appearanceManager.error("Error encoding configuration: \(error)")
}
} receiveValue: { data in
Defaults.set(data, forKey: .menuBarAppearanceConfiguration)
Expand Down
Loading

0 comments on commit 361144b

Please sign in to comment.