Repository Public License
1import Foundation2import os34/// Logging and signposts for ORIX Navigator. Local only: nothing here leaves the device, and there is no telemetry.5///6/// Privacy rule: URLs, page titles, search text and anything typed by the user are logged as `.private`7/// (redacted outside a debugger). Secrets are never passed to a logger at all.8public enum Log {9 public static let subsystem = "ai.spboucher.orix.navigator"1011 public static let app = Logger(subsystem: subsystem, category: "app")12 public static let web = Logger(subsystem: subsystem, category: "web")13 public static let storage = Logger(subsystem: subsystem, category: "storage")14 public static let privacy = Logger(subsystem: subsystem, category: "privacy")15 public static let downloads = Logger(subsystem: subsystem, category: "downloads")16 public static let agent = Logger(subsystem: subsystem, category: "agent")17 public static let flow = Logger(subsystem: subsystem, category: "flow")18 public static let sync = Logger(subsystem: subsystem, category: "sync")19 public static let performance = Logger(subsystem: subsystem, category: "performance")20}2122/// Signpost intervals visible in Instruments (Points of Interest / os_signpost).23public struct Signpost: Sendable {24 public static let signposter = OSSignposter(subsystem: Log.subsystem, category: .pointsOfInterest)2526 private let state: OSSignpostIntervalState27 private let name: StaticString2829 public static func begin(_ name: StaticString) -> Signpost {30 Signpost(state: signposter.beginInterval(name, id: signposter.makeSignpostID()), name: name)31 }3233 public func end() { Self.signposter.endInterval(name, state) }3435 public static func event(_ name: StaticString) { signposter.emitEvent(name) }3637 /// Measures an async operation as one interval.38 public static func measure<T: Sendable>(_ name: StaticString, _ work: () async throws -> T) async rethrows -> T {39 let interval = begin(name)40 defer { interval.end() }41 return try await work()42 }43}4445/// Removes anything that looks like a credential from text before it is shown in a diagnostic report.46public enum Redactor {47 /// Order matters: bearer tokens and key-shaped strings go first, so the generic `name: value` rule cannot stop48 /// at the word "Bearer" and leave the token behind.49 private static let rules: [(NSRegularExpression, String)] = [50 (#"(?i)\bbearer\s+[A-Za-z0-9._\-]{8,}"#, "‹redacted›"),51 (#"\b(sk|pk|rk|xai|gsk|or)-[A-Za-z0-9_\-]{12,}"#, "‹redacted›"),52 (#"(?i)(authorization|x-api-key|api[-_]?key|token|secret|password|cookie|set-cookie)(["']?\s*[:=]\s*["']?)([^\s"',;]+)"#,53 "$1$2‹redacted›"),54 ].compactMap { pattern, template in55 (try? NSRegularExpression(pattern: pattern)).map { ($0, template) }56 }5758 public static func redact(_ text: String) -> String {59 var result = text60 for (regex, template) in rules {61 let range = NSRange(result.startIndex..., in: result)62 result = regex.stringByReplacingMatches(in: result, range: range, withTemplate: template)63 }64 return result65 }6667 /// A URL without query, fragment or user info — safe for a diagnostic report.68 public static func redact(_ url: URL) -> String {69 guard var components = URLComponents(url: url, resolvingAgainstBaseURL: false) else { return "‹url›" }70 components.query = nil; components.fragment = nil; components.user = nil; components.password = nil71 return components.string ?? "‹url›"72 }73}7475/// Wall-clock stopwatch for benchmarks reported in docs/BENCHMARKS.md.76public struct Stopwatch: Sendable {77 private let start = ContinuousClock.now78 public init() {}79 public var milliseconds: Double {80 let elapsed = ContinuousClock.now - start81 return Double(elapsed.components.seconds) * 1_000 + Double(elapsed.components.attoseconds) / 1e1582 }83}84