Skip to content
This repository was archived by the owner on Jun 13, 2023. It is now read-only.

Commit c2e6bbd

Browse files
JoeSzymanskiflovilmart
authored andcommitted
Update logging for WebSockets (#138)
* Update logging for WebSockets * Convert all logs from print()/debugPrint() to NSLog() to support debugging on devices * Add opt-in trace level logging of all sent and received messages with a new property "shouldPrintWebSocketTrace" added to allow turning the new logs on * Add description for "shouldPrintWebSocketTrace" in README * Fix build errors for logging updates
1 parent 8715b15 commit c2e6bbd

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ By default, it will print the logs from WebSocket / WebSocketDelegate. You can t
5959
Client.shared.shouldPrintWebSocketLog = false
6060
```
6161

62+
You can also enable socket trace messages for all sent and received strings. By default, these trace messages are disabled.
63+
```swift
64+
Client.shared.shouldPrintWebSocketTrace = true
65+
```
66+
6267
Handling errors is and other events is similar, take a look at the `Subscription` class for more information.
6368

6469
## Advanced Usage

Sources/ParseLiveQuery/Client.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ open class Client: NSObject {
2424

2525
var socket: WebSocket?
2626
public var shouldPrintWebSocketLog = true
27+
public var shouldPrintWebSocketTrace = false
2728
public var userDisconnected = false
2829
var isConnecting = false
2930

@@ -159,7 +160,7 @@ extension Client {
159160
if !userDisconnected {
160161
reconnect()
161162
} else {
162-
debugPrint("Warning: The client was explicitly disconnected! You must explicitly call .reconnect() in order to process your subscriptions.")
163+
NSLog("ParseLiveQuery: Warning: The client was explicitly disconnected! You must explicitly call .reconnect() in order to process your subscriptions.")
163164
}
164165
}
165166

Sources/ParseLiveQuery/Internal/ClientPrivate.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,13 @@ func == (first: Client.RequestId, second: Client.RequestId) -> Bool {
117117
extension Client: WebSocketDelegate {
118118

119119
public func websocketDidReceiveData(socket: WebSocket, data: Data) {
120-
if shouldPrintWebSocketLog { print("Received binary data but we don't handle it...") }
120+
if shouldPrintWebSocketLog { NSLog("ParseLiveQuery: Received binary data but we don't handle it...") }
121121
}
122122

123123
public func websocketDidReceiveMessage(socket: WebSocket, text: String) {
124124
handleOperationAsync(text).continueWith { [weak self] task in
125125
if let error = task.error, self?.shouldPrintWebSocketLog == true {
126-
print("Error: \(error)")
126+
NSLog("ParseLiveQuery: Error processing message: \(error)")
127127
}
128128
}
129129
}
@@ -136,7 +136,7 @@ extension Client: WebSocketDelegate {
136136

137137
public func websocketDidDisconnect(socket: WebSocket, error: NSError?) {
138138
isConnecting = false
139-
if shouldPrintWebSocketLog { print("error: \(String(describing: error))") }
139+
if shouldPrintWebSocketLog { NSLog("ParseLiveQuery: WebSocket did disconnect with error: \(String(describing: error))") }
140140

141141
// TODO: Better retry logic, unless `disconnect()` was explicitly called
142142
if !userDisconnected {
@@ -146,7 +146,7 @@ extension Client: WebSocketDelegate {
146146

147147
public func webSocket(_ webSocket: WebSocket, didCloseWithCode code: Int, reason: String?, wasClean: Bool) {
148148
isConnecting = false
149-
if shouldPrintWebSocketLog { print("code: \(code) reason: \(String(describing: reason))") }
149+
if shouldPrintWebSocketLog { NSLog("ParseLiveQuery: WebSocket did close with code: \(code) reason: \(String(describing: reason))") }
150150

151151
// TODO: Better retry logic, unless `disconnect()` was explicitly called
152152
if !userDisconnected {
@@ -202,12 +202,14 @@ extension Client {
202202
let jsonEncoded = operation.JSONObjectRepresentation
203203
let jsonData = try JSONSerialization.data(withJSONObject: jsonEncoded, options: JSONSerialization.WritingOptions(rawValue: 0))
204204
let jsonString = String(data: jsonData, encoding: String.Encoding.utf8)
205+
if self.shouldPrintWebSocketTrace { NSLog("ParseLiveQuery: Sending message: \(jsonString!)") }
205206
self.socket?.write(string: jsonString!)
206207
}
207208
}
208209

209210
func handleOperationAsync(_ string: String) -> Task<Void> {
210211
return Task(.queue(queue)) {
212+
if self.shouldPrintWebSocketTrace { NSLog("ParseLiveQuery: Received message: \(string)") }
211213
guard
212214
let jsonData = string.data(using: String.Encoding.utf8),
213215
let jsonDecoded = try JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions(rawValue: 0))
@@ -217,8 +219,6 @@ extension Client {
217219
throw LiveQueryErrors.InvalidResponseError(response: string)
218220
}
219221

220-
221-
222222
switch response {
223223
case .connected:
224224
let sessionToken = PFUser.current()?.sessionToken

0 commit comments

Comments
 (0)