Skip to content

Commit b7370e7

Browse files
author
Justin Magnini
committed
add LogEntry protocol, to allow for storing of custom data in the LocalLogHandler if desired.
1 parent 832fd88 commit b7370e7

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

Example/Tests/LogTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class LogTests: XCTestCase {
3030

3131
var loggedStrings: [(level: Log.Level, string: String)] = []
3232
Log.globalHandler = { (log, level, string) in
33-
handler.addEntry(log: log, level: level, entry: string)
33+
handler.addEntry(log: log, level: level, string: string)
3434
loggedStrings.append((level: level, string: string))
3535
}
3636

Pod/Classes/Logging/LocalLogHandler.swift

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,31 @@
77

88
import Foundation
99

10+
public protocol LogEntry {
11+
/// Category of the Log where this entry was originated from
12+
var category: String {get}
13+
14+
/// Log level of entry.
15+
var level: Log.Level {get}
16+
17+
/// Message sent by Log.
18+
var message: String {get}
19+
20+
/// When Log entry was received
21+
var timestamp: Date {get}
22+
}
23+
24+
extension LogEntry {
25+
public func matches(query: String) -> Bool {
26+
(message.range(of: query, options: .regularExpression)?.isEmpty ?? true) == false
27+
}
28+
}
29+
1030
/// A handler to collect Log messages locally, and allow for searching and filtering.
1131
open class LocalLogHandler {
1232

13-
/// Represents a single entry collected from a Log instance
14-
public struct LogEntry {
33+
/// Basic implementation of the LogEntry protocol
34+
struct Entry: LogEntry {
1535
/// Category of the Log where this entry was originated from
1636
var category: String
1737

@@ -26,7 +46,7 @@ open class LocalLogHandler {
2646
}
2747

2848
/// Maximum number of Log entries that may be collected.
29-
var size: UInt = 1000
49+
public var size: UInt = 1000
3050

3151
private var entries: [LogEntry] = []
3252

@@ -39,8 +59,13 @@ open class LocalLogHandler {
3959
// MARK: Public Methods
4060

4161
/// Intended to be called from the Log.globalHandler to record Log messages.
42-
public func addEntry(log: Log, level: Log.Level, entry: String) {
43-
entries.append(.init(category: log.name, level: level, message: entry, timestamp: Date()))
62+
public func addEntry(log: Log, level: Log.Level, string: String) {
63+
addEntry(Entry(category: log.name, level: level, message: string, timestamp: Date()))
64+
}
65+
66+
/// Intended to be called from the Log.globalHandler to record Log messages.
67+
public func addEntry(_ entry: LogEntry) {
68+
entries.append(entry)
4469
while entries.count > size {
4570
entries.remove(at: 0)
4671
}

Pod/Classes/Logging/Log.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ open class Log {
138138
osLog(logMessage, objectString: objectString, level: level)
139139
}
140140
else {
141-
let nameString = name.count > 0 ? "[\(name)]]" : ""
141+
let nameString = name.count > 0 ? "[\(name)]" : ""
142142
print(nameString + logString + "\n")
143143
}
144144
self.handler?(level, logString)

0 commit comments

Comments
 (0)