another.im-ios/ConversationsClassic/AppCore/Middlewares/LoggerMiddleware.swift
2024-06-19 17:15:27 +02:00

56 lines
1.5 KiB
Swift
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Combine
import Foundation
let isConsoleLoggingEnabled = false
#if DEBUG
let prefixLength = 2000
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)) + " ..."
let str = "\(timeStr) ➡️ \(actionStr)\n\(timeStr)\(stateStr)\n"
print(str)
if isConsoleLoggingEnabled {
NSLog(str)
}
return Empty().eraseToAnyPublisher()
}
}
#else
func loggerMiddleware() -> Middleware<AppState, AppAction> {
{ _, _ in
Empty().eraseToAnyPublisher()
}
}
#endif
enum LogLevels: String {
case info = ""
case warning = "⚠️"
case error = ""
}
// 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
}