Skip to content

Log to stderr instead of sending window/logMessage #327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Sources/LSPLogging/Logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ public final class Logger {

var disableNSLog: Bool = false

/// Used to log to stderr when `NSLog` logging is disabled.
var dateFormatter: DateFormatter

/// The current logging level.
public var currentLevel: LogLevel {
get { return logLevelQueue.sync { _currentLevel } }
Expand All @@ -94,6 +97,9 @@ public final class Logger {
public init(disableOSLog: Bool = false, disableNSLog: Bool = false) {
self.disableOSLog = disableOSLog
self.disableNSLog = disableNSLog

self.dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
}

public func addLogHandler(_ handler: LogHandler) {
Expand Down Expand Up @@ -181,11 +187,19 @@ public final class Logger {
}
}

private func logToStderr(_ message: String, level: LogLevel) {
let time = self.dateFormatter.string(from: Date())
let fullMessage = "[\(time)] \(message)\n"
fputs(fullMessage, stderr)
}

private func logImpl(_ message: String, level: LogLevel, usedOSLog: Bool) {

if !self.disableNSLog && !usedOSLog {
// Fallback to NSLog if os_log isn't available.
NSLog(message)
} else {
self.logToStderr(message, level: level)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked: disableNSLog is never true except in unit tests. Should we just rely on NSLog?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm we could but won't that duplicate os_logs (os_log from us, NSLog)? If we drop our manual os_log. usage we could but it would be a slight change in behavior (filtering before logging)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry, I misread how it would work.

}

for handler in self.handlers {
Expand Down
4 changes: 0 additions & 4 deletions Sources/sourcekit-lsp/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,6 @@ struct Main: ParsableCommand {
_Exit(0)
})

Logger.shared.addLogHandler { message, _ in
clientConnection.send(LogMessageNotification(type: .log, message: message))
}

dispatchMain()
}
}
Expand Down