From ea7fd016ca70dba46500cc9a67131113dcd7f8fc Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Wed, 15 Feb 2023 13:00:18 -0800 Subject: [PATCH] feat(ios): add Logger class (#6284) --- .../mobile/ios-api/Sources/Tauri/Logger.swift | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 core/tauri/mobile/ios-api/Sources/Tauri/Logger.swift diff --git a/core/tauri/mobile/ios-api/Sources/Tauri/Logger.swift b/core/tauri/mobile/ios-api/Sources/Tauri/Logger.swift new file mode 100644 index 000000000..9c950a026 --- /dev/null +++ b/core/tauri/mobile/ios-api/Sources/Tauri/Logger.swift @@ -0,0 +1,46 @@ +import os.log +import UIKit + +/// Wrapper class for os_log function +public class Logger { + private static var _enabled = false + public static var enabled: Bool { + get { + #if DEBUG + return true + #else + return _enabled + #endif + } + set { + Logger._enabled = newValue + } + } + + static func log(_ items: Any..., category: String, type: OSLogType) { + if Logger.enabled { + var message = "" + let last = items.count - 1 + for (index, item) in items.enumerated() { + message += "\(item)" + if index != last { + message += " " + } + } + let log = OSLog(subsystem: Bundle.main.bundleIdentifier ?? "-", category: category) + os_log("%{public}@", log: log, type: type, String(message.prefix(4068))) + } + } + + public static func debug(_ items: Any..., category: String = "app") { + Logger.log(items, category: category, type: OSLogType.debug) + } + + public static func info(_ items: Any..., category: String = "app") { + Logger.log(items, category: category, type: OSLogType.info) + } + + public static func error(_ items: Any..., category: String = "app") { + Logger.log(items, category: category, type: OSLogType.error) + } +}