Skip to content

Commit 2323f90

Browse files
committed
Custom logging targets implemented
Register your own logging function. Useful to e.g. redirect the logging to a gui.
1 parent b944ad0 commit 2323f90

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

Common/Tracery.Logging.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,21 @@ extension Tracery {
2020
}
2121

2222
public static var logLevel = LoggingLevel.errors
23-
23+
public static var logTarget : (String) -> Void = stdLog
24+
25+
fileprivate static func stdLog(_ message: String) {
26+
// standard logger to standard output
27+
print(message)
28+
}
29+
30+
fileprivate static func notificationLog(_ message: String) {
31+
// install via setting logTarget to this function instead of the above
32+
NotificationCenter.default.post(name: Notification.Name("Tracery.log"), object: nil, userInfo: ["message":message])
33+
}
34+
2435
static func log(level: LoggingLevel, message: @autoclosure () -> String) {
2536
guard logLevel.rawValue >= level.rawValue else { return }
26-
print(message())
27-
// NotificationCenter.default.post(name: Notification.Name("Tracery.log"), object: nil, userInfo: ["message":message()])
37+
stdLog(message())
2838
}
2939

3040
func trace(_ message: @autoclosure () -> String) {

0 commit comments

Comments
 (0)