Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
Conditionally logging debug messages in response parsers (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
camposbrunocampos authored Jan 10, 2022
1 parent 8ff8951 commit ce85c0f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Sources/PSSRedisClient/RedisClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class RedisClient: NSObject, GCDAsyncSocketDelegate, RedisMessageReceived
@objc public init(delegate: RedisManagerDelegate?, isDebugLogEnabled: Bool = true) {
self.socket = GCDAsyncSocket(delegate: nil, delegateQueue: DispatchQueue.main)
self.separator = RedisClient.convertStringIntoData(str: "\r\n")!
self.parseManager = RedisResponseParser(delegate: nil)
self.parseManager = RedisResponseParser(delegate: nil, isDebugLogEnabled: isDebugLogEnabled)
self.delegate = delegate
self.completionBlocks = Array<CompletionBlock>()
self.isDebugLogEnabled = isDebugLogEnabled
Expand Down
43 changes: 30 additions & 13 deletions Sources/PSSRedisClient/RedisResponseParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ struct RedisStringParserClassConstants {
class RedisStringParser: NSObject, RedisParserInterface {
var length: Int
var value: String?
private var isDebugLogEnabled: Bool

init(length: Int) {
init(length: Int, isDebugLogEnabled: Bool) {
self.length = length;
self.isDebugLogEnabled = isDebugLogEnabled
}

func parseLine(data: Data, parserStack: inout Array<RedisParserInterface>, results: inout Array<Any?>) {
Expand All @@ -38,14 +40,22 @@ class RedisStringParser: NSObject, RedisParserInterface {

assert(self.length == separatorRange.location, "length mismatch");

debugPrint("SOCKET: string \(line)")
if (isDebugLogEnabled) {
debugPrint("SOCKET: string \(line)")
}

results.append(line.substring(to: separatorRange.location));
}
}
}

class RedisGenericParser: NSObject, RedisParserInterface {
private var isDebugLogEnabled: Bool

init(isDebugLogEnabled: Bool) {
self.isDebugLogEnabled = isDebugLogEnabled
}

func parseLine(data: Data, parserStack: inout Array<RedisParserInterface>, results: inout Array<Any?>) {

guard let line: NSString = NSString(data: data as Data, encoding: String.Encoding.utf8.rawValue) else {
Expand All @@ -61,61 +71,68 @@ class RedisGenericParser: NSObject, RedisParserInterface {
let restOfLineRange = NSMakeRange(1, separatorRange.location - 1)
let restOfLine: String = line.substring(with: restOfLineRange)
let firstCharacter: Character = Character(UnicodeScalar(line.character(at: 0))!)

var debugPrefix: String? = nil

switch (firstCharacter) {
case "-".first!:
debugPrint("SOCKET: - -- \(restOfLine)");
debugPrefix = "-"

let error =
NSError(domain: RedisStringParserClassConstants.errorDomain,
code: -1,
userInfo: ["message": restOfLine]);
results.append(error)
case ":".first!:
debugPrint("SOCKET: + -- \(restOfLine)");
debugPrefix = "+"

if let restOfLineInt = Int(restOfLine) {
results.append(restOfLineInt)
}
case "+".first!:
debugPrint("SOCKET: + -- \(restOfLine)");
debugPrefix = "+"

results.append(restOfLine);
case "$".first!:
debugPrint("SOCKET: $ -- \(restOfLine)");
debugPrefix = "$"

if let length = Int(restOfLine) {
if (length < 0) {
results.append(nil);
} else {
let stringParser = RedisStringParser(length: length)
let stringParser = RedisStringParser(length: length, isDebugLogEnabled: isDebugLogEnabled)
parserStack.append(stringParser)
}
}
case "*".first!:
debugPrint("SOCKET: * -- \(restOfLine)");
debugPrefix = "*"

if let length = Int(restOfLine) {
for _ in 0..<length {
let genericParser = RedisGenericParser()
let genericParser = RedisGenericParser(isDebugLogEnabled: isDebugLogEnabled)
parserStack.append(genericParser);
}
}
break;
default:
break;
}

if let debugPrefix = debugPrefix, isDebugLogEnabled == true {
debugPrint("SOCKET: \(debugPrefix) -- \(restOfLine)")
}

}
}

class RedisResponseParser: NSObject {
weak var delegate: RedisMessageReceivedDelegate?
var parserStack: Array<RedisParserInterface>
var results: Array<Any?>
private var isDebugLogEnabled: Bool

init(delegate: RedisMessageReceivedDelegate?) {
init(delegate: RedisMessageReceivedDelegate?, isDebugLogEnabled: Bool = true) {
self.delegate = delegate

self.isDebugLogEnabled = isDebugLogEnabled
self.parserStack = Array<RedisParserInterface>()
self.results = Array<Any?>()
}
Expand All @@ -128,7 +145,7 @@ class RedisResponseParser: NSObject {
func parseLine(data: Data) {

if (self.parserStack.count == 0) {
self.parserStack.append(RedisGenericParser())
self.parserStack.append(RedisGenericParser(isDebugLogEnabled: isDebugLogEnabled))
}

let parserInterface: RedisParserInterface = self.parserStack.last!
Expand Down

0 comments on commit ce85c0f

Please sign in to comment.