Logger.swift 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2019-2024 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. import os.log
  5. import UIKit
  6. /// Wrapper class for os_log function
  7. public class Logger {
  8. private static var _enabled = false
  9. public static var enabled: Bool {
  10. get {
  11. #if DEBUG
  12. return true
  13. #else
  14. return _enabled
  15. #endif
  16. }
  17. set {
  18. Logger._enabled = newValue
  19. }
  20. }
  21. static func log(_ items: Any..., category: String, type: OSLogType) {
  22. if Logger.enabled {
  23. var message = ""
  24. let last = items.count - 1
  25. for (index, item) in items.enumerated() {
  26. message += "\(item)"
  27. if index != last {
  28. message += " "
  29. }
  30. }
  31. let log = OSLog(subsystem: Bundle.main.bundleIdentifier ?? "-", category: category)
  32. os_log("%{public}@", log: log, type: type, String(message.prefix(4068)))
  33. }
  34. }
  35. public static func debug(_ items: Any..., category: String = "app") {
  36. #if DEBUG
  37. Logger.log(items, category: category, type: OSLogType.default)
  38. #else
  39. Logger.log(items, category: category, type: OSLogType.debug)
  40. #endif
  41. }
  42. public static func info(_ items: Any..., category: String = "app") {
  43. #if DEBUG
  44. Logger.log(items, category: category, type: OSLogType.default)
  45. #else
  46. Logger.log(items, category: category, type: OSLogType.info)
  47. #endif
  48. }
  49. public static func error(_ items: Any..., category: String = "app") {
  50. Logger.log(items, category: category, type: OSLogType.error)
  51. }
  52. }