49 lines
882 B
Swift
49 lines
882 B
Swift
|
import Foundation
|
||
|
|
||
|
let log = Log()
|
||
|
|
||
|
final class Log: ObservableObject {
|
||
|
|
||
|
private let df: DateFormatter
|
||
|
|
||
|
init() {
|
||
|
df = .init()
|
||
|
df.dateStyle = .short
|
||
|
df.timeStyle = .medium
|
||
|
}
|
||
|
|
||
|
enum Level: String {
|
||
|
case info = "INFO"
|
||
|
case warning = "WARN"
|
||
|
case error = "ERROR"
|
||
|
}
|
||
|
|
||
|
@Published
|
||
|
var logEntries: [LogEntry] = []
|
||
|
|
||
|
func info(_ message: String) {
|
||
|
log(.info, message)
|
||
|
}
|
||
|
|
||
|
func warning(_ message: String) {
|
||
|
log(.warning, message)
|
||
|
}
|
||
|
|
||
|
func error(_ message: String) {
|
||
|
log(.error, message)
|
||
|
}
|
||
|
|
||
|
func log(_ level: Level, _ message: String) {
|
||
|
let entry = LogEntry(level: level, message: message)
|
||
|
logEntries.insert(entry, at: 0)
|
||
|
print(entry)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
extension Log.Level: CustomStringConvertible {
|
||
|
|
||
|
var description: String {
|
||
|
rawValue
|
||
|
}
|
||
|
}
|