Skip to content

Commit

Permalink
Common SDK logs (#41)
Browse files Browse the repository at this point in the history
* Common SDK log

* Remove deprecated function
  • Loading branch information
DzmitryFomchyn authored May 25, 2022
1 parent 781890d commit 0a818bc
Show file tree
Hide file tree
Showing 29 changed files with 223 additions and 197 deletions.
36 changes: 0 additions & 36 deletions LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,24 +268,6 @@ License: [Mapbox Terms of Service](https://www.mapbox.com/legal/tos)

===========================================================================

Mapbox Search Android uses portions of the okhttp.
URL: [https://square.github.io/okhttp/](https://square.github.io/okhttp/)
License: [The Apache Software License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.txt)

===========================================================================

Mapbox Search Android uses portions of the okhttp-logging-interceptor.
URL: [https://square.github.io/okhttp/](https://square.github.io/okhttp/)
License: [The Apache Software License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.txt)

===========================================================================

Mapbox Search Android uses portions of the Okio.
URL: [https://github.com/square/okio/](https://github.com/square/okio/)
License: [The Apache Software License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.txt)

===========================================================================

Mapbox Search Android uses portions of the Parcelize Runtime.
URL: [https://kotlinlang.org/](https://kotlinlang.org/)
License: [The Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.txt)
Expand Down Expand Up @@ -648,24 +630,6 @@ License: [The Apache Software License, Version 2.0](http://www.apache.org/licens

===========================================================================

Mapbox Search Android uses portions of the okhttp.
URL: [https://square.github.io/okhttp/](https://square.github.io/okhttp/)
License: [The Apache Software License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.txt)

===========================================================================

Mapbox Search Android uses portions of the okhttp-logging-interceptor.
URL: [https://square.github.io/okhttp/](https://square.github.io/okhttp/)
License: [The Apache Software License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.txt)

===========================================================================

Mapbox Search Android uses portions of the Okio.
URL: [https://github.com/square/okio/](https://github.com/square/okio/)
License: [The Apache Software License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.txt)

===========================================================================

Mapbox Search Android uses portions of the Parcelize Runtime.
URL: [https://kotlinlang.org/](https://kotlinlang.org/)
License: [The Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.txt)
Expand Down
3 changes: 1 addition & 2 deletions MapboxSearch/gradle/versions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ ext {

ktlint_version = '0.39.0'
okhttp_version = '4.9.0'
okhttp_interceptor_version = '4.9.0'
okhttp_mock_version = '1.5.0'

conductor_version = "3.1.2"
Expand All @@ -43,7 +42,7 @@ ext {

mapbox_maps_version = "10.5.0"
common_sdk_version = '21.3.0'
mapbox_base_version = '0.6.0'
mapbox_base_version = '0.8.0'
mapbox_android_core_version = '5.0.1'

search_native_version = '0.54.1'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.mapbox.common

import androidx.annotation.VisibleForTesting

/**
* This class in a "com.mapbox.common" package
* because we need to access package-private com.mapbox.common.Log class from the Common SDK.
*/
internal object CommonSdkLog {

private const val SDK_IDENTIFIER = "search-sdk-android"

private var logger: LogImpl? = CommonSdkLogImpl()

/**
* Resets implementation that uses Common SDK class which can't be loaded in unit tests.
*/
@VisibleForTesting
fun resetLogImpl() {
logger = null
}

@VisibleForTesting
fun reinitializeLogImpl() {
logger = CommonSdkLogImpl()
}

fun logd(tag: String?, message: String) {
logger?.logd(tag, message)
}

fun logi(tag: String?, message: String) {
logger?.logi(tag, message)
}

fun logw(tag: String?, message: String) {
logger?.logw(tag, message)
}

fun loge(tag: String?, message: String) {
logger?.loge(tag, message)
}

private fun formatCategory(tag: String?): String {
return when (tag) {
null -> SDK_IDENTIFIER
else -> "$SDK_IDENTIFIER\\$tag"
}
}

private interface LogImpl {
fun logd(tag: String?, message: String)

fun logi(tag: String?, message: String)

fun logw(tag: String?, message: String)

fun loge(tag: String?, message: String)
}

private class CommonSdkLogImpl : LogImpl {
override fun logd(tag: String?, message: String) {
Log.debug(message, formatCategory(tag))
}

override fun logi(tag: String?, message: String) {
Log.info(message, formatCategory(tag))
}

override fun logw(tag: String?, message: String) {
Log.warning(message, formatCategory(tag))
}

override fun loge(tag: String?, message: String) {
Log.error(message, formatCategory(tag))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ inline fun assertDebug(value: Boolean, message: () -> Any) {

if (!value) {
logw(message().toString())
@Suppress("DEPRECATION")
reportError(IllegalStateException(message().toString()))
CommonErrorsReporter.reporter?.invoke(IllegalStateException(message().toString()))
}
}

Expand All @@ -20,8 +19,7 @@ inline fun failDebug(cause: Throwable? = null, message: () -> Any) {
if (BuildConfig.DEBUG) {
throw exception
} else {
@Suppress("DEPRECATION")
reportError(exception)
CommonErrorsReporter.reporter?.invoke(exception)
}
logw(message().toString())
}
Expand All @@ -31,38 +29,12 @@ inline fun throwDebug(e: Throwable? = null, message: () -> Any = { "Error!" }) {
if (BuildConfig.DEBUG) {
throw exception
}
if (e != null) {
logw(e, message().toString())
} else {
logw(message().toString())
}
}

fun reportRelease(e: Throwable, message: String) {
reportRelease(e) {
message
}
loge(message().toString())
}

inline fun reportRelease(e: Throwable, message: () -> Any = { "Error!" }) {
loge(e, message().toString())
fun reportRelease(e: Throwable, message: String = "Error occurred") {
loge("$message. Error: ${e.message}")
if (!BuildConfig.DEBUG) {
@Suppress("DEPRECATION")
reportError(e)
}
}

@Deprecated(
"""
Do not use this method inside SDK code.
Better use a more suitable "reportRelease(e)" method.
""",
replaceWith = ReplaceWith("reportRelease(e)"))
fun reportError(e: Throwable) {
val reporter = CommonErrorsReporter.reporter
if (reporter != null) {
reporter(e)
} else {
logw("Errors reported is not initialized")
CommonErrorsReporter.reporter?.invoke(e)
}
}
Original file line number Diff line number Diff line change
@@ -1,41 +1,30 @@
package com.mapbox.search.common.logger

import com.mapbox.base.common.logger.Logger
import com.mapbox.base.common.logger.model.Message
import com.mapbox.base.common.logger.model.Tag
import androidx.annotation.VisibleForTesting
import com.mapbox.common.CommonSdkLog

const val DEFAULT_SEARCH_SDK_LOG_TAG = "SearchSDK"

var searchSdkLogger: Logger? = null

fun logd(throwable: Throwable, message: String, tag: String = DEFAULT_SEARCH_SDK_LOG_TAG) {
searchSdkLogger?.d(tag = Tag(tag), msg = Message(message), tr = throwable)
}

fun logd(message: String, tag: String = DEFAULT_SEARCH_SDK_LOG_TAG) {
searchSdkLogger?.d(tag = Tag(tag), msg = Message(message))
}

fun logi(throwable: Throwable, message: String, tag: String = DEFAULT_SEARCH_SDK_LOG_TAG) {
searchSdkLogger?.i(tag = Tag(tag), msg = Message(message), tr = throwable)
@VisibleForTesting
fun resetLogImpl() {
CommonSdkLog.resetLogImpl()
}

fun logi(message: String, tag: String = DEFAULT_SEARCH_SDK_LOG_TAG) {
searchSdkLogger?.i(tag = Tag(tag), msg = Message(message))
@VisibleForTesting
fun reinitializeLogImpl() {
CommonSdkLog.resetLogImpl()
}

fun logw(throwable: Throwable, message: String, tag: String = DEFAULT_SEARCH_SDK_LOG_TAG) {
searchSdkLogger?.w(tag = Tag(tag), msg = Message(message), tr = throwable)
fun logd(message: String, tag: String? = null) {
CommonSdkLog.logd(tag, message)
}

fun logw(message: String, tag: String = DEFAULT_SEARCH_SDK_LOG_TAG) {
searchSdkLogger?.w(tag = Tag(tag), msg = Message(message))
fun logi(message: String, tag: String? = null) {
CommonSdkLog.logi(tag, message)
}

fun loge(throwable: Throwable, message: String, tag: String = DEFAULT_SEARCH_SDK_LOG_TAG) {
searchSdkLogger?.e(tag = Tag(tag), msg = Message(message), tr = throwable)
fun logw(message: String, tag: String? = null) {
CommonSdkLog.logw(tag, message)
}

fun loge(message: String, tag: String = DEFAULT_SEARCH_SDK_LOG_TAG) {
searchSdkLogger?.e(tag = Tag(tag), msg = Message(message))
fun loge(message: String, tag: String? = null) {
CommonSdkLog.loge(tag, message)
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.mapbox.search.common.logger

import com.mapbox.base.common.logger.model.Message
import com.mapbox.base.common.logger.model.Tag
import com.mapbox.common.CommonSdkLog
import com.mapbox.test.dsl.TestCase
import io.mockk.mockk
import io.mockk.every
import io.mockk.mockkObject
import io.mockk.unmockkObject
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.TestFactory
Expand All @@ -12,12 +13,16 @@ internal class LogFunctionsTest {

@BeforeEach
fun setUp() {
searchSdkLogger = mockk(relaxed = true)
mockkObject(CommonSdkLog)
every { CommonSdkLog.logd(any(), any()) } returns Unit
every { CommonSdkLog.logi(any(), any()) } returns Unit
every { CommonSdkLog.logw(any(), any()) } returns Unit
every { CommonSdkLog.loge(any(), any()) } returns Unit
}

@AfterEach
fun tearDown() {
searchSdkLogger = null
unmockkObject(CommonSdkLog)
}

@TestFactory
Expand All @@ -26,56 +31,28 @@ internal class LogFunctionsTest {
When("Call debug log") {
logd(message = MESSAGE, tag = TAG)
VerifyOnce("Call passed to logger instance") {
searchSdkLogger?.d(tag = Tag(TAG), msg = Message(MESSAGE))
}
}

When("Call debug log with exception") {
logd(throwable = ERROR_CAUSE, message = MESSAGE, tag = TAG)
VerifyOnce("Call passed to logger instance") {
searchSdkLogger?.d(tag = Tag(TAG), msg = Message(MESSAGE), tr = ERROR_CAUSE)
CommonSdkLog.logd(TAG, MESSAGE)
}
}

When("Call info log") {
logi(message = MESSAGE, tag = TAG)
VerifyOnce("Call passed to logger instance") {
searchSdkLogger?.i(tag = Tag(TAG), msg = Message(MESSAGE))
}
}

When("Call info log with exception") {
logi(throwable = ERROR_CAUSE, message = MESSAGE, tag = TAG)
VerifyOnce("Call passed to logger instance") {
searchSdkLogger?.i(tag = Tag(TAG), msg = Message(MESSAGE), tr = ERROR_CAUSE)
CommonSdkLog.logi(TAG, MESSAGE)
}
}

When("Call warning log") {
logw(message = MESSAGE, tag = TAG)
VerifyOnce("Call passed to logger instance") {
searchSdkLogger?.w(tag = Tag(TAG), msg = Message(MESSAGE))
}
}

When("Call warning log with exception") {
logw(throwable = ERROR_CAUSE, message = MESSAGE, tag = TAG)
VerifyOnce("Call passed to logger instance") {
searchSdkLogger?.w(tag = Tag(TAG), msg = Message(MESSAGE), tr = ERROR_CAUSE)
CommonSdkLog.logw(TAG, MESSAGE)
}
}

When("Call error log") {
loge(message = MESSAGE, tag = TAG)
VerifyOnce("Call passed to logger instance") {
searchSdkLogger?.e(tag = Tag(TAG), msg = Message(MESSAGE))
}
}

When("Call error log with exception") {
loge(throwable = ERROR_CAUSE, message = MESSAGE, tag = TAG)
VerifyOnce("Call passed to logger instance") {
searchSdkLogger?.e(tag = Tag(TAG), msg = Message(MESSAGE), tr = ERROR_CAUSE)
CommonSdkLog.loge(TAG, MESSAGE)
}
}
}
Expand All @@ -84,6 +61,5 @@ internal class LogFunctionsTest {
private companion object {
const val MESSAGE = "message"
const val TAG = "tag"
val ERROR_CAUSE = Exception()
}
}
3 changes: 0 additions & 3 deletions MapboxSearch/sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@ dependencies {
implementation "androidx.collection:collection-ktx:$androidx_collection_version"
implementation "androidx.core:core-ktx:$androidx_core_version"

implementation "com.squareup.okhttp3:okhttp:$okhttp_version"
implementation "com.squareup.okhttp3:logging-interceptor:$okhttp_interceptor_version"

api "com.mapbox.common:common:$common_sdk_version"
api "com.mapbox.mapboxsdk:mapbox-android-core:$mapbox_android_core_version"

Expand Down
Loading

0 comments on commit 0a818bc

Please sign in to comment.