From edfe2d638e60c8f3e6ecd9f1b042c7f1a1d42149 Mon Sep 17 00:00:00 2001 From: Anthony Drendel Date: Thu, 1 Jul 2021 18:10:07 +0200 Subject: [PATCH] Dramatically reduce the number of logs created by Phoenix (#39) --- Sources/Phoenix/Channel.swift | 40 +++--------- Sources/Phoenix/ChannelState.swift | 4 +- Sources/Phoenix/Foundation+Phoenix.swift | 4 +- Sources/Phoenix/IncomingMessage.swift | 15 ++--- Sources/Phoenix/OutgoingMessage.swift | 9 +-- Sources/Phoenix/Payload.swift | 15 ----- Sources/Phoenix/Ref.swift | 4 +- Sources/Phoenix/Socket.swift | 82 +++++------------------- Sources/Phoenix/SocketPush.swift | 6 +- Sources/Phoenix/SocketState.swift | 4 +- 10 files changed, 38 insertions(+), 145 deletions(-) diff --git a/Sources/Phoenix/Channel.swift b/Sources/Phoenix/Channel.swift index 49b3cf52..d72ad4f1 100644 --- a/Sources/Phoenix/Channel.swift +++ b/Sources/Phoenix/Channel.swift @@ -157,8 +157,8 @@ public final class Channel: Publisher { "channel.errored: oldstate=%{public}@ error=%s", log: .phoenix, type: .error, - state.debugDescription, - error.localizedDescription + state.description, + String(describing: error) ) self.state = .errored(error) @@ -213,7 +213,7 @@ extension Channel { log: .phoenix, type: .debug, topic, - state.debugDescription + state.description ) switch state { @@ -266,7 +266,7 @@ extension Channel { log: .phoenix, type: .debug, topic, - state.debugDescription + state.description ) switch state { @@ -345,26 +345,12 @@ extension Channel { private func send(_ message: OutgoingMessage, completionHandler: @escaping Socket.Callback) { guard let socket = socket else { - os_log( - "channel.send with nil socket: topic=%s message=%s", - log: .phoenix, - type: .debug, - topic, - message.debugDescription - ) + os_log("channel.send with nil socket: topic=%s", log: .phoenix, type: .debug, topic) errored(Channel.Error.lostSocket) completionHandler(Channel.Error.lostSocket) return } - os_log( - "channel.send: topic=%s message=%s", - log: .phoenix, - type: .debug, - topic, - message.debugDescription - ) - socket.send(message) { error in if let error = error { switch error { @@ -376,7 +362,7 @@ extension Channel { "channel.send error: error=%s", log: .phoenix, type: .error, - error.localizedDescription + String(describing: error) ) // NOTE: we don't change state to error here, instead we let the socket close do that for us } @@ -535,16 +521,16 @@ public extension Channel { // MARK: Socket Subscriber extension Channel { - enum ChannelSpecificSocketMessage: CustomDebugStringConvertible { + enum ChannelSpecificSocketMessage: CustomStringConvertible { case socketOpen case socketClose case channelMessage(IncomingMessage) - var debugDescription: String { + var description: String { switch self { case .socketOpen: return "open" case .socketClose: return "close" - case let .channelMessage(msg): return msg.debugDescription + case let .channelMessage(msg): return "topic: \(msg.topic)" } } } @@ -571,14 +557,6 @@ extension Channel { fatalError("`Never` means never") } let receiveValue = { [weak self] (input: SocketOutput) -> Void in - os_log( - "channel.receive: topic=%s message=%s", - log: .phoenix, - type: .debug, - topic, - input.debugDescription - ) - switch input { case let .channelMessage(message): self?.handle(message) diff --git a/Sources/Phoenix/ChannelState.swift b/Sources/Phoenix/ChannelState.swift index 2bb7d6ab..b00a1571 100644 --- a/Sources/Phoenix/ChannelState.swift +++ b/Sources/Phoenix/ChannelState.swift @@ -1,12 +1,12 @@ extension Channel { - enum State: CustomDebugStringConvertible { + enum State: CustomStringConvertible { case closed case joining(Ref) case joined(Ref) case leaving(joinRef: Ref, leavingRef: Ref) case errored(Swift.Error) - var debugDescription: String { + var description: String { switch self { case .closed: return "closed" case .joining: return "joining" diff --git a/Sources/Phoenix/Foundation+Phoenix.swift b/Sources/Phoenix/Foundation+Phoenix.swift index 702bf173..cbb41d1d 100644 --- a/Sources/Phoenix/Foundation+Phoenix.swift +++ b/Sources/Phoenix/Foundation+Phoenix.swift @@ -50,8 +50,8 @@ extension String { } } -extension DispatchTimeoutResult: CustomDebugStringConvertible { - public var debugDescription: String { +extension DispatchTimeoutResult: CustomStringConvertible { + public var description: String { switch self { case .success: return "success" case .timedOut: return "timedOut" diff --git a/Sources/Phoenix/IncomingMessage.swift b/Sources/Phoenix/IncomingMessage.swift index e7657ead..c7f1d4be 100644 --- a/Sources/Phoenix/IncomingMessage.swift +++ b/Sources/Phoenix/IncomingMessage.swift @@ -1,19 +1,19 @@ import Foundation import WebSocketProtocol -public enum RawIncomingMessage: CustomDebugStringConvertible, Hashable { +public enum RawIncomingMessage: CustomStringConvertible, Hashable { case binary(Data) case text(String) - public var debugDescription: String { + public var description: String { switch self { - case let .binary(data): return "\(data.count) bytes" + case let .binary(data): return data.base64EncodedString() case let .text(text): return text } } } -public struct IncomingMessage: CustomDebugStringConvertible { +public struct IncomingMessage { enum DecodingError: Error { case invalidType(Any?) case missingValue(String) @@ -85,13 +85,6 @@ public struct IncomingMessage: CustomDebugStringConvertible { self.event = event self.payload = payload } - - public var debugDescription: String { - let jr = joinRef?.debugDescription ?? "" - let r = ref?.debugDescription ?? "" - let e = event.stringValue - return "[\(jr),\(r),\"\(topic)\",\"\(e)\",\(payload._debugDescription)]" - } } private func _ref(_ object: Any?) -> Phoenix.Ref? { diff --git a/Sources/Phoenix/OutgoingMessage.swift b/Sources/Phoenix/OutgoingMessage.swift index 01b73e9c..a4bc6f56 100644 --- a/Sources/Phoenix/OutgoingMessage.swift +++ b/Sources/Phoenix/OutgoingMessage.swift @@ -13,7 +13,7 @@ public enum RawOutgoingMessage: CustomStringConvertible, Hashable { } } -public struct OutgoingMessage: CustomDebugStringConvertible { +public struct OutgoingMessage { public var joinRef: Ref? public var ref: Ref public var topic: Topic @@ -71,11 +71,4 @@ public struct OutgoingMessage: CustomDebugStringConvertible { let data = try JSONSerialization.data(withJSONObject: array, options: []) return .text(try String(data: data, encoding: .utf8)) } - - public var debugDescription: String { - let jr = joinRef?.debugDescription ?? "" - let r = ref.debugDescription - let e = event.stringValue - return "[\(jr),\(r),\"\(topic)\",\"\(e)\",\(payload._debugDescription)]" - } } diff --git a/Sources/Phoenix/Payload.swift b/Sources/Phoenix/Payload.swift index 619c6bdf..5e5569e3 100644 --- a/Sources/Phoenix/Payload.swift +++ b/Sources/Phoenix/Payload.swift @@ -1,18 +1,3 @@ import Foundation public typealias Payload = [String: Any] - -extension Dictionary where Key == String, Value == Any { - var _debugDescription: String { - let contents = map { key, value in - let maxLength = 32 - let valueStr = String(describing: value) - let truncatedValue = valueStr.count > maxLength ? - "\(valueStr.prefix(maxLength))..." : - valueStr - return "\(key):\(truncatedValue)" - } - .joined(separator: ",") - return "{\(contents)}" - } -} diff --git a/Sources/Phoenix/Ref.swift b/Sources/Phoenix/Ref.swift index c93169dd..5cdf3333 100644 --- a/Sources/Phoenix/Ref.swift +++ b/Sources/Phoenix/Ref.swift @@ -1,6 +1,6 @@ import Synchronized -public struct Ref: Comparable, CustomDebugStringConvertible, Hashable, ExpressibleByIntegerLiteral { +public struct Ref: Comparable, CustomStringConvertible, Hashable, ExpressibleByIntegerLiteral { public let rawValue: UInt64 public init(_ rawValue: UInt64) { @@ -11,7 +11,7 @@ public struct Ref: Comparable, CustomDebugStringConvertible, Hashable, Expressib rawValue = value } - public var debugDescription: String { "\(rawValue)" } + public var description: String { "\(rawValue)" } public static func == (lhs: Ref, rhs: Ref) -> Bool { lhs.rawValue == rhs.rawValue diff --git a/Sources/Phoenix/Socket.swift b/Sources/Phoenix/Socket.swift index 8f9b0ecf..1066c5d6 100644 --- a/Sources/Phoenix/Socket.swift +++ b/Sources/Phoenix/Socket.swift @@ -108,7 +108,7 @@ public final class Socket { return true } } - var connectionState: String { sync { state.debugDescription } } + var connectionState: String { sync { state.description } } public init( url: URL, @@ -233,7 +233,7 @@ extension Socket: ConnectablePublisher { "socket.disconnect: oldstate=%{public}@", log: .phoenix, type: .debug, - state.debugDescription + state.description ) shouldReconnect = false @@ -282,7 +282,7 @@ extension Socket: ConnectablePublisher { "socket.disconnectAndWait: result=%{public}@", log: .phoenix, type: .debug, - result.debugDescription + result.description ) return result @@ -372,13 +372,6 @@ public extension Socket { callback: callback ) - os_log( - "socket.push: message=%s", - log: .phoenix, - type: .debug, - thePush.debugDescription - ) - sync { pending.append(thePush) } @@ -440,24 +433,15 @@ extension Socket { func send(_ string: String, completionHandler: @escaping Callback) { sync { - os_log( - "socket.send: message=%s state=%{public}@", - log: .phoenix, - type: .debug, - string, - state.debugDescription - ) - switch state { case let .open(ws): ws.send(string) { error in if let error = error { os_log( - "socket.send.error: message=%s error=%s", + "socket.send.text.error: error=%s", log: .phoenix, - type: .debug, - string, - error.localizedDescription + type: .error, + String(describing: error) ) self.state = .closing(ws) // TODO: write a test to prove this works @@ -481,24 +465,15 @@ extension Socket { func send(_ data: Data, completionHandler: @escaping Callback) { sync { - os_log( - "socket.send: %lld bytes state=%{public}@", - log: .phoenix, - type: .debug, - data.count, - state.debugDescription - ) - switch state { case let .open(ws): ws.send(data) { error in if let error = error { os_log( - "socket.send.error: %lld bytes error=%s", + "socket.send.data.error: error=%s", log: .phoenix, - type: .debug, - data.count, - error.localizedDescription + type: .error, + String(describing: error) ) self.state = .closing(ws) // TODO: write a test to prove this works @@ -553,7 +528,7 @@ extension Socket { "socket.heartbeatTimeout: oldstate=%{public}@", log: .phoenix, type: .debug, - state.debugDescription + state.description ) self.pendingHeartbeatRef = nil @@ -624,12 +599,12 @@ extension Socket { "socket.receive could not decode message: message=%s error=%s", log: .phoenix, type: .error, - rawMessage.debugDescription, + rawMessage.description, error.localizedDescription ) notifySubjectQueue.async { - subject.send(.unreadableMessage(rawMessage.debugDescription)) + subject.send(.unreadableMessage(rawMessage.description)) } } } @@ -639,19 +614,12 @@ extension Socket { os_log( "socket.receive.failure: error=%s", log: .phoenix, - type: .debug, - error.localizedDescription + type: .error, + String(describing: error) ) notifySubjectQueue.async { subject.send(.websocketError(error)) } case let .success(message): - os_log( - "socket.receive.success: message=%s", - log: .phoenix, - type: .debug, - message.debugDescription - ) - switch message { case .open: sync { @@ -663,7 +631,7 @@ extension Socket { "socket.receive open in wrong state: state=%{public}@", log: .phoenix, type: .error, - state.debugDescription + state.description ) return @@ -686,14 +654,6 @@ extension Socket { private func receive(completion: Subscribers.Completion) { sync { - os_log( - "socket.receiveCompletion: oldstate=%{public}@ completion=%s", - log: .phoenix, - type: .debug, - state.debugDescription, - completion._debugDescription - ) - switch state { case .closed: return @@ -709,15 +669,3 @@ extension Socket { } } } - -private extension Subscribers.Completion where Failure == Socket.WebSocketFailure { - var _debugDescription: String { - switch self { - case .finished: - return "finished" - case let .failure(error): - let description = (error as NSError).description - return description.isEmpty ? description : "failure" - } - } -} diff --git a/Sources/Phoenix/SocketPush.swift b/Sources/Phoenix/SocketPush.swift index 55ac850a..0011236f 100644 --- a/Sources/Phoenix/SocketPush.swift +++ b/Sources/Phoenix/SocketPush.swift @@ -3,7 +3,7 @@ import Foundation extension Socket { public typealias Callback = (Swift.Error?) -> Void - struct Push: CustomDebugStringConvertible { + struct Push { public let topic: Topic public let event: PhxEvent public let payload: Payload @@ -21,9 +21,5 @@ extension Socket { DispatchQueue.backgroundQueue.async { cb(error) } } } - - var debugDescription: String { - "[\(topic),\(event.stringValue),\(payload)]" - } } } diff --git a/Sources/Phoenix/SocketState.swift b/Sources/Phoenix/SocketState.swift index f45496db..de2ac0eb 100644 --- a/Sources/Phoenix/SocketState.swift +++ b/Sources/Phoenix/SocketState.swift @@ -1,7 +1,7 @@ import WebSocket extension Socket { - enum State: CustomDebugStringConvertible { + enum State: CustomStringConvertible { case closed case connecting(WebSocket) case open(WebSocket) @@ -16,7 +16,7 @@ extension Socket { } } - var debugDescription: String { + var description: String { switch self { case .closed: return "closed" case .connecting: return "connecting"