Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove logging #3

Merged
merged 7 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
{
"originHash" : "2b9f814946e4952387f6691929b3d5d9a95ddbf99f1704f4beaab390f06560d6",
"originHash" : "310cf77e37f4fb0934d2f2d16691a6155cb61148b115825c6dace23cf8e569f5",
"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"
"revision" : "593020a9e76039b21cb07114b67ae5abb84a7ffa",
"version" : "2.0.1"
}
},
{
"identity" : "swift-protobuf",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-protobuf.git",
"state" : {
"revision" : "e17d61f26df0f0e06f58f6977ba05a097a720106",
"version" : "1.27.1"
"revision" : "ebc7251dd5b37f627c93698e4374084d98409633",
"version" : "1.28.2"
}
}
],
Expand Down
8 changes: 3 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,16 @@ let package = Package(
.library(name: "SublimationBonjour", targets: ["SublimationBonjour"])
],
dependencies: [
.package(url: "https://github.com/brightdigit/Sublimation", from: "2.0.0-alpha.5"),
.package(url: "https://github.com/apple/swift-protobuf.git", from: "1.26.0"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0")
.package(url: "https://github.com/brightdigit/Sublimation.git", from: "2.0.1"),
.package(url: "https://github.com/apple/swift-protobuf.git", from: "1.26.0")
],
targets: [
.target(
name: "SublimationBonjour",
dependencies: [
.product(name: "Sublimation", package: "Sublimation"),
.product(name: "SublimationCore", package: "Sublimation"),
.product(name: "SwiftProtobuf", package: "swift-protobuf"),
.product(name: "Logging", package: "swift-log")
.product(name: "SwiftProtobuf", package: "swift-protobuf")
],
swiftSettings: swiftSettings
),
Expand Down
11 changes: 7 additions & 4 deletions Sources/SublimationBonjour/Client/BonjourClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@

#if canImport(Network)
public import Foundation

import Network
internal import Network

#if canImport(os)
public import os
Expand All @@ -46,9 +45,10 @@
/// 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 +76,10 @@
/// - 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 82 in Sources/SublimationBonjour/Client/BonjourClient.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SublimationBonjour/Client/BonjourClient.swift#L82

Added line #L82 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 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
2 changes: 1 addition & 1 deletion Sources/SublimationBonjour/Extensions/Dictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
// OTHER DEALINGS IN THE SOFTWARE.
//

import Foundation
internal import Foundation

extension Dictionary where Key == String, Value == String {
internal init(
Expand Down
46 changes: 46 additions & 0 deletions Sources/SublimationBonjour/Extensions/NWConnection.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// NWConnection.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(Network)
import Foundation
public import Network

extension NWConnection.State: @retroactive CustomDebugStringConvertible {
@_documentation(visibility: internal) public var debugDescription: String {
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"
}
}

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
}
#endif
2 changes: 1 addition & 1 deletion Sources/SublimationBonjour/Extensions/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
// OTHER DEALINGS IN THE SOFTWARE.
//

import Foundation
internal import Foundation

extension String {
internal func isLocalhost() -> Bool {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SublimationBonjour/Extensions/URL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
// OTHER DEALINGS IN THE SOFTWARE.
//

import Foundation
internal import Foundation

extension URL {
internal init?(scheme: String, host: String, port: Int) {
Expand Down
20 changes: 9 additions & 11 deletions Sources/SublimationBonjour/Server/BonjourSublimatory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@
// OTHER DEALINGS IN THE SOFTWARE.
//

#if canImport(Network)
#if canImport(Network) && canImport(Logging)

internal import Foundation

public import Network

public import SublimationCore

public import Logging

/// Sublimatory for using Bonjour auto-discovery.
Expand Down Expand Up @@ -165,26 +164,25 @@
let txtRecord = NWTXTRecord(dictionary)
assert(listener.service == nil)
listener.service = .init(name: name, type: type, txtRecord: txtRecord)

let logger = self.logger
listener.newConnectionHandler = { connection in
connection.stateUpdateHandler = { state in
switch state { case .waiting(let error):

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

case .ready:
self.logger.debug("Connection Ready")
self.logger.debug("Sending data \(data.count) bytes")
logger.debug("Connection Ready")
logger.debug("Sending data \(data.count) bytes")
connection.send(
content: data,
completion: .contentProcessed { error in
if let error { self.logger.warning("Connection Send error: \(error)") }
if let error { self.logger.error("Connection Send error: \(error)") }
connection.cancel()
}
)
case .failed(let error): self.logger.error("Connection Failure: \(error)")

default: self.logger.debug("Connection state updated: \(state)")
case .failed(let error): logger.debug("Connection Failure: \(error)")
default: logger.debug("Connection state updated: \(state.debugDescription)")
}
}
connection.start(queue: connectionQueue)
Expand All @@ -202,7 +200,7 @@
self.logger.error("Listener Failure: \(error)")
continuation.resume(throwing: error)
case .cancelled: continuation.resume()
default: self.logger.debug("Listener state updated: \(state)")
default: self.logger.debug("Listener state updated: \(state.debugDescription)")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
// OTHER DEALINGS IN THE SOFTWARE.
//

#if canImport(Network)
#if canImport(Network) && canImport(Logging)
public import Network
public import Sublimation
public import Logging
Expand Down