damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

Log.swift (1961B)


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