Files
sourcekit-lsp/Sources/LSPLogging/Error+ForLogging.swift
Alex Hoppen f960d7ed9b Change logging to use OSLog
OSLog is the suggesting logging solution on Apple platforms and we should be using it there, taking advantage of the different log levels and privacy masking.

Switch sourcekit-lsp to use OSLog on Apple platforms and implement a logger that is API-compatible with OSLog for all uses in sourcekit-lsp and which can be used on non-Darwin platforms.

The goal of this commit is to introduce the new logging API. There are still improvements about what we log and we can display more privacy-insensitive information after masking. Those changes will be in follow-up commits.
2023-10-13 13:46:32 -07:00

44 lines
1.4 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Foundation
/// A wrapper around `Error` that conforms to `CustomLogStringConvertible`.
private struct MaskedError: CustomLogStringConvertible {
let underlyingError: any Error
init(_ underlyingError: any Error) {
self.underlyingError = underlyingError
}
var description: String {
return "\(underlyingError)"
}
var redactedDescription: String {
let error = underlyingError as NSError
return "\(error.code): \(error.description.hashForLogging)"
}
}
extension Error {
/// A version of the error that can be used for logging and will only log the
/// error code and a hash of the description in privacy-sensitive contexts.
public var forLogging: CustomLogStringConvertibleWrapper {
if let error = self as? CustomLogStringConvertible {
return error.forLogging
} else {
return MaskedError(self).forLogging
}
}
}