Skip to content

Commit 2fbf0b2

Browse files
authored
[Feature] Allow logs to be disabled (#47)
* Added a logging manager. * Using the logging manager everywhere. * Cleanup * Update the readme
1 parent 9ed2461 commit 2fbf0b2

File tree

5 files changed

+53
-12
lines changed

5 files changed

+53
-12
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ centralManager.eventPublisher
145145
.store(in: &cancellables)
146146
```
147147

148+
### Logging
149+
150+
The library uses `os.log` to provide logging for several operations. These logs are enabled by default. If you wish to disable them, you can do:
151+
152+
```
153+
AsyncBluetoothLogging.isEnabled = false
154+
```
155+
148156
## Examples
149157

150158
You can find practical, tasty recipes for how to use `AsyncBluetooth` in the

Sources/CentralManager/CentralManager.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import os.log
88
/// An object that scans for, discovers, connects to, and manages peripherals using concurrency.
99
public class CentralManager {
1010

11+
public static func test() {
12+
logger.log("WHATS UP!?")
13+
}
14+
1115
private typealias Utils = CentralManagerUtils
1216

1317
fileprivate class DelegateWrapper: NSObject {
@@ -18,10 +22,9 @@ public class CentralManager {
1822
}
1923
}
2024

21-
private static let logger = Logger(
22-
subsystem: Bundle(for: CentralManager.self).bundleIdentifier ?? "",
23-
category: "centralManager"
24-
)
25+
private static var logger: Logger {
26+
Logging.logger(for: "centralManager")
27+
}
2528

2629
public var bluetoothState: CBManagerState {
2730
self.cbCentralManager.state

Sources/Peripheral/Peripheral.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ import os.log
99
/// - This class acts as a wrapper around `CBPeripheral`.
1010
public class Peripheral {
1111

12-
private static let logger = Logger(
13-
subsystem: Bundle(for: Peripheral.self).bundleIdentifier ?? "",
14-
category: "peripheral"
15-
)
12+
private static var logger: Logger {
13+
Logging.logger(for: "peripheral")
14+
}
1615

1716
/// Publishes characteristics that are notifying of value changes.
1817
public lazy var characteristicValueUpdatedPublisher: AnyPublisher<Characteristic, Never> = {

Sources/Peripheral/PeripheralDelegate.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import CoreBluetooth
55
import os.log
66

77
class PeripheralDelegate: NSObject {
8-
private static let logger = Logger(
9-
subsystem: Bundle(for: PeripheralDelegate.self).bundleIdentifier ?? "",
10-
category: "peripheralDelegate"
11-
)
8+
9+
private static var logger: Logger {
10+
Logging.logger(for: "peripheralDelegate")
11+
}
1212

1313
let context = PeripheralContext()
1414
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) 2024 Manuel Fernandez. All rights reserved.
2+
3+
import Foundation
4+
import os.log
5+
6+
public class AsyncBluetoothLogging {
7+
8+
public static var isEnabled = true
9+
10+
private static var loggers: [String: Logger] = [:]
11+
private static let disabledLogger = Logger(OSLog.disabled)
12+
13+
static func logger(for category: String) -> Logger {
14+
guard Self.isEnabled else { return Self.disabledLogger }
15+
16+
if let logger = Self.loggers[category] {
17+
return logger
18+
}
19+
20+
let logger = Logger(
21+
subsystem: Bundle(for: CentralManager.self).bundleIdentifier ?? "",
22+
category: category
23+
)
24+
25+
Self.loggers[category] = logger
26+
27+
return logger
28+
}
29+
}
30+
31+
typealias Logging = AsyncBluetoothLogging

0 commit comments

Comments
 (0)