only do interpolation when logging
This commit is contained in:
parent
079063e396
commit
d982cdc1b2
@ -116,7 +116,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
||||
}
|
||||
|
||||
public func close(#fast:Bool) {
|
||||
SocketLogger.log("Engine is being closed. Fast: \(fast)", client: self)
|
||||
SocketLogger.log("Engine is being closed. Fast: %@", client: self, args: fast)
|
||||
|
||||
pingTimer?.invalidate()
|
||||
closed = true
|
||||
@ -324,7 +324,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
||||
|
||||
waitingForPost = true
|
||||
|
||||
SocketLogger.log("POSTing: \(postStr)", client: self)
|
||||
SocketLogger.log("POSTing: %@", client: self, args: postStr)
|
||||
|
||||
session.dataTaskWithRequest(req) {[weak self] data, res, err in
|
||||
if let this = self {
|
||||
@ -509,7 +509,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
||||
length += chr
|
||||
} else {
|
||||
if length == "" || testLength(length, &n) {
|
||||
SocketLogger.err("Parsing error: \(str)", client: self)
|
||||
SocketLogger.err("Parsing error: %@", client: self, args: str)
|
||||
handlePollingFailed("Error parsing XHR message")
|
||||
return
|
||||
}
|
||||
@ -517,7 +517,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
||||
msg = String(strArray[i+1...i+n])
|
||||
|
||||
if let lengthInt = length.toInt() where lengthInt != count(msg) {
|
||||
SocketLogger.err("parsing error: \(str)", client: self)
|
||||
SocketLogger.err("Parsing error: %@", client: self, args: str)
|
||||
return
|
||||
}
|
||||
|
||||
@ -543,7 +543,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
||||
}
|
||||
|
||||
private func parseEngineMessage(var message:String, fromPolling:Bool) {
|
||||
SocketLogger.log("Got message: \(message)", client: self)
|
||||
SocketLogger.log("Got message: %@", client: self, args: message)
|
||||
|
||||
if fromPolling {
|
||||
fixDoubleUTF8(&message)
|
||||
@ -603,7 +603,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
||||
/// Only call on emitQueue
|
||||
private func sendPollMessage(var msg:String, withType type:PacketType,
|
||||
datas:ContiguousArray<NSData>? = nil) {
|
||||
SocketLogger.log("Sending poll: \(msg) as type: \(type.rawValue)", client: self)
|
||||
SocketLogger.log("Sending poll: %@ as type: %@", client: self, args: msg, type.rawValue)
|
||||
|
||||
doubleEncodeUTF8(&msg)
|
||||
let strMsg = "\(type.rawValue)\(msg)"
|
||||
@ -627,7 +627,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
||||
/// Only call on emitQueue
|
||||
private func sendWebSocketMessage(str:String, withType type:PacketType,
|
||||
datas:ContiguousArray<NSData>? = nil) {
|
||||
SocketLogger.log("Sending ws: \(str) as type: \(type.rawValue)", client: self)
|
||||
SocketLogger.log("Sending ws: %@ as type: %@", client: self, args: str, type.rawValue)
|
||||
|
||||
ws?.writeString("\(type.rawValue)\(str)")
|
||||
|
||||
@ -678,10 +678,12 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
||||
dispatch_async(emitQueue) {[weak self] in
|
||||
if let this = self where this.connected {
|
||||
if this.websocket {
|
||||
SocketLogger.log("Writing ws: \(msg):\(data)", client: this)
|
||||
SocketLogger.log("Writing ws: %@ has data: %@", client: this,
|
||||
args: msg, data == nil ? false : true)
|
||||
this.sendWebSocketMessage(msg, withType: type, datas: data)
|
||||
} else {
|
||||
SocketLogger.log("Writing poll: \(msg):\(data)", client: this)
|
||||
SocketLogger.log("Writing poll: %@ has data: %@", client: this,
|
||||
args: msg, data == nil ? false : true)
|
||||
this.sendPollMessage(msg, withType: type, datas: data)
|
||||
}
|
||||
}
|
||||
|
||||
@ -235,7 +235,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
|
||||
return
|
||||
}
|
||||
|
||||
SocketLogger.log("Disconnected: \(reason)", client: self)
|
||||
SocketLogger.log("Disconnected: %@", client: self, args: reason)
|
||||
|
||||
_closed = true
|
||||
_connected = false
|
||||
@ -250,7 +250,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
|
||||
|
||||
/// error
|
||||
public func didError(reason:AnyObject) {
|
||||
SocketLogger.err("Error: \(reason)", client: self)
|
||||
SocketLogger.err("%@", client: self, args: reason)
|
||||
|
||||
handleEvent("error", data: reason as? [AnyObject] ?? [reason],
|
||||
isInternalMessage: true)
|
||||
@ -323,7 +323,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
|
||||
SocketParser.parseForEmit(packet)
|
||||
str = packet.createMessageForEvent(event)
|
||||
|
||||
SocketLogger.log("Emitting: \(str)", client: self)
|
||||
SocketLogger.log("Emitting: %@", client: self, args: str)
|
||||
|
||||
if packet.type == SocketPacket.PacketType.BINARY_EVENT {
|
||||
engine?.send(str, withData: packet.binary)
|
||||
@ -342,7 +342,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
|
||||
SocketParser.parseForEmit(packet)
|
||||
str = packet.createAck()
|
||||
|
||||
SocketLogger.log("Emitting Ack: \(str)", client: this)
|
||||
SocketLogger.log("Emitting Ack: %@", client: this, args: str)
|
||||
|
||||
if packet.type == SocketPacket.PacketType.BINARY_ACK {
|
||||
this.engine?.send(str, withData: packet.binary)
|
||||
@ -368,7 +368,8 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
|
||||
|
||||
// Called when the socket gets an ack for something it sent
|
||||
func handleAck(ack:Int, data:AnyObject?) {
|
||||
SocketLogger.log("Handling ack: \(ack) with data: \(data)", client: self)
|
||||
SocketLogger.log("Handling ack: %@ with data: %@", client: self,
|
||||
args: ack, data ?? "")
|
||||
|
||||
ackHandlers.executeAck(ack,
|
||||
items: (data as? [AnyObject]?) ?? (data != nil ? [data!] : nil))
|
||||
@ -384,7 +385,8 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
|
||||
return
|
||||
}
|
||||
|
||||
SocketLogger.log("Handling event: \(event) with data: \(data)", client: self)
|
||||
SocketLogger.log("Handling event: %@ with data: %@", client: self,
|
||||
args: event, data ?? "")
|
||||
|
||||
if anyHandler != nil {
|
||||
dispatch_async(dispatch_get_main_queue()) {[weak self] in
|
||||
@ -428,7 +430,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
|
||||
Removes handler(s)
|
||||
*/
|
||||
public func off(event:String) {
|
||||
SocketLogger.log("Removing handler for event: \(event)", client: self)
|
||||
SocketLogger.log("Removing handler for event: %@", client: self, args: event)
|
||||
|
||||
handlers = handlers.filter {$0.event == event ? false : true}
|
||||
}
|
||||
@ -436,10 +438,10 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
|
||||
/**
|
||||
Adds a handler for an event.
|
||||
*/
|
||||
public func on(name:String, callback:NormalCallback) {
|
||||
SocketLogger.log("Adding handler for event: \(name)", client: self)
|
||||
public func on(event:String, callback:NormalCallback) {
|
||||
SocketLogger.log("Adding handler for event: %@", client: self, args: event)
|
||||
|
||||
let handler = SocketEventHandler(event: name, callback: callback)
|
||||
let handler = SocketEventHandler(event: event, callback: callback)
|
||||
handlers.append(handler)
|
||||
}
|
||||
|
||||
|
||||
@ -24,6 +24,8 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
private let MESSAGE_LENGTH_MAX = 10000
|
||||
|
||||
protocol SocketLogClient {
|
||||
var log:Bool {get set}
|
||||
var logType:String {get}
|
||||
@ -32,23 +34,41 @@ protocol SocketLogClient {
|
||||
final class SocketLogger {
|
||||
private static let printQueue = dispatch_queue_create("printQueue", DISPATCH_QUEUE_SERIAL)
|
||||
|
||||
static func log(message:String, client:SocketLogClient, altType:String? = nil) {
|
||||
private static func shorten(item:AnyObject) -> CVarArgType {
|
||||
var str = toString(item)
|
||||
|
||||
if count(str) > MESSAGE_LENGTH_MAX {
|
||||
let endIndex = advance(str.startIndex, MESSAGE_LENGTH_MAX)
|
||||
|
||||
str = str.substringToIndex(endIndex)
|
||||
}
|
||||
|
||||
return str
|
||||
}
|
||||
|
||||
static func log(message:String, client:SocketLogClient, altType:String? = nil, args:AnyObject...) {
|
||||
if !client.log {
|
||||
return
|
||||
}
|
||||
|
||||
dispatch_async(printQueue) {[type = client.logType] in
|
||||
NSLog("%@: %@", altType ?? type, message)
|
||||
let newArgs = args.map(SocketLogger.shorten)
|
||||
let replaced = String(format: message, arguments: newArgs)
|
||||
|
||||
NSLog("%@: %@", altType ?? type, replaced)
|
||||
}
|
||||
}
|
||||
|
||||
static func err(message:String, client:SocketLogClient, altType:String? = nil) {
|
||||
static func err(message:String, client:SocketLogClient, altType:String? = nil, args:AnyObject...) {
|
||||
if !client.log {
|
||||
return
|
||||
}
|
||||
|
||||
dispatch_async(printQueue) {[type = client.logType] in
|
||||
NSLog("ERROR %@: %@", altType ?? type, message)
|
||||
let newArgs = args.map(SocketLogger.shorten)
|
||||
let replaced = String(format: message, arguments: newArgs)
|
||||
|
||||
NSLog("ERROR %@: %@", altType ?? type, replaced)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -185,7 +185,7 @@ class SocketParser {
|
||||
return nsp == "" && socket.nsp != "/"
|
||||
}
|
||||
|
||||
SocketLogger.log("Parsing \(stringMessage)", client: socket, altType: "SocketParser")
|
||||
SocketLogger.log("Parsing %@", client: socket, altType: "SocketParser", args: stringMessage)
|
||||
|
||||
let p:SocketPacket
|
||||
|
||||
@ -196,10 +196,7 @@ class SocketParser {
|
||||
return
|
||||
}
|
||||
|
||||
// Don't call SocketPacket.description unless we need to
|
||||
if socket.log {
|
||||
SocketLogger.log("Decoded packet as: \(p)", client: socket, altType: "SocketParser")
|
||||
}
|
||||
SocketLogger.log("Decoded packet as: %@", client: socket, altType: "SocketParser", args: p)
|
||||
|
||||
if p.type == SocketPacket.PacketType.EVENT {
|
||||
if checkNSP(p.nsp) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user