diff --git a/MiddleDrag/AppDelegate.swift b/MiddleDrag/AppDelegate.swift index e402537..4e96bfd 100644 --- a/MiddleDrag/AppDelegate.swift +++ b/MiddleDrag/AppDelegate.swift @@ -12,9 +12,11 @@ class AppDelegate: NSObject, NSApplicationDelegate { // MARK: - Application Lifecycle func applicationDidFinishLaunching(_ notification: Notification) { - //Initialize Analytics + // Initialize Analytics first (sets up Sentry for crash reporting) AnalyticsManager.shared.initialize() + Log.info("MiddleDrag starting...", category: .app) + // Hide dock icon (menu bar app only) NSApp.setActivationPolicy(.accessory) @@ -33,16 +35,19 @@ class AppDelegate: NSObject, NSApplicationDelegate { // Load preferences preferences = PreferencesManager.shared.loadPreferences() + Log.info("Preferences loaded", category: .app) // Configure and start multitouch manager multitouchManager.updateConfiguration(preferences.gestureConfig) multitouchManager.start() + Log.info("Multitouch manager started", category: .app) // Set up menu bar UI menuBarController = MenuBarController( multitouchManager: multitouchManager, preferences: preferences ) + Log.info("Menu bar controller initialized", category: .app) // Set up notification observers setupNotifications() @@ -55,14 +60,18 @@ class AppDelegate: NSObject, NSApplicationDelegate { // Check Accessibility permission AFTER UI is set up // This way the menu bar icon appears even if permission is missing if !AXIsProcessTrusted() { + Log.warning("Accessibility permission not granted", category: .app) AnalyticsManager.shared.trackAccessibilityPermission(granted: false) showAccessibilityAlert() } else { + Log.info("Accessibility permission granted", category: .app) AnalyticsManager.shared.trackAccessibilityPermission(granted: true) } // Final cleanup of any stray windows closeAllWindows() + + Log.info("MiddleDrag initialization complete", category: .app) } private func showAccessibilityAlert() { @@ -103,7 +112,9 @@ class AppDelegate: NSObject, NSApplicationDelegate { } func applicationWillTerminate(_ notification: Notification) { - //Track app termination + Log.info("MiddleDrag terminating", category: .app) + + // Track app termination AnalyticsManager.shared.trackTermination() multitouchManager.stop() @@ -142,6 +153,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { preferences = newPreferences PreferencesManager.shared.savePreferences(preferences) multitouchManager.updateConfiguration(preferences.gestureConfig) + Log.info("Preferences updated", category: .app) } } diff --git a/MiddleDrag/Managers/DeviceMonitor.swift b/MiddleDrag/Managers/DeviceMonitor.swift index a84fcb6..b50a5ae 100644 --- a/MiddleDrag/Managers/DeviceMonitor.swift +++ b/MiddleDrag/Managers/DeviceMonitor.swift @@ -1,29 +1,10 @@ import Foundation import CoreFoundation -// MARK: - Debug Logging (Debug builds only) +// MARK: - Debug Touch Counter (Debug builds only) #if DEBUG -private let debugLogPath = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent("middledrag_touch.log") private var touchCount = 0 - -private func logToFile(_ message: String) { - let timestamp = ISO8601DateFormatter().string(from: Date()) - let line = "[\(timestamp)] \(message)\n" - if let data = line.data(using: .utf8) { - if FileManager.default.fileExists(atPath: debugLogPath.path) { - if let handle = try? FileHandle(forWritingTo: debugLogPath) { - handle.seekToEndOfFile() - handle.write(data) - handle.closeFile() - } - } else { - try? data.write(to: debugLogPath) - } - } -} -#else -@inline(__always) private func logToFile(_ message: String) {} #endif // MARK: - Global Callback Storage @@ -36,8 +17,9 @@ private var gDeviceMonitor: DeviceMonitor? private let deviceContactCallback: MTContactCallbackFunction = { device, touches, numTouches, timestamp, frame in #if DEBUG touchCount += 1 - if touchCount <= 5 || touchCount % 100 == 0 { - logToFile("Touch callback #\(touchCount): \(numTouches) touches") + // Log sparingly to avoid performance impact + if touchCount <= 5 || touchCount % 500 == 0 { + Log.debug("Touch callback #\(touchCount): \(numTouches) touches", category: .device) } #endif @@ -85,20 +67,24 @@ class DeviceMonitor { func start() { guard !isRunning else { return } - logToFile("DeviceMonitor.start() called") + Log.info("DeviceMonitor starting...", category: .device) + + var deviceCount = 0 + var registeredDevices: Set = [] // Try to get all devices if let deviceList = MTDeviceCreateList() { let count = CFArrayGetCount(deviceList) - logToFile("Found \(count) multitouch device(s)") + Log.info("Found \(count) multitouch device(s)", category: .device) for i in 0..