-
Notifications
You must be signed in to change notification settings - Fork 320
Lazy logs #6439
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
Lazy logs #6439
Changes from 1 commit
264ce4e
9858d51
773484f
60c5793
5a5c55c
d138292
0a76da4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,15 @@ | ||
| package com.mapbox.navigation.utils.internal | ||
|
|
||
| import com.mapbox.common.LogConfiguration | ||
| import com.mapbox.common.LoggingLevel | ||
| import com.mapbox.common.NativeLoggerWrapper | ||
|
|
||
| private const val NAV_SDK_CATEGORY = "nav-sdk" | ||
|
|
||
| interface LoggerFrontend { | ||
| fun logV(msg: String, category: String? = null) | ||
| fun logD(msg: String, category: String? = null) | ||
| fun logD(category: String? = null, lazyMsg: () -> String) | ||
| fun logI(msg: String, category: String? = null) | ||
| fun logE(msg: String, category: String? = null) | ||
| fun logW(msg: String, category: String? = null) | ||
|
|
@@ -24,6 +27,13 @@ internal class MapboxCommonLoggerFrontend : LoggerFrontend { | |
| NativeLoggerWrapper.debug(message, NAV_SDK_CATEGORY) | ||
| } | ||
|
|
||
| override fun logD(category: String?, lazyMsg: () -> String) { | ||
| when (LogConfiguration.getLoggingLevel()) { | ||
|
||
| LoggingLevel.DEBUG -> logD(lazyMsg(), category) | ||
| LoggingLevel.ERROR, LoggingLevel.INFO, LoggingLevel.WARNING -> { } | ||
VysotskiVadim marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| override fun logI(msg: String, category: String?) { | ||
| val message = createMessage(msg, category) | ||
| NativeLoggerWrapper.info(message, NAV_SDK_CATEGORY) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like that. The lambda would still be created but I don't see how we could evade that.
Even if we make the logD method inline (which is not possible with interfaces),
LogConfiguration.getLoggingLevel()is not a constant, so the lambda would not be removed anyway.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inlining doesn't require everything in the lambda to be constant, does it?
LogConfiguration.getLoggingLevel()is statically accessible to the lambda, so it should work just fine (at least that's what I see in decompiled classes). @VysotskiVadim I think it would be worth exploring how we could make this setup work with inline functions, I guess the biggest hurdle would be organizing the interface in a way that makes it possible.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made methods inline to avoid creation of an object for lambda
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, right, I was thinking in other terms. If
LogConfiguration.getLoggingLevel()was a constant and the inlining was used, the whole code might have been removed after the compilation because it would be unreachable.But now yes, we have a code that creates a lambda but it won't be invoked if unnecessary, that works.