Skip to content

Commit ad6080f

Browse files
authored
Replaced debug print statements with proper loggers (#16)
1 parent 9ab33e2 commit ad6080f

12 files changed

+77
-41
lines changed

ORLib/ConfigManager.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919

2020
import Foundation
21+
import os
2122

2223
public enum ConfigManagerState: Equatable {
2324
case selectDomain
@@ -126,7 +127,7 @@ public class ConfigManager {
126127
}
127128
return state
128129
} catch {
129-
print("SetDomain -> error: \(error)")
130+
ORLogger.config.error("SetDomain -> error: \(error)")
130131
throw ConfigManagerError.couldNotLoadAppConfig
131132
}
132133
case .selectApp,

ORLib/ConsoleProviders/ESPProvision/BatteryProvisionAPI.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ import Foundation
2121
import os
2222

2323
struct BatteryProvisionAPIREST: BatteryProvisionAPI {
24-
private static let logger = Logger(
25-
subsystem: Bundle.main.bundleIdentifier!,
26-
category: String(describing: ESPProvisionProvider.self)
27-
)
2824

2925
init(apiURL: URL) {
3026
self.apiURL = apiURL
@@ -55,11 +51,11 @@ struct BatteryProvisionAPIREST: BatteryProvisionAPI {
5551
request.httpBody = try JSONEncoder().encode(ProvisionRequestBody(deviceId: deviceId, password: password))
5652
let (data, response) = try await URLSession.shared.data(for: request)
5753
guard let response = response as? HTTPURLResponse else {
58-
Self.logger.error("Received a non HTTP response")
54+
ORLogger.espprovisioning.error("Received a non HTTP response")
5955
throw BatteryProvisionAPIError.communicationError("Invalid response format")
6056
}
6157
guard (200...299).contains(response.statusCode) else {
62-
Self.logger.info("HTTP call error, status code \(response.statusCode)")
58+
ORLogger.espprovisioning.warning("HTTP call error, status code \(response.statusCode)")
6359
switch response.statusCode {
6460
case 401:
6561
throw BatteryProvisionAPIError.unauthorized
@@ -72,12 +68,12 @@ struct BatteryProvisionAPIREST: BatteryProvisionAPI {
7268
if let mimeType = response.mimeType,
7369
mimeType == "application/json",
7470
let dataString = String(data: data, encoding: .utf8) {
75-
Self.logger.info("Received JSON response from server \(dataString)")
71+
ORLogger.espprovisioning.info("Received JSON response from server \(dataString)")
7672
let assetId = try JSONDecoder().decode(ProvisionResponseBody.self, from: data).assetId
7773
return assetId
7874
}
7975
} catch {
80-
print(error.localizedDescription)
76+
ORLogger.espprovisioning.error("\(error.localizedDescription)")
8177
throw BatteryProvisionAPIError.genericError(error)
8278
}
8379
throw BatteryProvisionAPIError.unknownError

ORLib/ConsoleProviders/ESPProvision/ESPProvisionProvider.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ import os
2525
typealias SendDataCallback = ([String: Any]) -> (Void)
2626

2727
class ESPProvisionProvider: NSObject {
28-
private static let logger = Logger(
29-
subsystem: Bundle.main.bundleIdentifier!,
30-
category: String(describing: ESPProvisionProvider.self)
31-
)
3228
public static let espProvisionDisabledKey = "espProvisionDisabled"
3329

3430
let userdefaults = UserDefaults(suiteName: DefaultsKey.groupEntitlement)

ORLib/ConsoleProviders/GeofenceProvider.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import UIKit
2121
import CoreLocation
22+
import os
2223

2324
public class GeofenceProvider: NSObject, URLSessionDelegate {
2425

@@ -165,7 +166,7 @@ public class GeofenceProvider: NSObject, URLSessionDelegate {
165166
}
166167
self.clearAllRegions()
167168
NSLog("%@", "Geofences count: \(geofences.count)")
168-
print("Geofences count: \(geofences.count)")
169+
ORLogger.geofence.info("Geofences count: \(geofences.count)")
169170
for geofence in geofences {
170171
self.addGeofence(geofence: geofence)
171172
}

ORLib/Network/HttpApiManager.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919

2020
import UIKit
21+
import os
2122

2223
enum HttpMethod: String {
2324
case get = "GET"
@@ -82,7 +83,7 @@ public class HttpApiManager: NSObject, ApiManager {
8283
}
8384

8485
guard let responseModel = try? self.decoder.decode(ORConsoleConfig.self, from: responseData) else {
85-
print("Couldn't parse response: \(String(data: responseData, encoding: .utf8)!)")
86+
ORLogger.config.error("Couldn't parse response: \(String(data: responseData, encoding: .utf8)!)")
8687
callback?(httpStatusCode, nil, error);
8788
return;
8889
}
@@ -115,7 +116,7 @@ public class HttpApiManager: NSObject, ApiManager {
115116
}
116117

117118
guard let responseModel = try? self.decoder.decode(ORConsoleConfig.self, from: responseData) else {
118-
print("Couldn't parse response: \(String(data: responseData, encoding: .utf8)!)")
119+
ORLogger.config.error("Couldn't parse response: \(String(data: responseData, encoding: .utf8)!)")
119120
continuation.resume(throwing: ApiManagerError.parsingError(httpStatusCode))
120121
return
121122
}
@@ -148,7 +149,7 @@ public class HttpApiManager: NSObject, ApiManager {
148149
}
149150

150151
guard let responseModel = try? self.decoder.decode([String].self, from: responseData) else {
151-
print("Couldn't parse response: \(String(data: responseData, encoding: .utf8)!)")
152+
ORLogger.network.error("Couldn't parse response: \(String(data: responseData, encoding: .utf8)!)")
152153
callback?(httpStatusCode, nil, error);
153154
return;
154155
}
@@ -181,7 +182,7 @@ public class HttpApiManager: NSObject, ApiManager {
181182
}
182183

183184
guard let responseModel = try? self.decoder.decode([String].self, from: responseData) else {
184-
print("Couldn't parse response: \(String(data: responseData, encoding: .utf8)!)")
185+
ORLogger.network.error("Couldn't parse response: \(String(data: responseData, encoding: .utf8)!)")
185186
continuation.resume(throwing: ApiManagerError.parsingError(httpStatusCode))
186187
return
187188
}
@@ -216,7 +217,7 @@ public class HttpApiManager: NSObject, ApiManager {
216217
}
217218

218219
guard let responseModel = try? self.decoder.decode(ORAppInfo.self, from: responseData) else {
219-
print("Couldn't parse response: \(String(data: responseData, encoding: .utf8)!)")
220+
ORLogger.network.error("Couldn't parse response: \(String(data: responseData, encoding: .utf8)!)")
220221
continuation.resume(throwing: ApiManagerError.parsingError(httpStatusCode))
221222
return
222223
}
@@ -274,7 +275,7 @@ public class HttpApiManager: NSObject, ApiManager {
274275
}
275276

276277
guard let responseModel = try? self.decoder.decode(T.self, from: responseData) else {
277-
print("Couldn't parse response: \(String(data: responseData, encoding: .utf8)!)")
278+
ORLogger.network.error("Couldn't parse response: \(String(data: responseData, encoding: .utf8)!)")
278279
callback?(httpStatusCode, nil, error);
279280
return;
280281
}
@@ -300,7 +301,7 @@ public class HttpApiManager: NSObject, ApiManager {
300301
}
301302

302303
guard let responseModel = try? self.decoder.decode(R.self, from: responseData) else {
303-
print("Couldn't parse response: \(String(data: responseData, encoding: .utf8)!)")
304+
ORLogger.network.error("Couldn't parse response: \(String(data: responseData, encoding: .utf8)!)")
304305
callback?(httpStatusCode, nil, error);
305306
return;
306307
}
@@ -336,7 +337,7 @@ public class HttpApiManager: NSObject, ApiManager {
336337
}
337338

338339
guard let responseModel = try? self.decoder.decode(R.self, from: responseData) else {
339-
print("Couldn't parse response: \(String(data: responseData, encoding: .utf8)!)")
340+
ORLogger.network.error("Couldn't parse response: \(String(data: responseData, encoding: .utf8)!)")
340341
callback?(httpStatusCode, nil, error);
341342
return;
342343
}

ORLib/Network/ORNotificationResource.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919

2020
import UIKit
21+
import os
2122

2223
public class ORNotificationResource: NSObject, URLSessionDelegate {
2324

@@ -44,7 +45,7 @@ public class ORNotificationResource: NSObject, URLSessionDelegate {
4445
let error = NSError(domain: "", code: 0, userInfo: [
4546
NSLocalizedDescriptionKey : NSLocalizedString("ErrorCallingAPI", value: "Could not get data", comment: "")
4647
])
47-
print(error)
48+
ORLogger.network.error("\(error)")
4849
}
4950
}
5051
})
@@ -74,7 +75,7 @@ public class ORNotificationResource: NSObject, URLSessionDelegate {
7475
let error = NSError(domain: "", code: 0, userInfo: [
7576
NSLocalizedDescriptionKey : NSLocalizedString("ErrorCallingAPI", value: "Could not get data", comment: "")
7677
])
77-
print(error)
78+
ORLogger.network.error("\(error)")
7879
}
7980
}
8081
})

ORLib/UI/ORViewController.swift

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import Foundation
2121
import OSLog
2222
import UIKit
2323
import WebKit
24+
import os
2425

2526
open class ORViewcontroller : UIViewController {
2627
private static let logger = Logger(
@@ -95,7 +96,7 @@ open class ORViewcontroller : UIViewController {
9596
DispatchQueue.main.async {
9697
self.myWebView?.evaluateJavaScript("\(returnMessage)", completionHandler: { (any, error) in
9798
if let err = error {
98-
print(err)
99+
ORLogger.webview.error("\(err)")
99100
}
100101
})
101102
}
@@ -201,8 +202,8 @@ open class ORViewcontroller : UIViewController {
201202
}
202203

203204
internal func handleError(errorCode: Int, description: String, failingUrl: String, isForMainFrame: Bool) {
204-
print("Error requesting '\(failingUrl)': \(errorCode) (\(description))")
205-
205+
ORLogger.network.error("Error requesting '\(failingUrl)': \(errorCode) (\(description))")
206+
206207
if !self.offlineVIewControllerPresented {
207208
let alertView = UIAlertController(title: "Error", message: "Error requesting '\(failingUrl)': \(errorCode) (\(description))", preferredStyle: .alert)
208209

@@ -262,7 +263,7 @@ extension ORViewcontroller: WKScriptMessageHandler {
262263
sendData(data: disableData)
263264
}
264265
default:
265-
print("Wrong action \(action) for \(provider)")
266+
ORLogger.providers.error("Wrong action \(action) for \(provider)")
266267
}
267268
case Providers.geofence:
268269
switch(action) {
@@ -291,7 +292,7 @@ extension ORViewcontroller: WKScriptMessageHandler {
291292
case Actions.geofenceRefresh:
292293
geofenceProvider?.refreshGeofences()
293294
default:
294-
print("Wrong action \(action) for \(provider)")
295+
ORLogger.providers.error("Wrong action \(action) for \(provider)")
295296
}
296297
case Providers.storage:
297298
switch(action) {
@@ -315,7 +316,7 @@ extension ORViewcontroller: WKScriptMessageHandler {
315316
sendData(data: retrieveData)
316317
}
317318
default:
318-
print("Wrong action \(action) for \(provider)")
319+
ORLogger.providers.error("Wrong action \(action) for \(provider)")
319320
}
320321
case Providers.qr:
321322
switch (action) {
@@ -340,7 +341,7 @@ extension ORViewcontroller: WKScriptMessageHandler {
340341
self.sendData(data: scannedData)
341342
})
342343
default:
343-
print("Wrong action \(action) for \(provider)")
344+
ORLogger.providers.error("Wrong action \(action) for \(provider)")
344345
}
345346
case Providers.ble:
346347
switch (action) {
@@ -390,7 +391,7 @@ extension ORViewcontroller: WKScriptMessageHandler {
390391
}
391392
}
392393
default:
393-
print("Wrong action \(action) for \(provider)")
394+
ORLogger.providers.error("Wrong action \(action) for \(provider)")
394395
}
395396
case Providers.espprovision:
396397
switch(action) {
@@ -463,10 +464,10 @@ extension ORViewcontroller: WKScriptMessageHandler {
463464
self.sendData(data: payload)
464465
}
465466
default:
466-
print("Wrong action \(action) for \(provider)")
467+
ORLogger.providers.error("Wrong action \(action) for \(provider)")
467468
}
468469
default:
469-
print("Unknown provider type: \(provider )")
470+
ORLogger.providers.error("Unknown provider type: \(provider )")
470471
}
471472
}
472473
}
@@ -475,7 +476,7 @@ extension ORViewcontroller: WKScriptMessageHandler {
475476
clearWebBackForwardList()
476477
break
477478
default:
478-
print("Unknown message type: \(type )")
479+
ORLogger.webview.error("Unknown message type: \(type )")
479480
}
480481
}
481482
}
@@ -593,7 +594,7 @@ extension ORViewcontroller: WKNavigationDelegate {
593594
extension ORViewcontroller: ConnectivityDelegate {
594595
func connectivityStatusDidChange(isConnected: Bool) {
595596
DispatchQueue.main.async {
596-
print("connection changed \(isConnected)")
597+
ORLogger.network.info("connection changed \(isConnected)")
597598
if isConnected {
598599
if (self.offlineVIewControllerPresented) {
599600
self.reloadWebView()

ORLib/Utils/ORLogger.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2025, OpenRemote Inc.
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License as
6+
* published by the Free Software Foundation, either version 3 of the
7+
* License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Affero General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Affero General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*
17+
* SPDX-License-Identifier: AGPL-3.0-or-later
18+
*/
19+
20+
import Foundation
21+
import os
22+
23+
public struct ORLogger {
24+
private static let subsystem = Bundle.main.bundleIdentifier ?? "io.openremote.orlib"
25+
26+
public static let network = Logger(subsystem: subsystem, category: "Network")
27+
public static let bluetooth = Logger(subsystem: subsystem, category: "Bluetooth")
28+
public static let providers = Logger(subsystem: subsystem, category: "Providers")
29+
public static let espprovisioning = Logger(subsystem: subsystem, category: "ESPProvisioning")
30+
public static let webview = Logger(subsystem: subsystem, category: "WebView")
31+
public static let config = Logger(subsystem: subsystem, category: "Configuration")
32+
public static let geofence = Logger(subsystem: subsystem, category: "Geofence")
33+
public static let utils = Logger(subsystem: subsystem, category: "Utils")
34+
public static let test = Logger(subsystem: subsystem, category: "Test")
35+
}

ORLib/Utils/String+Utils.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919

2020
import Foundation
21+
import os
2122

2223
extension String {
2324

@@ -40,7 +41,7 @@ extension String {
4041
return "https://[\(self)]"
4142
}
4243
} catch let error as NSError {
43-
print("Error creating NSRegularExpression: \(error)")
44+
ORLogger.utils.error("Error creating NSRegularExpression: \(error)")
4445
}
4546

4647
let numberOfMatches: Int
@@ -49,7 +50,7 @@ extension String {
4950
numberOfMatches = schemePrefix.numberOfMatches(in: self, options: [], range: NSRange(location: 0, length: self.utf16.count))
5051
} catch let error as NSError {
5152
numberOfMatches = 0
52-
print("Error creating NSRegularExpression: \(error)")
53+
ORLogger.utils.error("Error creating NSRegularExpression: \(error)")
5354
}
5455
if numberOfMatches == 1 {
5556
if self.firstIndex(of: ".") != nil || self.firstIndex(of: "[") != nil {

ORLib/Utils/WKWebView+Utils.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919

2020
import WebKit
21+
import os
2122

2223
extension WKWebView {
2324

@@ -36,7 +37,7 @@ extension WKWebView {
3637
WKWebsiteDataStore.default().fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { records in
3738
records.forEach { record in
3839
WKWebsiteDataStore.default().removeData(ofTypes: record.dataTypes, for: [record], completionHandler: {})
39-
print("[WebCacheCleaner] Record \(record) deleted")
40+
ORLogger.webview.info("[WebCacheCleaner] Record \(record) deleted")
4041
}
4142
}
4243
WKWebsiteDataStore.default().httpCookieStore.getAllCookies { (cookies) in

0 commit comments

Comments
 (0)