DiagnosticLog.swift 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // DiagnosticLog.swift
  3. // LoopKit
  4. //
  5. // Created by Darin Krauss on 6/12/19.
  6. // Copyright © 2019 LoopKit Authors. All rights reserved.
  7. //
  8. import os.log
  9. public class DiagnosticLog {
  10. private let subsystem: String
  11. private let category: String
  12. private let log: OSLog
  13. public init(subsystem: String, category: String) {
  14. self.subsystem = subsystem
  15. self.category = category
  16. self.log = OSLog(subsystem: subsystem, category: category)
  17. }
  18. public func debug(_ message: StaticString, _ args: CVarArg...) {
  19. log(message, type: .debug, args)
  20. }
  21. public func info(_ message: StaticString, _ args: CVarArg...) {
  22. log(message, type: .info, args)
  23. }
  24. public func `default`(_ message: StaticString, _ args: CVarArg...) {
  25. log(message, type: .default, args)
  26. }
  27. public func error(_ message: StaticString, _ args: CVarArg...) {
  28. log(message, type: .error, args)
  29. }
  30. private func log(_ message: StaticString, type: OSLogType, _ args: [CVarArg]) {
  31. switch args.count {
  32. case 0:
  33. os_log(message, log: log, type: type)
  34. case 1:
  35. os_log(message, log: log, type: type, args[0])
  36. case 2:
  37. os_log(message, log: log, type: type, args[0], args[1])
  38. case 3:
  39. os_log(message, log: log, type: type, args[0], args[1], args[2])
  40. case 4:
  41. os_log(message, log: log, type: type, args[0], args[1], args[2], args[3])
  42. case 5:
  43. os_log(message, log: log, type: type, args[0], args[1], args[2], args[3], args[4])
  44. default:
  45. os_log(message, log: log, type: type, args)
  46. }
  47. guard let sharedLogging = SharedLogging.instance else {
  48. return
  49. }
  50. sharedLogging.log(message, subsystem: subsystem, category: category, type: type, args)
  51. }
  52. }