Skip to content

Commit

Permalink
refactored LoggerType
Browse files Browse the repository at this point in the history
  • Loading branch information
leogdion committed Oct 17, 2024
1 parent f722b29 commit 6a18b46
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 53 deletions.
17 changes: 4 additions & 13 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
{
"originHash" : "2b9f814946e4952387f6691929b3d5d9a95ddbf99f1704f4beaab390f06560d6",
"originHash" : "eb6d44461edfa318c7714318cbf366fdaa85477ce7d75eb7670439f78984597b",
"pins" : [
{
"identity" : "sublimation",
"kind" : "remoteSourceControl",
"location" : "https://github.com/brightdigit/Sublimation",
"location" : "https://github.com/brightdigit/Sublimation.git",
"state" : {
"revision" : "99d0da9f907d27deb180582b274c5f83e6732921",
"version" : "2.0.0-beta.1"
}
},
{
"identity" : "swift-log",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-log.git",
"state" : {
"revision" : "9cb486020ebf03bfa5b5df985387a14a98744537",
"version" : "1.6.1"
"branch" : "remove-logging",
"revision" : "a342f5c67827329a7db1caa1c700da79fa40cc7d"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ let package = Package(
.library(name: "SublimationBonjour", targets: ["SublimationBonjour"])
],
dependencies: [
.package(path: "../Sublimation"),
.package(url: "https://github.com/brightdigit/Sublimation.git", branch: "remove-logging"),
.package(url: "https://github.com/apple/swift-protobuf.git", from: "1.26.0")
],
targets: [
Expand Down
17 changes: 7 additions & 10 deletions Sources/SublimationBonjour/Client/BonjourClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,7 @@

#if canImport(Network)
public import Foundation

internal import Network

#if canImport(os)
public import os
#elseif canImport(Logging)
public import Logging
#endif
internal import Network

/// Client for fetching the url of the host server.
///
Expand All @@ -46,9 +39,10 @@ internal import Network
/// let hostURL = await depositor.first()
/// ```
public actor BonjourClient {

private let browser: NWBrowser
private let streams = StreamManager<UUID, URL>()
private let logger: Logger?
private let logger: LoggerType?
private let defaultURLConfiguration: URLDefaultConfiguration

/// AsyncStream of `URL` from the network.
Expand Down Expand Up @@ -76,7 +70,10 @@ internal import Network
/// - Parameters:
/// - logger: Logger
/// - defaultURLConfiguration: default ``URL`` configuration for missing properties.
public init(logger: Logger? = nil, defaultURLConfiguration: URLDefaultConfiguration = .init()) {
public init(
logger: LoggerType? = nil,
defaultURLConfiguration: URLDefaultConfiguration = .init()
) {

Check warning on line 76 in Sources/SublimationBonjour/Client/BonjourClient.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SublimationBonjour/Client/BonjourClient.swift#L76

Added line #L76 was not covered by tests
assert(logger != nil)
let descriptor: NWBrowser.Descriptor
descriptor = .bonjourWithTXTRecord(type: "_sublimation._tcp", domain: nil)
Expand Down
41 changes: 41 additions & 0 deletions Sources/SublimationBonjour/Client/LoggerType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// LoggerType.swift
// SublimationBonjour
//
// Created by Leo Dion.
// Copyright © 2024 BrightDigit.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//

#if canImport(os)
public import os
#elseif canImport(Logging)
public import Logging
#endif

#if canImport(os) || canImport(Logging)
public typealias LoggerType = Logger
#else
public typealias LoggerType = any NilLoggerType
public protocol NilLoggerType { func debug(_ message: String) }
#endif
8 changes: 1 addition & 7 deletions Sources/SublimationBonjour/Client/StreamManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@

internal import Foundation

#if canImport(os)
internal import os
#elseif canImport(Logging)
internal import Logging
#endif

internal actor StreamManager<Key: Hashable & Sendable, Value: Sendable> {
private var streamContinuations: [Key: AsyncStream<Value>.Continuation] = [:]

Expand All @@ -44,7 +38,7 @@ internal actor StreamManager<Key: Hashable & Sendable, Value: Sendable> {

internal init(newID: @escaping @Sendable () -> Key) { self.newID = newID }

internal func yield(_ urls: [Value], logger: Logger?) {
internal func yield(_ urls: [Value], logger: LoggerType?) {

Check warning on line 41 in Sources/SublimationBonjour/Client/StreamManager.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SublimationBonjour/Client/StreamManager.swift#L41

Added line #L41 was not covered by tests
if streamContinuations.isEmpty { logger?.debug("Missing Continuations.") }
for streamContinuation in streamContinuations {
for url in urls { streamContinuation.value.yield(url) }
Expand Down
27 changes: 9 additions & 18 deletions Sources/SublimationBonjour/Extensions/NWConnection.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// NWListener.swift
// NWConnection.swift
// SublimationBonjour
//
// Created by Leo Dion.
Expand Down Expand Up @@ -28,27 +28,18 @@
//

#if canImport(Network)
public import Foundation
import Foundation
public import Network

extension NWConnection.State: @retroactive CustomDebugStringConvertible {
extension NWConnection.State: @retroactive CustomDebugStringConvertible {
@_documentation(visibility: internal) public var debugDescription: String {
switch self {

case .setup:
return "setup"
case .waiting(let error):
switch self { case .setup: return "setup" case .waiting(let error):
return "waiting: \(error.debugDescription)"
case .preparing:
return "preparing"
case .ready:
return "ready"
case .failed(let error):
return "failed: \(error.debugDescription)"
case .cancelled:
return "cancelled"
@unknown default:
return "unknown state"
case .preparing: return "preparing"
case .ready: return "ready"
case .failed(let error): return "failed: \(error.debugDescription)"
case .cancelled: return "cancelled"
@unknown default: return "unknown state"
}
}

Check warning on line 44 in Sources/SublimationBonjour/Extensions/NWConnection.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SublimationBonjour/Extensions/NWConnection.swift#L35-L44

Added lines #L35 - L44 were not covered by tests
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SublimationBonjour/Extensions/NWListener.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
//

#if canImport(Network)
public import Foundation
import Foundation
public import Network

extension NWListener.State: @retroactive CustomDebugStringConvertible {
Expand Down
5 changes: 2 additions & 3 deletions Sources/SublimationBonjour/Server/BonjourSublimatory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
public import Network

public import SublimationCore
public import Logging
public import Logging

/// Sublimatory for using Bonjour auto-discovery.
public struct BonjourSublimatory: Sublimatory {
Expand Down Expand Up @@ -169,7 +169,6 @@ public import Logging
connection.stateUpdateHandler = { state in
switch state { case .waiting(let error):


logger.debug("Connection Waiting error: \(error.localizedDescription)")

case .ready:
Expand All @@ -184,7 +183,7 @@ public import Logging
)
case .failed(let error): logger.debug("Connection Failure: \(error)")

default: logger.debug("Connection state updated: \(state.debugDescription)")
default: logger.debug("Connection state updated: \(state.debugDescription)")
}
}
connection.start(queue: connectionQueue)
Expand Down

0 comments on commit 6a18b46

Please sign in to comment.