Skip to content

Commit

Permalink
Add support for '(%queue)' log format macro to log current DispatchQu…
Browse files Browse the repository at this point in the history
…eue label
  • Loading branch information
mman committed Nov 16, 2022
1 parent fc2a715 commit 8f1e1ef
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 5 deletions.
8 changes: 6 additions & 2 deletions Sources/HeliumLogger/HeliumLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ public enum HeliumLoggerFormatValues: String {
case logType = "(%type)"
/// The time and date at which the message was logged.
case date = "(%date)"

/// The label of the dispatch queue where the message was logged.
case queue = "(%queue)"

static let all: [HeliumLoggerFormatValues] = [
.message, .function, .line, .file, .logType, .date
.message, .function, .line, .file, .logType, .date, .queue
]
}

Expand Down Expand Up @@ -303,6 +305,8 @@ extension HeliumLogger : Logger {
value = literal
case .token(let token):
switch token {
case .queue:
value = String(cString: __dispatch_queue_get_label(nil))
case .date:
value = formatDate()
case .logType:
Expand Down
69 changes: 66 additions & 3 deletions Tests/HeliumLoggerTests/TestLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ class TestLogger : XCTestCase {
("testUniqueLogFormatRawValues", testUniqueLogFormatRawValues),
("testGetFile", testGetFile),
("testFormatDates", testFormatDates),
("testFormatEntries", testFormatEntries)
("testFormatEntries", testFormatEntries),
("testFormatEntriesOnDifferentDispatchQueues", testFormatEntriesOnDifferentDispatchQueues),
]
}

Expand Down Expand Up @@ -300,8 +301,9 @@ class TestLogger : XCTestCase {
logger.details = true
let longFormat = logger.formatEntry(type: type, msg: msg, functionName: funct, lineNum: line, fileName: file)

let format = "!!!TEST!!! [(%file):(%line) (%msg) (%func)](%date)"
let expected = "!!!TEST!!! [\(file):\(line) \(msg) \(funct)]"
let currentQueueLabel = String(cString: __dispatch_queue_get_label(nil))
let format = "!!!TEST!!! [(%queue)][(%file):(%line) (%msg) (%func)](%date)"
let expected = "!!!TEST!!! [\(currentQueueLabel)][\(file):\(line) \(msg) \(funct)]"
logger.format = format

// test custom format with fullFilePath = false
Expand Down Expand Up @@ -339,4 +341,65 @@ class TestLogger : XCTestCase {
logger.details = true
XCTAssertEqual(longFormat, logger.formatEntry(type: type, msg: msg, functionName: funct, lineNum: line, fileName: file))
}

func testFormatEntriesOnDifferentDispatchQueues() {
let logger = HeliumLogger()
logger.dateFormat = "" // short circuit date so we can compare entries generated at different times

let type: LoggerMessageType = .error
let msg = "test message"
let file = #file
let line = #line
let funct = #function

logger.fullFilePath = false
logger.colored = false

logger.details = false
let shortFormat = logger.formatEntry(type: type, msg: msg, functionName: funct, lineNum: line, fileName: file)
logger.details = true
let longFormat = logger.formatEntry(type: type, msg: msg, functionName: funct, lineNum: line, fileName: file)

DispatchQueue.global().sync {
let currentQueueLabel = String(cString: __dispatch_queue_get_label(nil))
let format = "!!!TEST!!! [(%queue)][(%file):(%line) (%msg) (%func)](%date)"
let expected = "!!!TEST!!! [\(currentQueueLabel)][\(file):\(line) \(msg) \(funct)]"
logger.format = format

// test custom format with fullFilePath = false
XCTAssertNotEqual(expected, logger.formatEntry(type: type, msg: msg, functionName: funct, lineNum: line, fileName: file))

logger.fullFilePath = true
// test custom format with fullFilePath = true
XCTAssertEqual(expected, logger.formatEntry(type: type, msg: msg, functionName: funct, lineNum: line, fileName: file))

// test custom formats are the same when not colorized for different log types
XCTAssertEqual(expected, logger.formatEntry(type: .warning, msg: msg, functionName: funct, lineNum: line, fileName: file))
XCTAssertEqual(expected, logger.formatEntry(type: .info, msg: msg, functionName: funct, lineNum: line, fileName: file))
XCTAssertEqual(expected, logger.formatEntry(type: .error, msg: msg, functionName: funct, lineNum: line, fileName: file))

logger.format = format + "(%type)"
logger.colored = true
let colorizedExpected = "\(TerminalColor.red.rawValue)\(expected)\(type)\(TerminalColor.foreground.rawValue)"

// test custom formats are NOT the same when not colorized for different log types
XCTAssertNotEqual(colorizedExpected, logger.formatEntry(type: .warning, msg: msg, functionName: funct, lineNum: line, fileName: file))
XCTAssertNotEqual(colorizedExpected, logger.formatEntry(type: .info, msg: msg, functionName: funct, lineNum: line, fileName: file))
XCTAssertEqual(colorizedExpected, logger.formatEntry(type: type, msg: msg, functionName: funct, lineNum: line, fileName: file))

// test formatted entries are NOT the same as defaults when logger.format is set
XCTAssertNotEqual(shortFormat, logger.formatEntry(type: type, msg: msg, functionName: funct, lineNum: line, fileName: file))
XCTAssertNotEqual(longFormat, logger.formatEntry(type: type, msg: msg, functionName: funct, lineNum: line, fileName: file))

logger.format = nil
logger.fullFilePath = false
logger.colored = false

// test formatted entries are the same as defaults when logger.format is nil
logger.details = false
XCTAssertEqual(shortFormat, logger.formatEntry(type: type, msg: msg, functionName: funct, lineNum: line, fileName: file))
logger.details = true
XCTAssertEqual(longFormat, logger.formatEntry(type: type, msg: msg, functionName: funct, lineNum: line, fileName: file))
}
}
}

0 comments on commit 8f1e1ef

Please sign in to comment.