Skip to content

Commit 482a988

Browse files
committed
Log to stderr instead of sending window/logMessage
- clangd itself already logs to stderr so sourcekit-lsp should do the same for consistency (unless we want to capture clangd's stderr and forward it in the LSP) - Editors such as VS Code will show stderr output in the same output channel where it shows logMessage + traces Change-Id: Iaf00cffa2e64d8490e21b49a3a9d34a17f54aa9f
1 parent c49849b commit 482a988

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

Sources/LSPLogging/Logging.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ public final class Logger {
8383

8484
var disableNSLog: Bool = false
8585

86+
/// Used to log to stderr when `NSLog` logging is disabled.
87+
var dateFormatter: DateFormatter
88+
8689
/// The current logging level.
8790
public var currentLevel: LogLevel {
8891
get { return logLevelQueue.sync { _currentLevel } }
@@ -94,6 +97,9 @@ public final class Logger {
9497
public init(disableOSLog: Bool = false, disableNSLog: Bool = false) {
9598
self.disableOSLog = disableOSLog
9699
self.disableNSLog = disableNSLog
100+
101+
self.dateFormatter = DateFormatter()
102+
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
97103
}
98104

99105
public func addLogHandler(_ handler: LogHandler) {
@@ -181,11 +187,19 @@ public final class Logger {
181187
}
182188
}
183189

190+
private func logToStderr(_ message: String, level: LogLevel) {
191+
let time = self.dateFormatter.string(from: Date())
192+
let fullMessage = "[\(time)] \(message)\n"
193+
fputs(fullMessage, stderr)
194+
}
195+
184196
private func logImpl(_ message: String, level: LogLevel, usedOSLog: Bool) {
185197

186198
if !self.disableNSLog && !usedOSLog {
187199
// Fallback to NSLog if os_log isn't available.
188200
NSLog(message)
201+
} else {
202+
self.logToStderr(message, level: level)
189203
}
190204

191205
for handler in self.handlers {

Sources/sourcekit-lsp/main.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,6 @@ struct Main: ParsableCommand {
178178
_Exit(0)
179179
})
180180

181-
Logger.shared.addLogHandler { message, _ in
182-
clientConnection.send(LogMessageNotification(type: .log, message: message))
183-
}
184-
185181
dispatchMain()
186182
}
187183
}

0 commit comments

Comments
 (0)