|
5 | 5 | import SwiftRs
|
6 | 6 | import Tauri
|
7 | 7 | import UIKit
|
| 8 | +import os.log |
8 | 9 |
|
9 | 10 | #if targetEnvironment(simulator)
|
10 | 11 | var logReady = false
|
11 | 12 | #else
|
12 | 13 | var logReady = true
|
13 | 14 | #endif
|
14 | 15 |
|
| 16 | +var pendingLogs: [(Int, NSString)] = [] |
| 17 | +var elapsedTime: TimeInterval = 0 |
| 18 | +var logFlushScheduled = false |
| 19 | + |
15 | 20 | @_cdecl("tauri_log")
|
16 | 21 | func log(level: Int, message: NSString) {
|
17 | 22 | if logReady {
|
18 | 23 | os_log(level, message)
|
19 | 24 | } else {
|
20 |
| - dispatch_log(level, message) |
| 25 | + pendingLogs.append((level, message)) |
| 26 | + scheduleLogFlush() |
21 | 27 | }
|
22 | 28 | }
|
23 | 29 |
|
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) |
| 30 | +// delay logging when the logger isn't immediately available |
| 31 | +// in some cases when using the simulator the app would hang when calling os_log too soon |
| 32 | +// better be safe here and wait a few seconds than actually freeze the app in dev mode |
| 33 | +// in production this isn't a problem |
| 34 | +func scheduleLogFlush() { |
| 35 | + guard !logFlushScheduled else { return } |
| 36 | + logFlushScheduled = true |
| 37 | + |
| 38 | + DispatchQueue.main.asyncAfter(deadline: .now() + 5) { |
31 | 39 | logReady = true
|
| 40 | + flushLogs() |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func flushLogs() { |
| 45 | + for (level, message) in pendingLogs { |
| 46 | + os_log(level, message) |
32 | 47 | }
|
| 48 | + pendingLogs.removeAll() |
33 | 49 | }
|
34 | 50 |
|
35 | 51 | func os_log(_ level: Int, _ message: NSString) {
|
|
0 commit comments