Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/fix-log-freeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"log": patch:bug
"log-js": patch:bug
---

Fixes iOS simulator still freezing sometimes due to early logging.
32 changes: 24 additions & 8 deletions plugins/log/ios/Sources/LogPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,47 @@
import SwiftRs
import Tauri
import UIKit
import os.log

#if targetEnvironment(simulator)
var logReady = false
#else
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) {
os_log(level, message)
// 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() + 5) {
logReady = true
flushLogs()
}
}

func flushLogs() {
for (level, message) in pendingLogs {
os_log(level, message)
}
pendingLogs.removeAll()
}

func os_log(_ level: Int, _ message: NSString) {
Expand Down