From 686a839c96fae1b0334f2df9dc76ca5cdbe00dbe Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Sat, 12 Apr 2025 12:50:18 -0300 Subject: [PATCH] fix(log): iOS simulator freezes when calling os_log too early (#2626) let's delay logs on simulator to prevent deadlocks - looks like the logging system isn't available when the Rust process starts, and it's freezing the app (simulator only) closes https://github.com/tauri-apps/tauri/issues/12172 --- .changes/fix-ios-log-simulator.md | 6 ++++ plugins/log/ios/Sources/LogPlugin.swift | 41 ++++++++++++++++++++----- 2 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 .changes/fix-ios-log-simulator.md diff --git a/.changes/fix-ios-log-simulator.md b/.changes/fix-ios-log-simulator.md new file mode 100644 index 00000000..1eda1d4c --- /dev/null +++ b/.changes/fix-ios-log-simulator.md @@ -0,0 +1,6 @@ +--- +"log": patch +"log-js": patch +--- + +Fix iOS app stuck when using the iOS Simulator and the log plugin due to a deadlock when calling os_log too early. diff --git a/plugins/log/ios/Sources/LogPlugin.swift b/plugins/log/ios/Sources/LogPlugin.swift index cefdc858..e21b4bab 100644 --- a/plugins/log/ios/Sources/LogPlugin.swift +++ b/plugins/log/ios/Sources/LogPlugin.swift @@ -2,16 +2,41 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: MIT -import UIKit -import Tauri import SwiftRs +import Tauri +import UIKit + +#if targetEnvironment(simulator) + var logReady = false +#else + var logReady = true +#endif @_cdecl("tauri_log") func log(level: Int, message: NSString) { - switch level { - case 1: Logger.debug(message as String) - case 2: Logger.info(message as String) - case 3: Logger.error(message as String) - default: break - } + if logReady { + os_log(level, message) + } else { + dispatch_log(level, message) + } +} + +func dispatch_log(_ level: Int, _ message: NSString) { + // delay logging when the logger isn't immediately available + // in some cases when using the simulator the app would hang when calling os_log too soon + // better be safe here and wait a few seconds than actually freeze the app in dev mode + // in production this isn't a problem + DispatchQueue.main.asyncAfter(deadline: .now() + 2) { + os_log(level, message) + logReady = true + } +} + +func os_log(_ level: Int, _ message: NSString) { + switch level { + case 1: Logger.debug(message as String) + case 2: Logger.info(message as String) + case 3: Logger.error(message as String) + default: break + } }