From 66dd6bfbe71838bd45e37218d392608958713e42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20R=C3=B6nnqvist?= Date: Tue, 3 Dec 2024 17:41:00 +0100 Subject: [PATCH] Add a no-op shim for os.Logger --- .../NoOpSignposterShim.swift | 26 ++++++++++++++++++- .../Utility/NoOpSignposterShimTests.swift | 14 ++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/Sources/SwiftDocC/Utility/FoundationExtensions/NoOpSignposterShim.swift b/Sources/SwiftDocC/Utility/FoundationExtensions/NoOpSignposterShim.swift index ce02bcb91..4bd5cfd6f 100644 --- a/Sources/SwiftDocC/Utility/FoundationExtensions/NoOpSignposterShim.swift +++ b/Sources/SwiftDocC/Utility/FoundationExtensions/NoOpSignposterShim.swift @@ -8,7 +8,31 @@ See https://swift.org/CONTRIBUTORS.txt for Swift project authors */ -/// A shim for `OSSignposter` that does nothing, except for running the passed interval task. +/// A shim for `os.Logger` that does nothing. +/// +/// This type allows calling code to avoid using `#if canImport(os)` throughout the implementation. +package struct NoOpLoggerShim : @unchecked Sendable { + package init() {} + + package var isEnabled: Bool { false } + + package enum Level { + case `default`, info, debug, error, fault + } + + package func log(_ message: NoOpLogMessage) {} + package func log(level: Level, _ message: NoOpLogMessage) {} + + package func trace(_ message: NoOpLogMessage) {} + package func debug(_ message: NoOpLogMessage) {} + package func info(_ message: NoOpLogMessage) {} + package func warning(_ message: NoOpLogMessage) {} + package func error(_ message: NoOpLogMessage) {} + package func critical(_ message: NoOpLogMessage) {} + package func fault(_ message: NoOpLogMessage) {} +} + +/// A shim for `os.OSSignposter` that does nothing, except for running the passed interval task. /// /// This type allows calling code to avoid using `#if canImport(os)` throughout the implementation. package struct NoOpSignposterShim : @unchecked Sendable { diff --git a/Tests/SwiftDocCTests/Utility/NoOpSignposterShimTests.swift b/Tests/SwiftDocCTests/Utility/NoOpSignposterShimTests.swift index facb69e04..66caff16f 100644 --- a/Tests/SwiftDocCTests/Utility/NoOpSignposterShimTests.swift +++ b/Tests/SwiftDocCTests/Utility/NoOpSignposterShimTests.swift @@ -38,6 +38,8 @@ final class NoOpSignposterShimTests: XCTestCase { } func testCanAcceptMessageInputs() { + // Note: this test has no assertions. + // It simply verifies that the message interpolations compile let signposter = NoOpSignposterShim() let handle = signposter.beginInterval("Some interval", "Some message") @@ -51,5 +53,17 @@ final class NoOpSignposterShimTests: XCTestCase { signposter.emitEvent("Some event", "Some non-secret string \("my secret", privacy: .public)") signposter.emitEvent("Some event", "Some aligned values \(12, align: .right(columns: 5)) \("some text", align: .left(columns: 10))") + + let logger = NoOpLoggerShim() + + logger.log("Some static string") + logger.info("Some formatted bool \(true, format: .answer)") + logger.debug("Some formatted integer \(12, format: .decimal)") + logger.error("Some formatted float \(7.0, format: .exponential)") + logger.fault("Some sensitive string \("my secret", privacy: .sensitive(mask: .hash))") + logger.log(level: .fault, "Some non-secret string \("my secret", privacy: .public)") + + logger.log(level: .default, "Some aligned values \(12, align: .right(columns: 5)) \("some text", align: .left(columns: 10))") + } }