Skip to content

Commit d982cdc

Browse files
committed
only do interpolation when logging
1 parent 079063e commit d982cdc

File tree

4 files changed

+49
-28
lines changed

4 files changed

+49
-28
lines changed

SocketIOClientSwift/SocketEngine.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
116116
}
117117

118118
public func close(#fast:Bool) {
119-
SocketLogger.log("Engine is being closed. Fast: \(fast)", client: self)
119+
SocketLogger.log("Engine is being closed. Fast: %@", client: self, args: fast)
120120

121121
pingTimer?.invalidate()
122122
closed = true
@@ -324,7 +324,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
324324

325325
waitingForPost = true
326326

327-
SocketLogger.log("POSTing: \(postStr)", client: self)
327+
SocketLogger.log("POSTing: %@", client: self, args: postStr)
328328

329329
session.dataTaskWithRequest(req) {[weak self] data, res, err in
330330
if let this = self {
@@ -509,15 +509,15 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
509509
length += chr
510510
} else {
511511
if length == "" || testLength(length, &n) {
512-
SocketLogger.err("Parsing error: \(str)", client: self)
512+
SocketLogger.err("Parsing error: %@", client: self, args: str)
513513
handlePollingFailed("Error parsing XHR message")
514514
return
515515
}
516516

517517
msg = String(strArray[i+1...i+n])
518518

519519
if let lengthInt = length.toInt() where lengthInt != count(msg) {
520-
SocketLogger.err("parsing error: \(str)", client: self)
520+
SocketLogger.err("Parsing error: %@", client: self, args: str)
521521
return
522522
}
523523

@@ -543,7 +543,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
543543
}
544544

545545
private func parseEngineMessage(var message:String, fromPolling:Bool) {
546-
SocketLogger.log("Got message: \(message)", client: self)
546+
SocketLogger.log("Got message: %@", client: self, args: message)
547547

548548
if fromPolling {
549549
fixDoubleUTF8(&message)
@@ -603,7 +603,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
603603
/// Only call on emitQueue
604604
private func sendPollMessage(var msg:String, withType type:PacketType,
605605
datas:ContiguousArray<NSData>? = nil) {
606-
SocketLogger.log("Sending poll: \(msg) as type: \(type.rawValue)", client: self)
606+
SocketLogger.log("Sending poll: %@ as type: %@", client: self, args: msg, type.rawValue)
607607

608608
doubleEncodeUTF8(&msg)
609609
let strMsg = "\(type.rawValue)\(msg)"
@@ -627,7 +627,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
627627
/// Only call on emitQueue
628628
private func sendWebSocketMessage(str:String, withType type:PacketType,
629629
datas:ContiguousArray<NSData>? = nil) {
630-
SocketLogger.log("Sending ws: \(str) as type: \(type.rawValue)", client: self)
630+
SocketLogger.log("Sending ws: %@ as type: %@", client: self, args: str, type.rawValue)
631631

632632
ws?.writeString("\(type.rawValue)\(str)")
633633

@@ -678,10 +678,12 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
678678
dispatch_async(emitQueue) {[weak self] in
679679
if let this = self where this.connected {
680680
if this.websocket {
681-
SocketLogger.log("Writing ws: \(msg):\(data)", client: this)
681+
SocketLogger.log("Writing ws: %@ has data: %@", client: this,
682+
args: msg, data == nil ? false : true)
682683
this.sendWebSocketMessage(msg, withType: type, datas: data)
683684
} else {
684-
SocketLogger.log("Writing poll: \(msg):\(data)", client: this)
685+
SocketLogger.log("Writing poll: %@ has data: %@", client: this,
686+
args: msg, data == nil ? false : true)
685687
this.sendPollMessage(msg, withType: type, datas: data)
686688
}
687689
}

SocketIOClientSwift/SocketIOClient.swift

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
235235
return
236236
}
237237

238-
SocketLogger.log("Disconnected: \(reason)", client: self)
238+
SocketLogger.log("Disconnected: %@", client: self, args: reason)
239239

240240
_closed = true
241241
_connected = false
@@ -250,7 +250,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
250250

251251
/// error
252252
public func didError(reason:AnyObject) {
253-
SocketLogger.err("Error: \(reason)", client: self)
253+
SocketLogger.err("%@", client: self, args: reason)
254254

255255
handleEvent("error", data: reason as? [AnyObject] ?? [reason],
256256
isInternalMessage: true)
@@ -323,7 +323,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
323323
SocketParser.parseForEmit(packet)
324324
str = packet.createMessageForEvent(event)
325325

326-
SocketLogger.log("Emitting: \(str)", client: self)
326+
SocketLogger.log("Emitting: %@", client: self, args: str)
327327

328328
if packet.type == SocketPacket.PacketType.BINARY_EVENT {
329329
engine?.send(str, withData: packet.binary)
@@ -342,7 +342,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
342342
SocketParser.parseForEmit(packet)
343343
str = packet.createAck()
344344

345-
SocketLogger.log("Emitting Ack: \(str)", client: this)
345+
SocketLogger.log("Emitting Ack: %@", client: this, args: str)
346346

347347
if packet.type == SocketPacket.PacketType.BINARY_ACK {
348348
this.engine?.send(str, withData: packet.binary)
@@ -368,7 +368,8 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
368368

369369
// Called when the socket gets an ack for something it sent
370370
func handleAck(ack:Int, data:AnyObject?) {
371-
SocketLogger.log("Handling ack: \(ack) with data: \(data)", client: self)
371+
SocketLogger.log("Handling ack: %@ with data: %@", client: self,
372+
args: ack, data ?? "")
372373

373374
ackHandlers.executeAck(ack,
374375
items: (data as? [AnyObject]?) ?? (data != nil ? [data!] : nil))
@@ -384,7 +385,8 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
384385
return
385386
}
386387

387-
SocketLogger.log("Handling event: \(event) with data: \(data)", client: self)
388+
SocketLogger.log("Handling event: %@ with data: %@", client: self,
389+
args: event, data ?? "")
388390

389391
if anyHandler != nil {
390392
dispatch_async(dispatch_get_main_queue()) {[weak self] in
@@ -428,18 +430,18 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
428430
Removes handler(s)
429431
*/
430432
public func off(event:String) {
431-
SocketLogger.log("Removing handler for event: \(event)", client: self)
433+
SocketLogger.log("Removing handler for event: %@", client: self, args: event)
432434

433435
handlers = handlers.filter {$0.event == event ? false : true}
434436
}
435437

436438
/**
437439
Adds a handler for an event.
438440
*/
439-
public func on(name:String, callback:NormalCallback) {
440-
SocketLogger.log("Adding handler for event: \(name)", client: self)
441+
public func on(event:String, callback:NormalCallback) {
442+
SocketLogger.log("Adding handler for event: %@", client: self, args: event)
441443

442-
let handler = SocketEventHandler(event: name, callback: callback)
444+
let handler = SocketEventHandler(event: event, callback: callback)
443445
handlers.append(handler)
444446
}
445447

SocketIOClientSwift/SocketLogger.swift

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
import Foundation
2626

27+
private let MESSAGE_LENGTH_MAX = 10000
28+
2729
protocol SocketLogClient {
2830
var log:Bool {get set}
2931
var logType:String {get}
@@ -32,23 +34,41 @@ protocol SocketLogClient {
3234
final class SocketLogger {
3335
private static let printQueue = dispatch_queue_create("printQueue", DISPATCH_QUEUE_SERIAL)
3436

35-
static func log(message:String, client:SocketLogClient, altType:String? = nil) {
37+
private static func shorten(item:AnyObject) -> CVarArgType {
38+
var str = toString(item)
39+
40+
if count(str) > MESSAGE_LENGTH_MAX {
41+
let endIndex = advance(str.startIndex, MESSAGE_LENGTH_MAX)
42+
43+
str = str.substringToIndex(endIndex)
44+
}
45+
46+
return str
47+
}
48+
49+
static func log(message:String, client:SocketLogClient, altType:String? = nil, args:AnyObject...) {
3650
if !client.log {
3751
return
3852
}
3953

4054
dispatch_async(printQueue) {[type = client.logType] in
41-
NSLog("%@: %@", altType ?? type, message)
55+
let newArgs = args.map(SocketLogger.shorten)
56+
let replaced = String(format: message, arguments: newArgs)
57+
58+
NSLog("%@: %@", altType ?? type, replaced)
4259
}
4360
}
4461

45-
static func err(message:String, client:SocketLogClient, altType:String? = nil) {
62+
static func err(message:String, client:SocketLogClient, altType:String? = nil, args:AnyObject...) {
4663
if !client.log {
4764
return
4865
}
4966

5067
dispatch_async(printQueue) {[type = client.logType] in
51-
NSLog("ERROR %@: %@", altType ?? type, message)
68+
let newArgs = args.map(SocketLogger.shorten)
69+
let replaced = String(format: message, arguments: newArgs)
70+
71+
NSLog("ERROR %@: %@", altType ?? type, replaced)
5272
}
5373
}
5474
}

SocketIOClientSwift/SocketParser.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ class SocketParser {
185185
return nsp == "" && socket.nsp != "/"
186186
}
187187

188-
SocketLogger.log("Parsing \(stringMessage)", client: socket, altType: "SocketParser")
188+
SocketLogger.log("Parsing %@", client: socket, altType: "SocketParser", args: stringMessage)
189189

190190
let p:SocketPacket
191191

@@ -196,10 +196,7 @@ class SocketParser {
196196
return
197197
}
198198

199-
// Don't call SocketPacket.description unless we need to
200-
if socket.log {
201-
SocketLogger.log("Decoded packet as: \(p)", client: socket, altType: "SocketParser")
202-
}
199+
SocketLogger.log("Decoded packet as: %@", client: socket, altType: "SocketParser", args: p)
203200

204201
if p.type == SocketPacket.PacketType.EVENT {
205202
if checkNSP(p.nsp) {

0 commit comments

Comments
 (0)