DomainDig/DomainDebugLog.swift
48 lines · 1784 bytes
4 symbols in this file
1import Foundation
2import os
3
4/// Nonisolated because nothing here needs the main actor: `enabled` is a
5/// compile-time constant and `Logger` is `Sendable` and thread-safe. Without
6/// this the project-wide `SWIFT_DEFAULT_ACTOR_ISOLATION = MainActor` makes it
7/// main-actor-isolated, so a nonisolated caller — a UIKit completion handler,
8/// say — cannot log the failure it was handed without hopping actors to write
9/// one line. `AppIconController` had exactly that, as a Release-only warning
10/// the Debug build did not show.
11nonisolated enum DomainDebugLog {
12#if DEBUG
13 static let enabled = true
14#else
15 static let enabled = false
16#endif
17 private static let logger = Logger(subsystem: "co.krz.domain-dig", category: "Debug")
18
19 static func debug(_ message: String) {
20 guard enabled else { return }
21 logger.debug("\(message, privacy: .public)")
22 }
23
24 static func error(_ message: String) {
25 guard enabled else { return }
26 logger.error("\(message, privacy: .public)")
27 }
28
29 static func signpostStart(_ scope: String, domain: String? = nil) -> CFAbsoluteTime {
30 let start = CFAbsoluteTimeGetCurrent()
31 if let domain {
32 debug("[START] \(scope) domain=\(domain)")
33 } else {
34 debug("[START] \(scope)")
35 }
36 return start
37 }
38
39 static func signpostEnd(_ scope: String, start: CFAbsoluteTime, domain: String? = nil, extra: String? = nil) {
40 let elapsedMs = Int((CFAbsoluteTimeGetCurrent() - start) * 1000)
41 let suffix = extra.map { " \($0)" } ?? ""
42 if let domain {
43 debug("[END] \(scope) domain=\(domain) elapsedMs=\(elapsedMs)\(suffix)")
44 } else {
45 debug("[END] \(scope) elapsedMs=\(elapsedMs)\(suffix)")
46 }
47 }
48}