|
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 | +// 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() + 10) { |
| 39 | + flushLogs() |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func flushLogs() { |
| 44 | + for (level, message) in pendingLogs { |
30 | 45 | os_log(level, message)
|
31 |
| - logReady = true |
32 | 46 | }
|
| 47 | + pendingLogs.removeAll() |
33 | 48 | }
|
34 | 49 |
|
35 | 50 | func os_log(_ level: Int, _ message: NSString) {
|
| 51 | + os_log("%{public}@", log: OSLog.default, type: osLogType(from: level), message) |
| 52 | +} |
| 53 | + |
| 54 | +func osLogType(from level: Int) -> OSLogType { |
36 | 55 | 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 |
| 56 | + case 1: return .debug |
| 57 | + case 2: return .info |
| 58 | + case 3: return .error |
| 59 | + default: return .default |
41 | 60 | }
|
42 | 61 | }
|
0 commit comments