Skip to content

Commit 6551342

Browse files
Merge pull request #323 from pusher/feature/198-remove-print-logging
Refactor 'print()' logging to use 'PusherLogger'
2 parents d405b36 + 16740cc commit 6551342

File tree

7 files changed

+109
-88
lines changed

7 files changed

+109
-88
lines changed

Sources/Extensions/PusherWebsocketDelegate.swift

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,20 @@ extension PusherConnection: WebSocketConnectionDelegate {
1111
- parameter string: The message received over the websocket
1212
*/
1313
public func webSocketDidReceiveMessage(connection: WebSocketConnection, string: String) {
14-
self.delegate?.debugLog?(message: PusherLogger.debug(for: .receivedMessage, context: string))
14+
PusherLogger.shared.debug(for: .receivedMessage, context: string)
1515

1616
guard let payload = PusherParser.getPusherEventJSON(from: string),
1717
let event = payload[Constants.JSONKeys.event] as? String
1818
else {
19-
self.delegate?.debugLog?(message: PusherLogger.debug(for: .unableToHandleIncomingMessage, context: string))
19+
PusherLogger.shared.debug(for: .unableToHandleIncomingMessage,
20+
context: string)
2021
return
2122
}
2223

2324
if event == Constants.Events.Pusher.error {
2425
guard let error = PusherError(jsonObject: payload) else {
25-
self.delegate?.debugLog?(message: PusherLogger.debug(for: .unableToHandleIncomingError,
26-
context: string))
26+
PusherLogger.shared.debug(for: .unableToHandleIncomingError,
27+
context: string)
2728
return
2829
}
2930
self.handleError(error: error)
@@ -35,7 +36,7 @@ extension PusherConnection: WebSocketConnectionDelegate {
3536
/// Delegate method called when a pong is received over a websocket
3637
/// - Parameter connection: The websocket that has received the pong
3738
public func webSocketDidReceivePong(connection: WebSocketConnection) {
38-
self.delegate?.debugLog?(message: PusherLogger.debug(for: .pongReceived))
39+
PusherLogger.shared.debug(for: .pongReceived)
3940
resetActivityTimeoutTimer()
4041
}
4142

@@ -63,7 +64,7 @@ extension PusherConnection: WebSocketConnectionDelegate {
6364
updateConnectionState(to: .disconnected)
6465

6566
guard !intentionalDisconnect else {
66-
self.delegate?.debugLog?(message: PusherLogger.debug(for: .intentionalDisconnection))
67+
PusherLogger.shared.debug(for: .intentionalDisconnection)
6768
return
6869
}
6970

@@ -81,7 +82,7 @@ extension PusherConnection: WebSocketConnectionDelegate {
8182
}
8283

8384
guard reconnectAttemptsMax == nil || reconnectAttempts < reconnectAttemptsMax! else {
84-
self.delegate?.debugLog?(message: PusherLogger.debug(for: .maxReconnectAttemptsLimitReached))
85+
PusherLogger.shared.debug(for: .maxReconnectAttemptsLimitReached)
8586
return
8687
}
8788

@@ -90,9 +91,9 @@ extension PusherConnection: WebSocketConnectionDelegate {
9091

9192
public func webSocketViabilityDidChange(connection: WebSocketConnection, isViable: Bool) {
9293
if isViable {
93-
self.delegate?.debugLog?(message: PusherLogger.debug(for: .networkConnectionViable))
94+
PusherLogger.shared.debug(for: .networkConnectionViable)
9495
} else {
95-
self.delegate?.debugLog?(message: PusherLogger.debug(for: .networkConnectionUnviable))
96+
PusherLogger.shared.debug(for: .networkConnectionUnviable)
9697
}
9798
}
9899

@@ -101,10 +102,10 @@ extension PusherConnection: WebSocketConnectionDelegate {
101102
case .success:
102103
updateConnectionState(to: .reconnecting)
103104
case .failure(let error):
104-
self.delegate?.debugLog?(message: PusherLogger.debug(for: .errorReceived,
105-
context: """
105+
PusherLogger.shared.debug(for: .errorReceived,
106+
context: """
106107
Path migration error: \(error.debugDescription)
107-
"""))
108+
""")
108109
}
109110
}
110111

@@ -187,8 +188,8 @@ extension PusherConnection: WebSocketConnectionDelegate {
187188
context = "\(timeInterval) seconds " + context
188189
}
189190

190-
self.delegate?.debugLog?(message: PusherLogger.debug(for: loggingEvent,
191-
context: context))
191+
PusherLogger.shared.debug(for: loggingEvent,
192+
context: context)
192193
}
193194

194195
/// Logs the websocket disconnection event.
@@ -214,8 +215,8 @@ extension PusherConnection: WebSocketConnectionDelegate {
214215
closeMessage += " Reason: \(reasonString)."
215216
}
216217

217-
self.delegate?.debugLog?(message: PusherLogger.debug(for: .disconnectionWithoutError,
218-
context: closeMessage))
218+
PusherLogger.shared.debug(for: .disconnectionWithoutError,
219+
context: closeMessage)
219220
}
220221

221222
/**
@@ -232,9 +233,9 @@ extension PusherConnection: WebSocketConnectionDelegate {
232233
}
233234

234235
public func webSocketDidReceiveError(connection: WebSocketConnection, error: NWError) {
235-
self.delegate?.debugLog?(message: PusherLogger.debug(for: .errorReceived,
236-
context: """
236+
PusherLogger.shared.debug(for: .errorReceived,
237+
context: """
237238
Error: \(error.debugDescription)
238-
"""))
239+
""")
239240
}
240241
}

Sources/Helpers/PusherLogger.swift

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,32 @@ internal class PusherLogger {
1313
case presenceChannelSubscriptionAttemptWithoutChannelData =
1414
"Attempting to subscribe to presence channel but no channelData value provided"
1515
case subscriptionSucceededNoDataInPayload = "Subscription succeeded event received without data key in payload"
16+
case unableToSubscribeToChannel = "Unable to subscribe to channel:"
17+
case unableToAddMemberToChannel = "Unable to add member to channel"
18+
case unableToRemoveMemberFromChannel = "Unable to remove member from channel"
19+
case authInfoForCompletionHandlerIsNil = "Auth info passed to authorizer completionHandler was nil"
20+
case authenticationFailed = "Authentication failed. You may not be connected"
21+
case authValueOnSubscriptionNotSupported = """
22+
Passing an auth value to 'subscribe' is not supported for encrypted channels. \
23+
Event decryption will fail. You must use one of the following auth methods: \
24+
'endpoint', 'authRequestBuilder', 'authorizer'
25+
"""
1626

1727
// Events
1828

1929
case clientEventSent = "sendClientEvent"
2030
case eventSent = "sendEvent"
2131
case skippedEventAfterDecryptionFailure = "Skipping event that failed to decrypt on channel"
32+
case cannotSendClientEventForChannel = "You must be subscribed to a private or presence channel to send client events"
33+
case clientEventsNotSupported = "Client events are not supported on encrypted channels:"
34+
35+
// JSON parsing
36+
37+
case unableToParseStringAsJSON = "Unable to parse string as JSON:"
38+
39+
// Misc
40+
41+
case genericError = ""
2242

2343
// Network
2444

@@ -48,63 +68,62 @@ internal class PusherLogger {
4868
case error = "[PUSHER ERROR]"
4969
}
5070

71+
internal static let shared = PusherLogger()
72+
73+
internal weak var delegate: PusherDelegate?
74+
5175
// MARK: - Event logging
5276

53-
/// A debug message relating to a particular event of interest.
77+
/// Logs a debug message relating to a particular event of interest.
5478
/// - Parameters:
5579
/// - event: A particular `LoggingEvent` of interest.
5680
/// - context: Additional context for the message.
57-
/// - Returns: A `String` with information to log concerning the event.
58-
internal static func debug(for event: LoggingEvent,
59-
context: CustomStringConvertible? = nil) -> String {
60-
return message(for: event, level: .debug, context: context)
81+
internal func debug(for event: LoggingEvent,
82+
context: CustomStringConvertible? = nil) {
83+
message(for: event, level: .debug, context: context)
6184
}
6285

63-
/// An informational message relating to a particular event of interest.
86+
/// Logs an informational message relating to a particular event of interest.
6487
/// - Parameters:
6588
/// - event: A particular `LoggingEvent` of interest.
6689
/// - context: Additional context for the message.
67-
/// - Returns: A `String` with information to log concerning the event.
68-
internal static func info(for event: LoggingEvent,
69-
context: CustomStringConvertible? = nil) -> String {
70-
return message(for: event, level: .info, context: context)
90+
internal func info(for event: LoggingEvent,
91+
context: CustomStringConvertible? = nil) {
92+
message(for: event, level: .info, context: context)
7193
}
7294

73-
/// A warning message relating to a particular event of interest.
95+
/// Logs a warning message relating to a particular event of interest.
7496
/// - Parameters:
7597
/// - event: A particular `LoggingEvent` of interest.
7698
/// - context: Additional context for the message.
77-
/// - Returns: A `String` with information to log concerning the event.
78-
internal static func warning(for event: LoggingEvent,
79-
context: CustomStringConvertible? = nil) -> String {
80-
return message(for: event, level: .warning, context: context)
99+
internal func warning(for event: LoggingEvent,
100+
context: CustomStringConvertible? = nil) {
101+
message(for: event, level: .warning, context: context)
81102
}
82103

83-
/// An error message relating to a particular event of interest.
104+
/// Logs an error message relating to a particular event of interest.
84105
/// - Parameters:
85106
/// - event: A particular `LoggingEvent` of interest.
86107
/// - context: Additional context for the message.
87-
/// - Returns: A `String` with information to log concerning the event.
88-
internal static func error(for event: LoggingEvent,
89-
context: CustomStringConvertible? = nil) -> String {
90-
return message(for: event, level: .error, context: context)
108+
internal func error(for event: LoggingEvent,
109+
context: CustomStringConvertible? = nil) {
110+
message(for: event, level: .error, context: context)
91111
}
92112

93113
// MARK: - Private methods
94114

95-
/// An informational message relating to a particular event of interest.
115+
/// Logs an informational message relating to a particular event of interest.
96116
/// - Parameter event: A particular `LoggingEvent` of interest.
97117
/// - Parameter level: The `LoggingLevel` to set for the message.
98118
/// - Parameter context: Additional context for the message.
99-
/// - Returns: A `String` with information to log concerning the event.
100-
private static func message(for event: LoggingEvent,
101-
level: LoggingLevel,
102-
context: CustomStringConvertible? = nil) -> String {
119+
private func message(for event: LoggingEvent,
120+
level: LoggingLevel,
121+
context: CustomStringConvertible? = nil) {
103122
var message = "\(level.rawValue) \(event.rawValue)"
104123
if let context = context {
105124
message += " \(context)"
106125
}
107126

108-
return message
127+
self.delegate?.debugLog?(message: message)
109128
}
110129
}

Sources/Helpers/PusherParser.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ internal struct PusherParser {
2020
options: []) as? [String: AnyObject] {
2121
return jsonObject
2222
} else {
23-
print("Unable to parse string from WebSocket: \(string)")
23+
PusherLogger.shared.debug(for: .unableToParseStringAsJSON,
24+
context: string)
2425
}
2526
} catch let error as NSError {
26-
print("Error: \(error.localizedDescription)")
27+
PusherLogger.shared.error(for: .genericError,
28+
context: error.localizedDescription)
2729
}
2830
return nil
2931
}
@@ -42,7 +44,8 @@ internal struct PusherParser {
4244
if let jsonData = data, let jsonObject = try? JSONSerialization.jsonObject(with: jsonData, options: []) {
4345
return jsonObject
4446
} else {
45-
print("Unable to parse string as JSON - check that your JSON is valid.")
47+
PusherLogger.shared.debug(for: .unableToParseStringAsJSON,
48+
context: string)
4649
}
4750
}
4851
return nil

Sources/Models/PusherChannel.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,9 @@ open class PusherChannel: NSObject {
172172
*/
173173
open func trigger(eventName: String, data: Any) {
174174
if PusherEncryptionHelpers.isEncryptedChannel(channelName: self.name) {
175-
let error = """
176-
ERROR: Client events are not supported on encrypted channels: '\(self.name)'. \
177-
Client event '\(eventName)' will not be sent.
178-
"""
179-
print(error)
175+
let context = "'\(self.name)'. Client event '\(eventName)' will not be sent"
176+
PusherLogger.shared.error(for: .clientEventsNotSupported,
177+
context: context)
180178
return
181179
}
182180

Sources/Models/PusherPresenceChannel.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,12 @@ public typealias PusherUserInfoObject = [String: AnyObject]
138138
options: []) as? [String: AnyObject] {
139139
return jsonObject
140140
} else {
141-
print("Unable to parse string: \(channelData)")
141+
PusherLogger.shared.debug(for: .unableToParseStringAsJSON,
142+
context: channelData)
142143
}
143144
} catch let error as NSError {
144-
print(error.localizedDescription)
145+
PusherLogger.shared.error(for: .genericError,
146+
context: error.localizedDescription)
145147
}
146148
return nil
147149
}

Sources/PusherSwift.swift

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,7 @@ let CLIENT_NAME = "pusher-websocket-swift"
5757
let isEncryptedChannel = PusherEncryptionHelpers.isEncryptedChannel(channelName: channelName)
5858

5959
if isEncryptedChannel && auth != nil {
60-
let error = """
61-
62-
WARNING: Passing an auth value to 'subscribe' is not supported for encrypted channels. \
63-
Event decryption will fail. You must use one of the following auth methods: \
64-
'endpoint', 'authRequestBuilder', 'authorizer'
65-
66-
"""
67-
print(error)
60+
PusherLogger.shared.warning(for: .authValueOnSubscriptionNotSupported)
6861
}
6962

7063
return self.connection.subscribe(

0 commit comments

Comments
 (0)