Files
coucou/NotchBuddy/Sources/App/AppLog.swift
Claude b4343d9dfa perf: idle CPU and smoothness pass (no visual change)
Hidden island
- The idle poll measured "near" from the 720x560 panel, so a hidden island polled at
  60 Hz most of the time: measured from the island now, 8 Hz elsewhere.
- Poll stops while the screen sleeps or is locked; small timer tolerance.
- Countdown bar's 10 Hz timer only runs while the island is open.
- Global key monitor: no Task per keystroke, only Escape goes further.

Drawing
- Island views behind the active one pause their animations (environment flag):
  ticker shimmer, mini Mochis, upload bar.
- Mini Mochis drawn at 30 fps with the same motion (engine still steps per display
  frame); notch Mochi not drawn while it lives on the desktop.
- Mochi's look target and seasonal outfit no longer recomputed with screen lookups
  and calendar math every frame.
- Desktop Mochi polls at 30 Hz (it draws at 30), feeds the eye tracking, and stops
  when the screen sleeps.

Voice
- VAD durations follow the real buffer length (macOS often gives 100 ms buffers: the
  silence tail was ~3.5 s instead of 0.8 s, keeping recognition running on noise).
- vDSP for the mic power, recognizer reused per pipeline, system voice cached,
  micLevel no longer @Published, repeated partials ignored, watchdog tolerance,
  no "deaf recognizer" rebuild while music plays.

Background
- Logging on a background queue with a cached formatter (was ~8 file ops on main).
- n8n poller silent when not configured; quiet polls write nothing.
- Poller timer leeway; Stripe/Notion/Cal.com publish only on change.
- Status line: plan usage published on change (or every 30 s), not several times/s.
- Codex plan fetch rate-limited on the attempt, not only on success.
- Apple Mail send runs off the main thread (the island froze for 1 s+).
- iPhone instructions: checked on CloudKit push, poll 90 s when pushes work.
- Window highlight during an attach drag only rescans when the pointer moves.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LJm3SRL9k3gBPaVtfPhwcT
2026-10-10 20:17:27 +02:00

87 lines
3.8 KiB
Swift

import Foundation
// MARK: - Shared diagnostic log helpers
/// Escapes ASCII control characters so log lines can't inject terminal sequences.
func escapedForLog(_ s: String) -> String {
s.unicodeScalars.map { sc -> String in
let v = sc.value
return (v < 0x20 || v == 0x7F) ? "\\u\(String(format: "%04X", v))" : String(sc)
}.joined()
}
/// Appends one timestamped line to `~/Library/Logs/NotchBuddy/<fileName>`.
/// - Log directory is created at mode 0700.
/// - Log file is set to mode 0600 on first creation and after each rotation.
/// - File is rotated (truncated) when it reaches 1 MB.
/// The time is taken now; the write happens on a background serial queue, in order, so a
/// log line never blocks the main thread (it used to cost ~8 file operations per line).
func appendAppLog(_ fileName: String, _ message: String,
timestampFormat: String = "yyyy-MM-dd HH:mm:ss") {
let date = Date()
AppLogWriter.queue.async {
AppLogWriter.write(fileName, message, date: date, format: timestampFormat)
}
}
private enum AppLogWriter {
static let queue = DispatchQueue(label: "fr.louisraille.NotchBuddy.applog", qos: .utility)
static let maxLogBytes = 1_048_576 // 1 MB
// Touched only on `queue`.
nonisolated(unsafe) static var dirReady = false
nonisolated(unsafe) static var sizes: [String: Int] = [:]
nonisolated(unsafe) static var formatters: [String: DateFormatter] = [:]
static let logsDir = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask)[0]
.appendingPathComponent("Logs/NotchBuddy")
static func write(_ fileName: String, _ message: String, date: Date, format: String) {
let fm = FileManager.default
if !dirReady {
try? fm.createDirectory(at: logsDir, withIntermediateDirectories: true)
try? fm.setAttributes([.posixPermissions: 0o700 as NSNumber], ofItemAtPath: logsDir.path)
dirReady = true
}
let logFile = logsDir.appendingPathComponent(fileName)
let f: DateFormatter
if let cached = formatters[format] { f = cached } else {
f = DateFormatter(); f.dateFormat = format; formatters[format] = f
}
let line = "\(f.string(from: date)) \(escapedForLog(message))\n"
guard let data = line.data(using: .utf8) else { return }
// Size known from our own writes; read from disk once per file (or if it vanished).
var size = sizes[fileName]
if size == nil || !fm.fileExists(atPath: logFile.path) {
size = fm.fileExists(atPath: logFile.path)
? ((try? fm.attributesOfItem(atPath: logFile.path)[.size] as? Int) ?? 0) : nil
}
guard let current = size else {
// New file
try? data.write(to: logFile, options: .atomic)
try? fm.setAttributes([.posixPermissions: 0o600 as NSNumber], ofItemAtPath: logFile.path)
sizes[fileName] = data.count
return
}
if current >= maxLogBytes {
// Rotate when the file reaches the limit
try? fm.removeItem(at: logFile)
try? data.write(to: logFile, options: .atomic)
try? fm.setAttributes([.posixPermissions: 0o600 as NSNumber], ofItemAtPath: logFile.path)
sizes[fileName] = data.count
return
}
if sizes[fileName] == nil {
// First write to an existing file this run: make sure of its permissions.
try? fm.setAttributes([.posixPermissions: 0o600 as NSNumber], ofItemAtPath: logFile.path)
}
if let handle = try? FileHandle(forWritingTo: logFile) {
handle.seekToEndOfFile()
handle.write(data)
try? handle.close()
}
sizes[fileName] = current + data.count
}
}