Skip to content

Commit 686a839

Browse files
authored
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 tauri-apps/tauri#12172
1 parent db7baff commit 686a839

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

.changes/fix-ios-log-simulator.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"log": patch
3+
"log-js": patch
4+
---
5+
6+
Fix iOS app stuck when using the iOS Simulator and the log plugin due to a deadlock when calling os_log too early.

plugins/log/ios/Sources/LogPlugin.swift

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,41 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
import UIKit
6-
import Tauri
75
import SwiftRs
6+
import Tauri
7+
import UIKit
8+
9+
#if targetEnvironment(simulator)
10+
var logReady = false
11+
#else
12+
var logReady = true
13+
#endif
814

915
@_cdecl("tauri_log")
1016
func log(level: Int, message: NSString) {
11-
switch level {
12-
case 1: Logger.debug(message as String)
13-
case 2: Logger.info(message as String)
14-
case 3: Logger.error(message as String)
15-
default: break
16-
}
17+
if logReady {
18+
os_log(level, message)
19+
} else {
20+
dispatch_log(level, message)
21+
}
22+
}
23+
24+
func dispatch_log(_ level: Int, _ message: NSString) {
25+
// delay logging when the logger isn't immediately available
26+
// in some cases when using the simulator the app would hang when calling os_log too soon
27+
// better be safe here and wait a few seconds than actually freeze the app in dev mode
28+
// in production this isn't a problem
29+
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
30+
os_log(level, message)
31+
logReady = true
32+
}
33+
}
34+
35+
func os_log(_ level: Int, _ message: NSString) {
36+
switch level {
37+
case 1: Logger.debug(message as String)
38+
case 2: Logger.info(message as String)
39+
case 3: Logger.error(message as String)
40+
default: break
41+
}
1742
}

0 commit comments

Comments
 (0)