Skip to content

Commit

Permalink
Add a no-op shim for os.Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
d-ronnqvist committed Dec 3, 2024
1 parent e0c04f5 commit 66dd6bf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions Tests/SwiftDocCTests/Utility/NoOpSignposterShimTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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))")

}
}

0 comments on commit 66dd6bf

Please sign in to comment.