-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathLogger.swift
More file actions
185 lines (168 loc) · 5.49 KB
/
Logger.swift
File metadata and controls
185 lines (168 loc) · 5.49 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import Foundation
import os.log
enum LogLevel: String {
case debug
case info
case error
}
public final class Logger {
private let subsystem: String
private let category: String
private let osLog: OSLog
private let fileLogger = FileLogger()
public static let service = Logger(category: "Service")
public static let ui = Logger(category: "UI")
public static let client = Logger(category: "Client")
public static let updateChecker = Logger(category: "UpdateChecker")
public static let gitHubCopilot = Logger(category: "GitHubCopilot")
public static let langchain = Logger(category: "LangChain")
public static let retrieval = Logger(category: "Retrieval")
public static let license = Logger(category: "License")
public static let `extension` = Logger(category: "Extension")
public static let communicationBridge = Logger(category: "CommunicationBridge")
public static let workspacePool = Logger(category: "WorkspacePool")
public static let debug = Logger(category: "Debug")
public static var telemetryLogger: TelemetryLoggerProvider? = nil
#if DEBUG
/// Use a temp logger to log something temporary. I won't be available in release builds.
public static let temp = Logger(category: "Temp")
#endif
public init(subsystem: String = "com.github.CopilotForXcode", category: String) {
self.subsystem = subsystem
self.category = category
osLog = OSLog(subsystem: subsystem, category: category)
}
func log(
level: LogLevel,
message: String,
error: Error? = nil,
file: StaticString = #file,
line: UInt = #line,
function: StaticString = #function,
callStackSymbols: [String] = []
) {
let osLogType: OSLogType
switch level {
case .debug:
osLogType = .debug
case .info:
osLogType = .info
case .error:
osLogType = .error
}
os_log("%{public}@", log: osLog, type: osLogType, message as CVarArg)
fileLogger.log(level: level, category: category, message: message)
if osLogType == .error {
if let error = error {
Logger.telemetryLogger?.sendError(
error: error,
category: category,
file: file,
line: line,
function: function,
callStackSymbols: callStackSymbols
)
} else {
Logger.telemetryLogger?.sendError(
message: message,
category: category,
file: file,
line: line,
function: function,
callStackSymbols: callStackSymbols
)
}
}
}
public func debug(
_ message: String,
file: StaticString = #file,
line: UInt = #line,
function: StaticString = #function
) {
log(level: .debug, message: """
\(message)
file: \(file)
line: \(line)
function: \(function)
""", file: file, line: line, function: function)
}
public func info(
_ message: String,
file: StaticString = #file,
line: UInt = #line,
function: StaticString = #function
) {
log(level: .info, message: message, file: file, line: line, function: function)
}
public func error(
_ message: String,
file: StaticString = #file,
line: UInt = #line,
function: StaticString = #function,
callStackSymbols: [String] = []
) {
log(
level: .error,
message: message,
file: file,
line: line,
function: function,
callStackSymbols: callStackSymbols
)
}
public func error(
_ error: Error,
file: StaticString = #file,
line: UInt = #line,
function: StaticString = #function,
callStackSymbols: [String] = Thread.callStackSymbols
) {
log(
level: .error,
message: error.localizedDescription,
error: error,
file: file,
line: line,
function: function,
callStackSymbols: callStackSymbols
)
}
public func signpostBegin(
name: StaticString,
file: StaticString = #file,
line: UInt = #line,
function: StaticString = #function
) -> Signposter {
let poster = OSSignposter(logHandle: osLog)
let id = poster.makeSignpostID()
let state = poster.beginInterval(name, id: id)
return .init(log: osLog, id: id, name: name, signposter: poster, beginState: state)
}
public struct Signposter {
let log: OSLog
let id: OSSignpostID
let name: StaticString
let signposter: OSSignposter
let state: OSSignpostIntervalState
init(
log: OSLog,
id: OSSignpostID,
name: StaticString,
signposter: OSSignposter,
beginState: OSSignpostIntervalState
) {
self.id = id
self.log = log
self.name = name
self.signposter = signposter
state = beginState
}
public func end() {
signposter.endInterval(name, state)
}
public func event(_ text: String) {
signposter.emitEvent(name, id: id, "\(text, privacy: .public)")
}
}
}