Skip to content

Commit

Permalink
Dramatically reduce the number of logs created by Phoenix (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
atdrendel authored Jul 1, 2021
1 parent 3b19b6c commit edfe2d6
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 145 deletions.
40 changes: 9 additions & 31 deletions Sources/Phoenix/Channel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -213,7 +213,7 @@ extension Channel {
log: .phoenix,
type: .debug,
topic,
state.debugDescription
state.description
)

switch state {
Expand Down Expand Up @@ -266,7 +266,7 @@ extension Channel {
log: .phoenix,
type: .debug,
topic,
state.debugDescription
state.description
)

switch state {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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)"
}
}
}
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Phoenix/ChannelState.swift
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
4 changes: 2 additions & 2 deletions Sources/Phoenix/Foundation+Phoenix.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 4 additions & 11 deletions Sources/Phoenix/IncomingMessage.swift
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -85,13 +85,6 @@ public struct IncomingMessage: CustomDebugStringConvertible {
self.event = event
self.payload = payload
}

public var debugDescription: String {
let jr = joinRef?.debugDescription ?? "<nil>"
let r = ref?.debugDescription ?? "<nil>"
let e = event.stringValue
return "[\(jr),\(r),\"\(topic)\",\"\(e)\",\(payload._debugDescription)]"
}
}

private func _ref(_ object: Any?) -> Phoenix.Ref? {
Expand Down
9 changes: 1 addition & 8 deletions Sources/Phoenix/OutgoingMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ?? "<nil>"
let r = ref.debugDescription
let e = event.stringValue
return "[\(jr),\(r),\"\(topic)\",\"\(e)\",\(payload._debugDescription)]"
}
}
15 changes: 0 additions & 15 deletions Sources/Phoenix/Payload.swift
Original file line number Diff line number Diff line change
@@ -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)}"
}
}
4 changes: 2 additions & 2 deletions Sources/Phoenix/Ref.swift
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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
Expand Down
Loading

0 comments on commit edfe2d6

Please sign in to comment.