DomainDig/DomainDebugLog.swift

41 lines · 1267 bytes

 1import Foundation
 2import os
 3
 4enum DomainDebugLog {
 5#if DEBUG
 6    static let enabled = true
 7#else
 8    static let enabled = false
 9#endif
10    private static let logger = Logger(subsystem: "co.krz.domain-dig", category: "Debug")
11
12    static func debug(_ message: String) {
13        guard enabled else { return }
14        logger.debug("\(message, privacy: .public)")
15    }
16
17    static func error(_ message: String) {
18        guard enabled else { return }
19        logger.error("\(message, privacy: .public)")
20    }
21
22    static func signpostStart(_ scope: String, domain: String? = nil) -> CFAbsoluteTime {
23        let start = CFAbsoluteTimeGetCurrent()
24        if let domain {
25            debug("[START] \(scope) domain=\(domain)")
26        } else {
27            debug("[START] \(scope)")
28        }
29        return start
30    }
31
32    static func signpostEnd(_ scope: String, start: CFAbsoluteTime, domain: String? = nil, extra: String? = nil) {
33        let elapsedMs = Int((CFAbsoluteTimeGetCurrent() - start) * 1000)
34        let suffix = extra.map { " \($0)" } ?? ""
35        if let domain {
36            debug("[END] \(scope) domain=\(domain) elapsedMs=\(elapsedMs)\(suffix)")
37        } else {
38            debug("[END] \(scope) elapsedMs=\(elapsedMs)\(suffix)")
39        }
40    }
41}