another.im-ios/ConversationsClassic/AppCore/Middlewares/LoggerMiddleware.swift

58 lines
1.5 KiB
Swift
Raw Normal View History

2024-06-19 15:15:27 +00:00
import Combine
import Foundation
2024-06-26 09:29:30 +00:00
import SwiftUI
2024-06-19 15:15:27 +00:00
let isConsoleLoggingEnabled = false
#if DEBUG
2024-07-23 16:05:19 +00:00
let prefixLength = 400
2024-06-19 15:15:27 +00:00
func loggerMiddleware() -> Middleware<AppState, AppAction> {
{ state, action in
let timeStr = dateFormatter.string(from: Date())
var actionStr = "\(action)"
actionStr = String(actionStr.prefix(prefixLength)) + " ..."
var stateStr = "\(state)"
stateStr = String(stateStr.prefix(prefixLength)) + " ..."
2024-06-26 09:29:30 +00:00
let str = "\(timeStr) \u{EA86} \(actionStr)\n\(timeStr) \u{F129} \(stateStr)\n"
2024-06-19 15:15:27 +00:00
print(str)
if isConsoleLoggingEnabled {
NSLog(str)
}
return Empty().eraseToAnyPublisher()
}
}
#else
func loggerMiddleware() -> Middleware<AppState, AppAction> {
{ _, _ in
Empty().eraseToAnyPublisher()
}
}
#endif
enum LogLevels: String {
2024-06-26 09:29:30 +00:00
case info = "\u{F449}"
case warning = "\u{F071}"
case error = "\u{EA76}"
2024-06-19 15:15:27 +00:00
}
// For database errors logging
func logIt(_ level: LogLevels, _ message: String) {
#if DEBUG
let timeStr = dateFormatter.string(from: Date())
let str = "\(timeStr) \(level.rawValue) \(message)"
print(str)
if isConsoleLoggingEnabled {
NSLog(str)
}
#endif
}
private var dateFormatter: DateFormatter {
let formatter = DateFormatter()
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") as Locale
formatter.dateFormat = "MM-dd HH:mm:ss.SSS"
return formatter
}