Skip to content

Commit

Permalink
fix: Conditional usage of os.log for Linux support
Browse files Browse the repository at this point in the history
  • Loading branch information
surpher committed Jul 29, 2021
1 parent 703bb15 commit bc1c129
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
20 changes: 17 additions & 3 deletions Sources/PactSwiftToolbox/Logger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,21 +41,30 @@ 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)")
}
}

}

private extension Logger {

static func print(message: String) {
debugPrint(message)
}

enum PactLoggingLevel: String {
case all
case disabled
Expand Down
4 changes: 4 additions & 0 deletions Sources/PactSwiftToolbox/SocketBinder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//

#if !os(Linux)

import Foundation

@frozen public enum SocketBinder {
Expand Down Expand Up @@ -80,3 +82,5 @@ private extension SocketBinder {
}

}

#endif

0 comments on commit bc1c129

Please sign in to comment.