// // Log.swift // Befund // // Created by Irakli Abetschkhrischwili on 30.04.22. // Copyright © 2022 MVZ Dr. Stein und Kollegen. All rights reserved. // import Foundation extension Core { public struct Log { /** * Log types */ public enum LogTypes: String { case CRITICAL case ERROR case INFO case LOG case DEBUG case WARNING } /** * Initializes the loger context */ public static func Initialize() { Core.Database.Logs.CreateDBIfNotExists() } /** * Logs critical error with namespace and method of occurrent */ public static func Critical(err: Error, namespace: String, method: String) { let message = "(" + namespace + "->" + method + "): " + err.localizedDescription Write(message: message, type: .CRITICAL) } /** * Logs critical message with namespace and method of occurrent */ public static func Critical(msg: String, namespace: String, method: String) { let message = "(" + namespace + "->" + method + "): " + msg Write(message: message, type: .CRITICAL) } /** * Logs error with namespace and method of occurrent */ public static func Error(err: Error, namespace: String, method: String) { let message = "(" + namespace + "->" + method + "): " + err.localizedDescription Write(message: message, type: .ERROR) } /** * Logs error message with namespace and method of occurrent */ public static func Error(msg: String, namespace: String, method: String) { let message = "(" + namespace + "->" + method + "): " + msg Write(message: message, type: .ERROR) } /** * Logs info message with namespace and method of occurrent */ public static func Info(msg: String, namespace: String, method: String) { let message = "(" + namespace + "->" + method + "): " + msg Write(message: message, type: .INFO) } /** * Logs debug message with namespace and method of occurrent */ public static func Debug(msg: String, namespace: String, method: String) { let message = "(" + namespace + "->" + method + "): " + msg Write(message: message, type: .DEBUG) } /** * Logs warnings message with namespace and method of occurrent */ public static func Warning(msg: String, namespace: String, method: String) { let message = "(" + namespace + "->" + method + "): " + msg Write(message: message, type: .WARNING) } /** * Writes the log by type in the database */ public static func Write(message: String, type: LogTypes) { let now = Date() let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" let msg = dateFormatter.string(from: now) + " [" + type.rawValue + "]:" + message + "\r\n"; if(type == LogTypes.DEBUG) { print(msg) } var dbLog = Core.Database.Log() dbLog.guid = UUID().uuidString dbLog.message = msg dbLog.type = type.rawValue Core.Database.Logs.Create(log: dbLog) } } }