-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathLog.swift
More file actions
68 lines (58 loc) · 2.31 KB
/
Copy pathLog.swift
File metadata and controls
68 lines (58 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright © 2025 Snap, Inc. All rights reserved.
import Foundation
import os
/// Used to configure how SpectaclesKit logs are written out.
public enum Log {
/// Logging levels used by the framework. Correspond roughly to OSLogType
public enum LogLevel: Int, Sendable {
case debug
case info
case notice
case error
case fault
}
/// Protocol allowing client to inject custom logging implementations
public protocol Logger: Sendable {
func log(level: LogLevel, message: @autoclosure () -> String)
}
/// Default logger that logs messages to the os.Logger framework.
public struct DefaultLogger: Logger {
let logger = os.Logger(subsystem: "com.snap.spectacles.spectacles-kit-mobile", category: "SpectaclesKit")
let logLevel: LogLevel
let enablePublicLogging: Bool
public init(logLevel: LogLevel = .info, enablePublicLogging: Bool = false) {
self.logLevel = logLevel
self.enablePublicLogging = enablePublicLogging
}
public func log(level: Log.LogLevel, message: () -> String) {
guard level.rawValue >= logLevel.rawValue else { return }
let (logType, tag): (OSLogType, String) = switch level {
case .debug: (.debug, "debug")
case .info: (.info, "info")
case .notice: (.default, "notice")
case .error: (.error, "error")
case .fault: (.fault, "fault")
}
let message = message()
if enablePublicLogging {
logger.log(level: logType, "[\(tag, privacy: .public)] \(message, privacy: .public)")
} else {
logger.log(level: logType, "[\(tag, privacy: .public)] \(message)")
}
}
}
/**
Logger used by the framework to write log events.
Defaults to an instance of `DefaultLogger`. Set this to redirect logging to a custom logging framework, or to `nil` to disable logging entirely.
*/
public static var logger: (any Logger)? {
get {
loggerStorage.withLock { $0 }
}
set {
loggerStorage.withLock { $0 = newValue }
}
}
/// Private thread-safe storage for the logger
static let loggerStorage = Lock<(any Logger)?>(initialState: DefaultLogger())
}