From 58e0b1c83f7c19650b610050ebb729dca4925b15 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Tue, 24 Jun 2025 12:05:12 -0300 Subject: [PATCH 1/3] fix(log): iOS simulator freezing due to early logging follow-up for #2626 --- .changes/fix-log-freeze.md | 6 ++++ plugins/log/ios/Sources/LogPlugin.swift | 43 ++++++++++++++++++------- 2 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 .changes/fix-log-freeze.md diff --git a/.changes/fix-log-freeze.md b/.changes/fix-log-freeze.md new file mode 100644 index 0000000000..dee03ad359 --- /dev/null +++ b/.changes/fix-log-freeze.md @@ -0,0 +1,6 @@ +--- +"log": patch:bug +"log-js": patch:bug +--- + +Fixes iOS simulator still freezing sometimes due to early logging. diff --git a/plugins/log/ios/Sources/LogPlugin.swift b/plugins/log/ios/Sources/LogPlugin.swift index e21b4bab9c..d2491afb00 100644 --- a/plugins/log/ios/Sources/LogPlugin.swift +++ b/plugins/log/ios/Sources/LogPlugin.swift @@ -5,6 +5,7 @@ import SwiftRs import Tauri import UIKit +import os.log #if targetEnvironment(simulator) var logReady = false @@ -12,31 +13,49 @@ import UIKit var logReady = true #endif +var pendingLogs: [(Int, NSString)] = [] +var elapsedTime: TimeInterval = 0 +var logFlushScheduled = false + @_cdecl("tauri_log") func log(level: Int, message: NSString) { if logReady { os_log(level, message) } else { - dispatch_log(level, message) + pendingLogs.append((level, message)) + scheduleLogFlush() } } -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) { +// 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 +func scheduleLogFlush() { + guard !logFlushScheduled else { return } + logFlushScheduled = true + + DispatchQueue.main.asyncAfter(deadline: .now() + 10) { + flushLogs() + } +} + +func flushLogs() { + for (level, message) in pendingLogs { os_log(level, message) - logReady = true } + pendingLogs.removeAll() } func os_log(_ level: Int, _ message: NSString) { + os_log("%{public}@", log: OSLog.default, type: osLogType(from: level), message) +} + +func osLogType(from level: Int) -> OSLogType { 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 + case 1: return .debug + case 2: return .info + case 3: return .error + default: return .default } } From 34d2fa001639e2f8ff4557e2646194ce85877aee Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Tue, 24 Jun 2025 14:30:26 -0300 Subject: [PATCH 2/3] use logger --- plugins/log/ios/Sources/LogPlugin.swift | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/plugins/log/ios/Sources/LogPlugin.swift b/plugins/log/ios/Sources/LogPlugin.swift index d2491afb00..1784e044bb 100644 --- a/plugins/log/ios/Sources/LogPlugin.swift +++ b/plugins/log/ios/Sources/LogPlugin.swift @@ -48,14 +48,10 @@ func flushLogs() { } func os_log(_ level: Int, _ message: NSString) { - os_log("%{public}@", log: OSLog.default, type: osLogType(from: level), message) -} - -func osLogType(from level: Int) -> OSLogType { switch level { - case 1: return .debug - case 2: return .info - case 3: return .error - default: return .default + case 1: Logger.debug(message as String) + case 2: Logger.info(message as String) + case 3: Logger.error(message as String) + default: break } } From 620c961bad46f2e3bd1a536016deea1bd4802188 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Tue, 24 Jun 2025 14:35:34 -0300 Subject: [PATCH 3/3] set logReady --- plugins/log/ios/Sources/LogPlugin.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/log/ios/Sources/LogPlugin.swift b/plugins/log/ios/Sources/LogPlugin.swift index 1784e044bb..9903920f25 100644 --- a/plugins/log/ios/Sources/LogPlugin.swift +++ b/plugins/log/ios/Sources/LogPlugin.swift @@ -35,7 +35,8 @@ func scheduleLogFlush() { guard !logFlushScheduled else { return } logFlushScheduled = true - DispatchQueue.main.asyncAfter(deadline: .now() + 10) { + DispatchQueue.main.asyncAfter(deadline: .now() + 5) { + logReady = true flushLogs() } }