Skip to content

Commit 6690787

Browse files
authored
Lazy logs (#6439)
1 parent 8300e2f commit 6690787

File tree

7 files changed

+219
-4
lines changed

7 files changed

+219
-4
lines changed

libnavigation-base/src/main/java/com/mapbox/navigation/base/route/NavigationRoute.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,9 @@ class NavigationRoute internal constructor(
184184
}
185185
val deferredRouteOptionsParsing = async(ThreadController.DefaultDispatcher) {
186186
RouteOptions.fromUrl(URL(routeRequestUrl)).also {
187-
logD(
188-
"parsed request url to RouteOptions: ${it.toUrl("***")}",
189-
LOG_CATEGORY
190-
)
187+
logD(LOG_CATEGORY) {
188+
"parsed request url to RouteOptions: ${it.toUrl("***")}"
189+
}
191190
}
192191
}
193192
create(

libnavigation-util/src/main/java/com/mapbox/common/NativeLoggerWrapper.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ internal object NativeLoggerWrapper {
2020
fun error(message: String, category: String?) {
2121
Log.error(message, category)
2222
}
23+
24+
fun getLogLevel(category: String) = LogConfiguration.getLoggingLevel(category)
2325
}

libnavigation-util/src/main/java/com/mapbox/navigation/utils/internal/LoggerFrontend.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.mapbox.navigation.utils.internal
22

3+
import com.mapbox.common.LoggingLevel
34
import com.mapbox.common.NativeLoggerWrapper
45

56
private const val NAV_SDK_CATEGORY = "nav-sdk"
67

78
interface LoggerFrontend {
9+
fun getLogLevel(): LoggingLevel?
810
fun logV(msg: String, category: String? = null)
911
fun logD(msg: String, category: String? = null)
1012
fun logI(msg: String, category: String? = null)
@@ -13,6 +15,9 @@ interface LoggerFrontend {
1315
}
1416

1517
internal class MapboxCommonLoggerFrontend : LoggerFrontend {
18+
19+
override fun getLogLevel() = NativeLoggerWrapper.getLogLevel(NAV_SDK_CATEGORY)
20+
1621
override fun logV(msg: String, category: String?) {
1722
val message = createMessage(msg, category)
1823
// There's no com.mapbox.common.Log.verbose available - using Log.debug instead

libnavigation-util/src/main/java/com/mapbox/navigation/utils/internal/LoggerProvider.kt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.mapbox.navigation.utils.internal
22

33
import androidx.annotation.VisibleForTesting
44
import com.mapbox.base.common.logger.Logger
5+
import com.mapbox.common.LoggingLevel
56

67
/**
78
* Singleton provider of [Logger].
@@ -39,6 +40,18 @@ fun logD(msg: String, category: String? = null) {
3940
LoggerProvider.frontend.logD(msg, category)
4041
}
4142

43+
/**
44+
* @param category optional string to identify the source or category of the log message.
45+
* @param lazyMsg is a lazy message to log. The lazy message isn't executed if current log level is less verbose than Debug.
46+
* Noting that the category is appended to the log message to give extra context along with the `[nav-sdk]` parent category.
47+
* As an example, this is how the logs would look like `D/Mapbox: [nav-sdk] [ConnectivityHandler] NetworkStatus=ReachableViaWiFi`.
48+
*/
49+
inline fun logD(category: String? = null, lazyMsg: () -> String) {
50+
if (logLevel().atLeast(LoggingLevel.DEBUG)) {
51+
logD(lazyMsg(), category)
52+
}
53+
}
54+
4255
/**
4356
* @param msg to log.
4457
* @param category optional string to identify the source or category of the log message.
@@ -49,6 +62,18 @@ fun logI(msg: String, category: String? = null) {
4962
LoggerProvider.frontend.logI(msg, category)
5063
}
5164

65+
/**
66+
* @param category optional string to identify the source or category of the log message.
67+
* @param lazyMsg is a lazy message to log. The lazy message isn't executed if current log level is less verbose than Info.
68+
* Noting that the category is appended to the log message to give extra context along with the `[nav-sdk]` parent category.
69+
* As an example, this is how the logs would look like `I/Mapbox: [nav-sdk] [ConnectivityHandler] NetworkStatus=ReachableViaWiFi`.
70+
*/
71+
inline fun logI(category: String? = null, lazyMsg: () -> String) {
72+
if (logLevel().atLeast(LoggingLevel.INFO)) {
73+
logI(lazyMsg(), category)
74+
}
75+
}
76+
5277
/**
5378
* @param msg to log.
5479
* @param category optional string to identify the source or category of the log message.
@@ -59,6 +84,18 @@ fun logW(msg: String, category: String? = null) {
5984
LoggerProvider.frontend.logW(msg, category)
6085
}
6186

87+
/**
88+
* @param category optional string to identify the source or category of the log message.
89+
* @param lazyMsg is a lazy message to log. The lazy message isn't executed if current log level is less verbose than Warning.
90+
* Noting that the category is appended to the log message to give extra context along with the `[nav-sdk]` parent category.
91+
* As an example, this is how the logs would look like `W/Mapbox: [nav-sdk] [ConnectivityHandler] NetworkStatus=ReachableViaWiFi`.
92+
*/
93+
inline fun logW(category: String? = null, lazyMsg: () -> String) {
94+
if (logLevel().atLeast(LoggingLevel.WARNING)) {
95+
logW(lazyMsg(), category)
96+
}
97+
}
98+
6299
/**
63100
* @param msg to log.
64101
* @param category optional string to identify the source or category of the log message.
@@ -68,3 +105,21 @@ fun logW(msg: String, category: String? = null) {
68105
fun logE(msg: String, category: String? = null) {
69106
LoggerProvider.frontend.logE(msg, category)
70107
}
108+
109+
/**
110+
* @param category optional string to identify the source or category of the log message.
111+
* @param lazyMsg is a lazy message to log. The lazy message isn't executed if current log level is less verbose than Error.
112+
* Noting that the category is appended to the log message to give extra context along with the `[nav-sdk]` parent category.
113+
* As an example, this is how the logs would look like `E/Mapbox: [nav-sdk] [ConnectivityHandler] NetworkStatus=ReachableViaWiFi`.
114+
*/
115+
inline fun logE(category: String? = null, lazyMsg: () -> String) {
116+
if (logLevel().atLeast(LoggingLevel.ERROR)) {
117+
logE(lazyMsg(), category)
118+
}
119+
}
120+
121+
/**
122+
* Should not be used directly.
123+
* Added to support inline calls. Public inline functions can use only public API inside.
124+
*/
125+
fun logLevel() = LoggerProvider.frontend.getLogLevel()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.mapbox.navigation.utils.internal
2+
3+
import com.mapbox.common.LoggingLevel
4+
5+
fun LoggingLevel?.atLeast(loggingLevel: LoggingLevel): Boolean {
6+
return toPriority(this) >= toPriority(loggingLevel)
7+
}
8+
9+
private fun toPriority(loggingLevel: LoggingLevel?) = when (loggingLevel) {
10+
null -> 0
11+
LoggingLevel.DEBUG -> 1
12+
LoggingLevel.INFO -> 2
13+
LoggingLevel.WARNING -> 3
14+
LoggingLevel.ERROR -> 4
15+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package com.mapbox.navigation.utils.internal
2+
3+
import com.mapbox.common.LoggingLevel
4+
import org.junit.Assert.assertEquals
5+
import org.junit.Test
6+
import org.junit.runner.RunWith
7+
import org.junit.runners.Parameterized
8+
9+
@RunWith(Parameterized::class)
10+
class LoggingLevelUtilKtTest(val parameter: LogLevelData) {
11+
12+
companion object {
13+
14+
@JvmStatic
15+
@Parameterized.Parameters(name = "{0}")
16+
fun parameters() = listOf(
17+
LogLevelData(
18+
what = null,
19+
atLeast = LoggingLevel.ERROR,
20+
expected = false
21+
),
22+
LogLevelData(
23+
what = null,
24+
atLeast = LoggingLevel.WARNING,
25+
expected = false
26+
),
27+
LogLevelData(
28+
what = null,
29+
atLeast = LoggingLevel.INFO,
30+
expected = false
31+
),
32+
LogLevelData(
33+
what = null,
34+
atLeast = LoggingLevel.DEBUG,
35+
expected = false
36+
),
37+
38+
LogLevelData(
39+
what = LoggingLevel.ERROR,
40+
atLeast = LoggingLevel.ERROR,
41+
expected = true
42+
),
43+
LogLevelData(
44+
what = LoggingLevel.ERROR,
45+
atLeast = LoggingLevel.WARNING,
46+
expected = true
47+
),
48+
LogLevelData(
49+
what = LoggingLevel.ERROR,
50+
atLeast = LoggingLevel.INFO,
51+
expected = true
52+
),
53+
LogLevelData(
54+
what = LoggingLevel.ERROR,
55+
atLeast = LoggingLevel.DEBUG,
56+
expected = true
57+
),
58+
59+
LogLevelData(
60+
what = LoggingLevel.WARNING,
61+
atLeast = LoggingLevel.ERROR,
62+
expected = false
63+
),
64+
LogLevelData(
65+
what = LoggingLevel.WARNING,
66+
atLeast = LoggingLevel.WARNING,
67+
expected = true
68+
),
69+
LogLevelData(
70+
what = LoggingLevel.WARNING,
71+
atLeast = LoggingLevel.INFO,
72+
expected = true
73+
),
74+
LogLevelData(
75+
what = LoggingLevel.WARNING,
76+
atLeast = LoggingLevel.DEBUG,
77+
expected = true
78+
),
79+
80+
LogLevelData(
81+
what = LoggingLevel.INFO,
82+
atLeast = LoggingLevel.ERROR,
83+
expected = false
84+
),
85+
LogLevelData(
86+
what = LoggingLevel.INFO,
87+
atLeast = LoggingLevel.WARNING,
88+
expected = false
89+
),
90+
LogLevelData(
91+
what = LoggingLevel.INFO,
92+
atLeast = LoggingLevel.INFO,
93+
expected = true
94+
),
95+
LogLevelData(
96+
what = LoggingLevel.INFO,
97+
atLeast = LoggingLevel.DEBUG,
98+
expected = true
99+
),
100+
101+
LogLevelData(
102+
what = LoggingLevel.DEBUG,
103+
atLeast = LoggingLevel.ERROR,
104+
expected = false
105+
),
106+
LogLevelData(
107+
what = LoggingLevel.DEBUG,
108+
atLeast = LoggingLevel.WARNING,
109+
expected = false
110+
),
111+
LogLevelData(
112+
what = LoggingLevel.DEBUG,
113+
atLeast = LoggingLevel.INFO,
114+
expected = false
115+
),
116+
LogLevelData(
117+
what = LoggingLevel.DEBUG,
118+
atLeast = LoggingLevel.DEBUG,
119+
expected = true
120+
),
121+
)
122+
}
123+
124+
@Test
125+
fun `test atLeast`() {
126+
val result = parameter.what.atLeast(parameter.atLeast)
127+
assertEquals(parameter.expected, result)
128+
}
129+
130+
data class LogLevelData(
131+
val what: LoggingLevel?,
132+
val atLeast: LoggingLevel,
133+
val expected: Boolean
134+
)
135+
}

libtesting-navigation-util/src/main/java/com/mapbox/navigation/testing/LoggingFrontendTestRule.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package com.mapbox.navigation.testing
22

33
import android.annotation.SuppressLint
4+
import com.mapbox.common.LoggingLevel
45
import com.mapbox.navigation.utils.internal.LoggerFrontend
56
import com.mapbox.navigation.utils.internal.LoggerProvider
67
import org.junit.rules.TestRule
78
import org.junit.runner.Description
89
import org.junit.runners.model.Statement
910

1011
private object NoLoggingFrontend : LoggerFrontend {
12+
13+
override fun getLogLevel(): LoggingLevel = LoggingLevel.DEBUG
14+
1115
override fun logV(msg: String, category: String?) {
1216
}
1317

0 commit comments

Comments
 (0)