Log.swift (2027B)
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 timeline 18 case push_notifications 19 case damus_purple 20 case image_uploading 21 case video_coordination 22 } 23 24 /// Damus structured logger 25 class Log { 26 static private func logger(for logcat: LogCategory) -> OSLog { 27 return OSLog(subsystem: "com.jb55.damus", category: logcat.rawValue) 28 } 29 30 /// dumb workaround, swift can't forward C vararsg 31 static private func log(_ message: StaticString, for log: OSLog, type: OSLogType, _ args: [CVarArg]) { 32 switch args.count { 33 case 0: 34 os_log(message, log: log, type: type) 35 case 1: 36 os_log(message, log: log, type: type, args[0]) 37 case 2: 38 os_log(message, log: log, type: type, args[0], args[1]) 39 case 3: 40 os_log(message, log: log, type: type, args[0], args[1], args[2]) 41 case 4: 42 os_log(message, log: log, type: type, args[0], args[1], args[2], args[3]) 43 case 5: 44 os_log(message, log: log, type: type, args[0], args[1], args[2], args[3], args[4]) 45 default: 46 os_log("Too many variadic params were sent to the logger so we tossed them!", log: log, type: .error) 47 os_log(message, log: log, type: type) 48 } 49 } 50 51 static func info(_ msg: StaticString, for logcat: LogCategory, _ args: CVarArg...) { 52 Log.log(msg, for: logger(for: logcat), type: OSLogType.info, args) 53 } 54 55 static func debug(_ msg: StaticString, for logcat: LogCategory, _ args: CVarArg...) { 56 Log.log(msg, for: logger(for: logcat), type: OSLogType.debug, args) 57 } 58 59 static func error(_ msg: StaticString, for logcat: LogCategory, _ args: CVarArg...) { 60 Log.log(msg, for: logger(for: logcat), type: OSLogType.error, args) 61 } 62 }