7
7
8
8
import Foundation
9
9
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
+
10
30
/// A handler to collect Log messages locally, and allow for searching and filtering.
11
31
open class LocalLogHandler {
12
32
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 {
15
35
/// Category of the Log where this entry was originated from
16
36
var category : String
17
37
@@ -26,7 +46,7 @@ open class LocalLogHandler {
26
46
}
27
47
28
48
/// Maximum number of Log entries that may be collected.
29
- var size : UInt = 1000
49
+ public var size : UInt = 1000
30
50
31
51
private var entries : [ LogEntry ] = [ ]
32
52
@@ -39,8 +59,13 @@ open class LocalLogHandler {
39
59
// MARK: Public Methods
40
60
41
61
/// 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)
44
69
while entries. count > size {
45
70
entries. remove ( at: 0 )
46
71
}
0 commit comments