Skip to content

Commit

Permalink
refactoring to work around String CVarArg compatibility issue
Browse files Browse the repository at this point in the history
  • Loading branch information
tanderson-ld committed May 30, 2024
1 parent c10ec29 commit 1098609
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
6 changes: 4 additions & 2 deletions Source/LDSwiftEventSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ class EventSourceDelegate: NSObject, URLSessionDataDelegate {

readyState = .closed
let sleep = reconnectionTimer.reconnectDelay(baseDelay: currentRetry)
logger.log(.info, "Waiting %.3f seconds before reconnecting...", sleep)
// this formatting shenanigans is to workaround String not implementing CVarArg on Swift<5.4 on Linux
logger.log(.info, "Waiting %@ seconds before reconnecting...", String(format: "%.3f", sleep))
delegateQueue.asyncAfter(deadline: .now() + sleep) { [weak self] in
self?.connect()
}
Expand Down Expand Up @@ -326,7 +327,8 @@ class EventSourceDelegate: NSObject, URLSessionDataDelegate {
config.handler.onOpened()
completionHandler(.allow)
} else {
logger.log(.info, "Unsuccessful response: %d", statusCode)
// this formatting shenanigans is to workaround String not implementing CVarArg on Swift<5.4 on Linux
logger.log(.info, "Unsuccessful response: %@", String(format: "%d", statusCode))
if dispatchError(error: UnsuccessfulResponseError(responseCode: statusCode)) == .shutdown {
logger.log(.info, "Connection has been explicitly shut down by error handler")
readyState = .shutdown
Expand Down
12 changes: 6 additions & 6 deletions Source/Logs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import os.log

protocol InternalLogging {
func log(_ level: Level, _ staticMsg: StaticString)
func log(_ level: Level, _ staticMsg: StaticString, _ arg: CVarArg)
func log(_ level: Level, _ staticMsg: StaticString, _ arg1: CVarArg, _ arg2: CVarArg)
func log(_ level: Level, _ staticMsg: StaticString, _ arg: String)
func log(_ level: Level, _ staticMsg: StaticString, _ arg1: String, _ arg2: String)
}

enum Level {
Expand Down Expand Up @@ -35,18 +35,18 @@ class OSLogAdapter: InternalLogging {
os_log(staticMsg, log: self.osLog, type: level.osLogType)
}

func log(_ level: Level, _ staticMsg: StaticString, _ arg: CVarArg) {
func log(_ level: Level, _ staticMsg: StaticString, _ arg: String) {
os_log(staticMsg, log: self.osLog, type: level.osLogType, arg)
}

func log(_ level: Level, _ staticMsg: StaticString, _ arg1: CVarArg, _ arg2: CVarArg) {
func log(_ level: Level, _ staticMsg: StaticString, _ arg1: String, _ arg2: String) {
os_log(staticMsg, log: self.osLog, type: level.osLogType, arg1, arg2)
}
}
#endif

class NoOpLogging: InternalLogging {
func log(_ level: Level, _ staticMsg: StaticString) {}
func log(_ level: Level, _ staticMsg: StaticString, _ arg: CVarArg) {}
func log(_ level: Level, _ staticMsg: StaticString, _ arg1: CVarArg, _ arg2: CVarArg) {}
func log(_ level: Level, _ staticMsg: StaticString, _ arg: String) {}
func log(_ level: Level, _ staticMsg: StaticString, _ arg1: String, _ arg2: String) {}
}

0 comments on commit 1098609

Please sign in to comment.