Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a customisation point for injecting a logger in RxBluetoothKit #341

Merged
merged 5 commits into from
Jul 26, 2019

Conversation

vladcorneci
Copy link
Contributor

Adds the capability to route the RxBluetoothKit logs in another app by exposing a customisation point.
Each custom logger must conform to Logger protocol.

The current functionality remains unchanged unless another logger is injected.

@CLAassistant
Copy link

CLAassistant commented May 21, 2019

CLA assistant check
All committers have signed the CLA.

@vladcorneci vladcorneci changed the title Add a customisation point for injecting a custom logger in RxBluetoothKit Add a customisation point for injecting a logger in RxBluetoothKit May 21, 2019
@vladcorneci vladcorneci force-pushed the asaltvld30/add-injectable-logger branch from c65f1d8 to 33e5517 Compare May 21, 2019 14:22
Copy link
Collaborator

@minixT minixT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @asaltvld30 for your pull requests. This is a great extension to the library and can be useful feature for some projects.

I've added some comments to your changes. Please update your pull request.

@minixT
Copy link
Collaborator

minixT commented May 31, 2019

Can you also prepare some example how to use custom logger which I can add to the wiki page?

@vladcorneci
Copy link
Contributor Author

Can you also prepare some example how to use custom logger which I can add to the wiki page?

Sure.
A custom logger can be created by simply implementing the Logger protocol which specifies two logging methods and log level getters and setters.

For example, we can create a simple logger which counts the number of logs for each log level.

private class SimpleCountLogger: Logger {
    private var logCount: [UInt]

    private var currentLogLevel: RxBluetoothKitLog.LogLevel = .verbose

    init() {
        logCount = [UInt](repeating: 0, count: Int(UInt8.max))
    }

    public func getLogCount() -> [UInt] {
        return logCount
    }

    public func setLogLevel(_ logLevel: RxBluetoothKitLog.LogLevel) {
        self.currentLogLevel = logLevel
    }

    public func getLogLevel() -> RxBluetoothKitLog.LogLevel {
        return currentLogLevel
    }

    func log(
        _ message: @autoclosure () -> String,
        level: RxBluetoothKitLog.LogLevel,
        file: StaticString,
        function: StaticString,
        line: UInt
    ) {
        log(
            message(),
            level: level,
            file: String(describing: file),
            function: String(describing: function),
            line: line
        )
    }

    func log(
        _ message: @autoclosure () -> String,
        level: RxBluetoothKitLog.LogLevel,
        file: String,
        function: String,
        line: UInt
    ) {
        logCount[Int(level.rawValue)] += 1
    }
}

After this, one can simply inject it into the library by modifying RxBluetoothKitLogger.defaultLogger variable. A good place for injecting it can be the AppDelegate.swift.

@minixT minixT self-requested a review July 26, 2019 09:14
@minixT minixT merged commit 7b77989 into Polidea:master Jul 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants