Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add LogEntry protocol, to allow for storing of custom data in the LocalLogHandler if desired.
  • Loading branch information
Justin Magnini committed May 27, 2021
1 parent 832fd88 commit b7370e7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Example/Tests/LogTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class LogTests: XCTestCase {

var loggedStrings: [(level: Log.Level, string: String)] = []
Log.globalHandler = { (log, level, string) in
handler.addEntry(log: log, level: level, entry: string)
handler.addEntry(log: log, level: level, string: string)
loggedStrings.append((level: level, string: string))
}

Expand Down
35 changes: 30 additions & 5 deletions Pod/Classes/Logging/LocalLogHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,31 @@

import Foundation

public protocol LogEntry {
/// Category of the Log where this entry was originated from
var category: String {get}

/// Log level of entry.
var level: Log.Level {get}

/// Message sent by Log.
var message: String {get}

/// When Log entry was received
var timestamp: Date {get}
}

extension LogEntry {
public func matches(query: String) -> Bool {
(message.range(of: query, options: .regularExpression)?.isEmpty ?? true) == false
}
}

/// A handler to collect Log messages locally, and allow for searching and filtering.
open class LocalLogHandler {

/// Represents a single entry collected from a Log instance
public struct LogEntry {
/// Basic implementation of the LogEntry protocol
struct Entry: LogEntry {
/// Category of the Log where this entry was originated from
var category: String

Expand All @@ -26,7 +46,7 @@ open class LocalLogHandler {
}

/// Maximum number of Log entries that may be collected.
var size: UInt = 1000
public var size: UInt = 1000

private var entries: [LogEntry] = []

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

/// Intended to be called from the Log.globalHandler to record Log messages.
public func addEntry(log: Log, level: Log.Level, entry: String) {
entries.append(.init(category: log.name, level: level, message: entry, timestamp: Date()))
public func addEntry(log: Log, level: Log.Level, string: String) {
addEntry(Entry(category: log.name, level: level, message: string, timestamp: Date()))
}

/// Intended to be called from the Log.globalHandler to record Log messages.
public func addEntry(_ entry: LogEntry) {
entries.append(entry)
while entries.count > size {
entries.remove(at: 0)
}
Expand Down
2 changes: 1 addition & 1 deletion Pod/Classes/Logging/Log.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ open class Log {
osLog(logMessage, objectString: objectString, level: level)
}
else {
let nameString = name.count > 0 ? "[\(name)]]" : ""
let nameString = name.count > 0 ? "[\(name)]" : ""
print(nameString + logString + "\n")
}
self.handler?(level, logString)
Expand Down

0 comments on commit b7370e7

Please sign in to comment.