Skip to content

Commit

Permalink
Logger code cleanup (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcobbs-bentley authored Mar 7, 2024
1 parent 86950ae commit 3d612c6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
45 changes: 29 additions & 16 deletions mobile-sdk/src/main/java/com/github/itwin/mobilesdk/ITMLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
@file:Suppress("unused", "MemberVisibilityCanBePrivate")

package com.github.itwin.mobilesdk

import android.util.Log
Expand All @@ -10,12 +12,16 @@ import java.time.format.DateTimeFormatter
import java.util.*

/**
* Class used by iTwin Mobile SDK to log information during runtime. Replace the [logger][ITMApplication.logger]
* property on [ITMApplication] with a subclass of this class for custom logging. This default logger uses
* [Log] functions for logging.
* Class used by the iTwin Mobile SDK to log information during runtime. Replace the
* [logger][ITMApplication.logger] property on [ITMApplication] with a subclass of this class for
* custom logging. This default logger uses [Log] functions for logging.
*/
@Suppress("unused")
open class ITMLogger {
/**
* Set this to `false` to completely disable logging.
*/
var enabled = true

private val dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")

/**
Expand All @@ -37,10 +43,10 @@ open class ITMLogger {
fun fromString(value: String): Severity? {
var lowercaseValue = value.lowercase(Locale.ROOT)
lowercaseValue = when (lowercaseValue) {
"log" -> "debug"
"log" -> "debug"
"assert" -> "fatal"
"warn" -> "warning"
else -> lowercaseValue
"warn" -> "warning"
else -> lowercaseValue
}

return values().firstOrNull { lowercaseValue == it.name.lowercase(Locale.ROOT) }
Expand All @@ -54,19 +60,26 @@ open class ITMLogger {
}

/**
* Logs the given message using a function from the [Log] class that is appropriate to the given
* [severity], with a tag of `"ITMLogger"`.
* If [enabled] is `true`, Logs the given message using a function from the [Log] class that is
* appropriate to the given [severity], with a tag of `"ITMLogger"`.
*
* @param severity The [Severity] of the log message.
* @param message The message to log.
*/
open fun log(severity: Severity?, message: String) {
if (!enabled) {
return
}
val logger: (String?, String) -> Int = when (severity) {
Severity.Fatal -> Log::e
Severity.Error -> Log::e
Severity.Fatal -> Log::e
Severity.Error -> Log::e
Severity.Warning -> Log::w
Severity.Info -> Log::i
Severity.Debug -> Log::d
Severity.Trace -> Log::v
else -> Log::e
Severity.Info -> Log::i
Severity.Debug -> Log::d
Severity.Trace -> Log::v
else -> Log::e
}
logger("ITMLogger", "%s: %s".format(LocalDateTime.now().format(dateFormatter), message))
val timestamp = LocalDateTime.now().format(dateFormatter)
logger("ITMLogger", "$timestamp: $message")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
@file:Suppress("unused", "MemberVisibilityCanBePrivate")

package com.github.itwin.mobilesdk

import android.webkit.JavascriptInterface
import android.webkit.WebView
import java.util.*

/**
* Logger that, when attached to a [WebView], redirects console messages from that web view to the given [callback].
* Logger that, when attached to a [WebView], redirects console messages from that web view to the
* given [callback].
*
* @param webView The [WebView] from which console output should be redirected.
* @param callback The callback function that is called when console output is received.
*/
open class ITMWebViewLogger(
@Suppress("MemberVisibilityCanBePrivate") protected val webView: WebView,
protected val webView: WebView,
protected val callback: (type: LogType, message: String) -> Unit) {
enum class LogType {
Assert,
Expand Down Expand Up @@ -62,7 +65,6 @@ open class ITMWebViewLogger(

init {
webView.addJavascriptInterface(object {
@Suppress("unused")
@JavascriptInterface
fun log(typeString: String, message: String) {
val type = try {
Expand Down

0 comments on commit 3d612c6

Please sign in to comment.