From bc1c1290e61233db56f0feb4f2114ccafe9ff922 Mon Sep 17 00:00:00 2001 From: Marko Justinek Date: Thu, 29 Jul 2021 15:37:24 +1000 Subject: [PATCH] fix: Conditional usage of os.log for Linux support --- Sources/PactSwiftToolbox/Logger.swift | 20 +++++++++++++++++--- Sources/PactSwiftToolbox/SocketBinder.swift | 4 ++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Sources/PactSwiftToolbox/Logger.swift b/Sources/PactSwiftToolbox/Logger.swift index 83bdb5b..dc38ed1 100644 --- a/Sources/PactSwiftToolbox/Logger.swift +++ b/Sources/PactSwiftToolbox/Logger.swift @@ -16,13 +16,18 @@ // import Foundation + +#if !os(Linux) import os.log +#endif + +// TODO: - Use something like apple/swift-log instead of os.log public enum Logger { - /// Uses `os_log` to log Pact related messages. + /// Logs Pact related messages. /// - /// Looks for environment variable `PACT_ENABLE_LOGGING = "all"`. Can be set in project's scheme. + /// Looks for environment variable `PACT_ENABLE_LOGGING = "all"`. Can be set in project's scheme. Uses `os_log` on Apple platforms. /// /// - Parameters: /// - message: The message to log @@ -36,14 +41,19 @@ public enum Logger { let stringData = data.flatMap { String(data: $0, encoding: .utf8) } ?? "" if #available(iOS 10, OSX 10.14, *) { + #if !os(Linux) os_log( "PactSwift: %{private}s", log: .default, type: .default, "\(message): \(stringData)" ) + #else + print(message: "PactSwift: \(message)\n\(stringData)") + #endif + } else { - debugPrint("PactSwift: \(message)\n\(stringData)") + print(message: "PactSwift: \(message)\n\(stringData)") } } @@ -51,6 +61,10 @@ public enum Logger { private extension Logger { + static func print(message: String) { + debugPrint(message) + } + enum PactLoggingLevel: String { case all case disabled diff --git a/Sources/PactSwiftToolbox/SocketBinder.swift b/Sources/PactSwiftToolbox/SocketBinder.swift index f629914..b18ae68 100644 --- a/Sources/PactSwiftToolbox/SocketBinder.swift +++ b/Sources/PactSwiftToolbox/SocketBinder.swift @@ -15,6 +15,8 @@ // IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. // +#if !os(Linux) + import Foundation @frozen public enum SocketBinder { @@ -80,3 +82,5 @@ private extension SocketBinder { } } + +#endif