Log.swift (1981B)
1 // 2 // Log.swift 3 // damus 4 // 5 // Created by William Casarin on 2023-08-02. 6 // 7 8 import Foundation 9 import os.log 10 11 12 enum LogCategory: String { 13 case nav 14 case render 15 case storage 16 case networking 17 case push_notifications 18 case damus_purple 19 case image_uploading 20 } 21 22 /// Damus structured logger 23 class Log { 24 static private func logger(for logcat: LogCategory) -> OSLog { 25 return OSLog(subsystem: "com.jb55.damus", category: logcat.rawValue) 26 } 27 28 /// dumb workaround, swift can't forward C vararsg 29 static private func log(_ message: StaticString, for log: OSLog, type: OSLogType, _ args: [CVarArg]) { 30 switch args.count { 31 case 0: 32 os_log(message, log: log, type: type) 33 case 1: 34 os_log(message, log: log, type: type, args[0]) 35 case 2: 36 os_log(message, log: log, type: type, args[0], args[1]) 37 case 3: 38 os_log(message, log: log, type: type, args[0], args[1], args[2]) 39 case 4: 40 os_log(message, log: log, type: type, args[0], args[1], args[2], args[3]) 41 case 5: 42 os_log(message, log: log, type: type, args[0], args[1], args[2], args[3], args[4]) 43 default: 44 os_log("Too many variadic params were sent to the logger so we tossed them!", log: log, type: .error) 45 os_log(message, log: log, type: type) 46 } 47 } 48 49 static func info(_ msg: StaticString, for logcat: LogCategory, _ args: CVarArg...) { 50 Log.log(msg, for: logger(for: logcat), type: OSLogType.info, args) 51 } 52 53 static func debug(_ msg: StaticString, for logcat: LogCategory, _ args: CVarArg...) { 54 Log.log(msg, for: logger(for: logcat), type: OSLogType.debug, args) 55 } 56 57 static func error(_ msg: StaticString, for logcat: LogCategory, _ args: CVarArg...) { 58 Log.log(msg, for: logger(for: logcat), type: OSLogType.error, args) 59 } 60 }