USB-Meter / USB Meter / AppDelegate.swift
Newer Older
149 lines | 4.762kb
Bogdan Timofte authored 2 weeks ago
1
//
2
//  AppDelegate.swift
3
//  USB Meter
4
//
5
//  Created by Bogdan Timofte on 01/03/2020.
6
//  Copyright © 2020 Bogdan Timofte. All rights reserved.
7
//
8

            
9
import UIKit
10

            
11
//let btSerial = BluetoothSerial(delegate: BSD())
12
let appData = AppData()
13
enum Constants {
14
    static let chartUnderscan: CGFloat = 0.5
15
    static let chartOverscan: CGFloat = 1 - chartUnderscan
16
}
17
// MARK: Clock
18

            
19
// MARK: Debug
20
public func track(_ message: String = "", file: String = #file, function: String = #function, line: Int = #line ) {
Bogdan Timofte authored 4 days ago
21
    guard shouldEmitTrackMessage(message, file: file, function: function) else {
22
        return
23
    }
Bogdan Timofte authored 2 weeks ago
24
    let date = Date()
25
    let calendar = Calendar.current
26
    let hour = calendar.component(.hour, from: date)
27
    let minutes = calendar.component(.minute, from: date)
28
    let seconds = calendar.component(.second, from: date)
29
    print("\(hour):\(minutes):\(seconds) - \(file):\(line) - \(function) \(message)")
30
}
31

            
Bogdan Timofte authored 4 days ago
32
private func shouldEmitTrackMessage(_ message: String, file: String, function: String) -> Bool {
33
    #if DEBUG
34
    if ProcessInfo.processInfo.environment["USB_METER_VERBOSE_LOGS"] == "1" {
35
        return true
36
    }
37

            
38
    #if targetEnvironment(macCatalyst)
39
    let importantMarkers = [
40
        "Error",
41
        "error",
42
        "Failed",
43
        "failed",
44
        "timeout",
45
        "Timeout",
46
        "Missing",
47
        "missing",
48
        "overflow",
49
        "Disconnect",
50
        "disconnect",
51
        "Disconnected",
52
        "unauthorized",
53
        "not authorized",
54
        "not supported",
55
        "Unexpected",
56
        "Invalid Context",
57
        "ignored",
58
        "Guard:",
59
        "Skip data request",
60
        "Dropping unsolicited data",
61
        "This is not possible!",
62
        "Inferred",
63
        "Clearing",
64
        "Reconnecting"
65
    ]
66

            
67
    if importantMarkers.contains(where: { message.contains($0) }) {
68
        return true
69
    }
70

            
71
    let noisyFunctions: Set<String> = [
72
        "logRuntimeICloudDiagnostics()",
73
        "refreshCloudAvailability(reason:)",
74
        "start()",
75
        "centralManagerDidUpdateState(_:)",
76
        "discoveredMeter(peripheral:advertising:rssi:)",
77
        "connect()",
78
        "connectionEstablished()",
79
        "peripheral(_:didDiscoverServices:)",
80
        "peripheral(_:didDiscoverCharacteristicsFor:error:)",
81
        "refreshOperationalStateIfReady()",
82
        "peripheral(_:didUpdateNotificationStateFor:error:)",
83
        "scheduleDataDumpRequest(after:reason:)"
84
    ]
85

            
86
    if noisyFunctions.contains(function) {
87
        return false
88
    }
89

            
90
    let noisyMarkers = [
91
        "Runtime iCloud diagnostics",
92
        "iCloud availability",
93
        "Starting Bluetooth manager",
94
        "Bluetooth is On... Start scanning...",
95
        "adding new USB Meter",
96
        "Connect called for",
97
        "Connection established for",
98
        "Optional([<CBService:",
99
        "Optional([<CBCharacteristic:",
100
        "Waiting for notifications on",
101
        "Notification state updated for",
102
        "Peripheral ready with notify",
103
        "Schedule data request in",
104
        "Operational state changed"
105
    ]
106

            
107
    if noisyMarkers.contains(where: { message.contains($0) }) {
108
        return false
109
    }
110
    #endif
111

            
112
    return true
113
    #else
114
    _ = file
115
    _ = function
116
    return false
117
    #endif
118
}
119

            
Bogdan Timofte authored 2 weeks ago
120
@UIApplicationMain
121
class AppDelegate: UIResponder, UIApplicationDelegate {
122

            
123

            
124
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Bogdan Timofte authored a week ago
125
        logRuntimeICloudDiagnostics()
Bogdan Timofte authored 2 weeks ago
126
        return true
127
    }
128

            
Bogdan Timofte authored a week ago
129
    private func logRuntimeICloudDiagnostics() {
130
        #if DEBUG
131
        let hasUbiquityIdentityToken = FileManager.default.ubiquityIdentityToken != nil
132
        track("Runtime iCloud diagnostics: ubiquityIdentityTokenAvailable=\(hasUbiquityIdentityToken)")
133
        #endif
134
    }
135

            
Bogdan Timofte authored 2 weeks ago
136
    // MARK: UISceneSession Lifecycle
137

            
138
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
139
        // Called when a new scene session is being created.
140
        // Use this method to select a configuration to create the new scene with.
141
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
142
    }
143

            
144
    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
145
        // Called when the user discards a scene session.
146
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
147
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
148
    }
149
}