Skip to content
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dev.hotwire.core.bridge

import dev.hotwire.core.config.Hotwire
import dev.hotwire.core.logging.logError
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

abstract class BridgeComponentJsonConverter {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.hotwire.core.bridge

import dev.hotwire.core.logging.logError
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.decodeFromJsonElement
Expand Down
17 changes: 8 additions & 9 deletions core/src/main/kotlin/dev/hotwire/core/config/HotwireConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import android.webkit.WebView
import dev.hotwire.core.bridge.BridgeComponent
import dev.hotwire.core.bridge.BridgeComponentFactory
import dev.hotwire.core.bridge.BridgeComponentJsonConverter
import dev.hotwire.core.logging.DefaultHotwireLogger
import dev.hotwire.core.logging.HotwireLogger
import dev.hotwire.core.turbo.config.PathConfiguration
import dev.hotwire.core.turbo.http.HotwireHttpClient
import dev.hotwire.core.turbo.offline.OfflineRequestHandler
import dev.hotwire.core.turbo.webview.HotwireWebView

Expand All @@ -32,16 +33,14 @@ class HotwireConfig internal constructor() {
var offlineRequestHandler: OfflineRequestHandler? = null

/**
* Enables/disables debug logging. This should be disabled in production environments.
* Disabled by default.
* Set a custom logger to handle debug, warning, and error messages.
*
* The default logger is [DefaultHotwireLogger] which prints debug logs based on the value
* of BuildConfig.DEBUG and always prints warnings and errors to Logcat.
*
* Important: You should not enable debug logging in production release builds.
* If you'd like to change this behavior, provide your own implementation of [HotwireLogger].
*/
var debugLoggingEnabled = false
set(value) {
field = value
HotwireHttpClient.reset()
}
var logger: HotwireLogger = DefaultHotwireLogger

/**
* Enables/disables debugging of web contents loaded into WebViews.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import android.webkit.ValueCallback
import android.webkit.WebChromeClient.FileChooserParams
import androidx.activity.result.ActivityResult
import dev.hotwire.core.R
import dev.hotwire.core.logging.logError
import dev.hotwire.core.turbo.session.Session
import dev.hotwire.core.files.util.HOTWIRE_REQUEST_CODE_FILES
import dev.hotwire.core.files.util.HotwireFileProvider
import dev.hotwire.core.logging.logError
import dev.hotwire.core.turbo.session.Session
import dev.hotwire.core.turbo.util.dispatcherProvider
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
Expand Down
37 changes: 12 additions & 25 deletions core/src/main/kotlin/dev/hotwire/core/logging/CoreLog.kt
Original file line number Diff line number Diff line change
@@ -1,45 +1,32 @@
package dev.hotwire.core.logging

import android.util.Log
import dev.hotwire.core.config.Hotwire
import okhttp3.logging.HttpLoggingInterceptor

internal object CoreLog {
private const val DEFAULT_TAG = "Hotwire-Core"

private val debugEnabled get() = Hotwire.config.debugLoggingEnabled

internal fun d(msg: String) = log(Log.DEBUG, msg)

internal fun w(msg: String) = log(Log.WARN, msg)

internal fun e(msg: String) = log(Log.ERROR, msg)
private const val DEFAULT_TAG = "Hotwire-Core"
private const val PAD_END_LENGTH = 35

private fun log(logLevel: Int, msg: String) {
when (logLevel) {
Log.DEBUG -> if (debugEnabled) Log.d(DEFAULT_TAG, msg)
Log.WARN -> Log.w(DEFAULT_TAG, msg)
Log.ERROR -> Log.e(DEFAULT_TAG, msg)
}
internal class HotwireHttpLogger : HttpLoggingInterceptor.Logger {
override fun log(message: String) {
Hotwire.config.logger.d(DEFAULT_TAG, message)
}
}

private const val PAD_END_LENGTH = 35

internal fun logEvent(event: String, details: String = "") {
CoreLog.d("$event ".padEnd(PAD_END_LENGTH, '.') + " [$details]")
Hotwire.config.logger.d(DEFAULT_TAG, "$event ".padEnd(PAD_END_LENGTH, '.') + " [$details]")
}

internal fun logEvent(event: String, attributes: List<Pair<String, Any>>) {
val description = attributes.joinToString(prefix = "[", postfix = "]", separator = ", ") {
"${it.first}: ${it.second}"
}
CoreLog.d("$event ".padEnd(PAD_END_LENGTH, '.') + " $description")
Hotwire.config.logger.d(DEFAULT_TAG, "$event ".padEnd(PAD_END_LENGTH, '.') + " $description")
}

internal fun logWarning(event: String, details: String) {
CoreLog.w("$event ".padEnd(PAD_END_LENGTH, '.') + " [$details]")
Hotwire.config.logger.w(DEFAULT_TAG, "$event ".padEnd(PAD_END_LENGTH, '.') + " [$details]")
}

internal fun logError(event: String, error: Exception) {
CoreLog.e("$event: ${error.stackTraceToString()}")
}
internal fun logError(event: String, throwable: Throwable) {
Hotwire.config.logger.e(DEFAULT_TAG, "$event: ${throwable.stackTraceToString()}", throwable)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package dev.hotwire.core.logging

import android.util.Log
import dev.hotwire.core.BuildConfig

internal object DefaultHotwireLogger : HotwireLogger {
private val debugEnabled get() = BuildConfig.DEBUG

override fun d(tag: String, msg: String) =
log(Log.DEBUG, tag, msg)

override fun w(tag: String, msg: String) =
log(Log.WARN, tag, msg)

override fun e(tag: String, msg: String, throwable: Throwable?) =
log(Log.ERROR, tag, msg, throwable)

private fun log(logLevel: Int, tag: String, msg: String, throwable: Throwable? = null) {
when (logLevel) {
Log.DEBUG -> if (debugEnabled) Log.d(tag, msg)
Log.WARN -> Log.w(tag, msg)
Log.ERROR -> Log.e(tag, msg, throwable)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.hotwire.core.logging

interface HotwireLogger {
fun d(tag: String, msg: String)
fun w(tag: String, msg: String)
fun e(tag: String, msg: String, throwable: Throwable? = null)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.hotwire.core.turbo.http

import android.content.Context
import dev.hotwire.core.config.Hotwire
import dev.hotwire.core.logging.HotwireHttpLogger
import dev.hotwire.core.logging.logError
import okhttp3.Cache
import okhttp3.OkHttpClient
Expand All @@ -16,7 +16,7 @@ import java.util.concurrent.TimeUnit
object HotwireHttpClient {
private var cache: Cache? = null
private var httpCacheSize = 100L * 1024L * 1024L // 100 MBs
private val loggingInterceptor = HttpLoggingInterceptor().apply {
private val loggingInterceptor = HttpLoggingInterceptor(logger = HotwireHttpLogger()).apply {
level = HttpLoggingInterceptor.Level.BASIC
}

Expand Down Expand Up @@ -55,15 +55,12 @@ object HotwireHttpClient {
.connectTimeout(10L, TimeUnit.SECONDS)
.readTimeout(30L, TimeUnit.SECONDS)
.writeTimeout(30L, TimeUnit.SECONDS)
.addInterceptor(loggingInterceptor)

cache?.let {
builder.cache(it)
}

if (Hotwire.config.debugLoggingEnabled) {
builder.addInterceptor(loggingInterceptor)
}

return builder.build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dev.hotwire.core.turbo.webview

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.Configuration
import android.os.Build
import android.util.AttributeSet
import android.webkit.WebView
Expand Down
1 change: 0 additions & 1 deletion demo/src/main/kotlin/dev/hotwire/demo/DemoApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ class DemoApplication : Application() {
)

// Set configuration options
Hotwire.config.debugLoggingEnabled = BuildConfig.DEBUG
Hotwire.config.webViewDebuggingEnabled = BuildConfig.DEBUG
Hotwire.config.jsonConverter = KotlinXJsonConverter()
Hotwire.config.applicationUserAgentPrefix = "Hotwire Demo;"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import dev.hotwire.navigation.fragments.HotwireWebBottomSheetFragment
import dev.hotwire.navigation.fragments.HotwireWebFragment
import dev.hotwire.navigation.routing.AppNavigationRouteDecisionHandler
import dev.hotwire.navigation.routing.BrowserTabRouteDecisionHandler
import dev.hotwire.navigation.routing.SystemNavigationRouteDecisionHandler
import dev.hotwire.navigation.routing.Router
import dev.hotwire.navigation.routing.SystemNavigationRouteDecisionHandler
import kotlin.reflect.KClass

internal object HotwireNavigation {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
package dev.hotwire.navigation.destinations

import android.content.Intent
import android.os.Bundle
import androidx.activity.result.ActivityResultLauncher
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import androidx.navigation.NavOptions
import androidx.navigation.navOptions
import dev.hotwire.core.bridge.BridgeDestination
import dev.hotwire.core.config.Hotwire
import dev.hotwire.core.turbo.config.PathConfigurationProperties
import dev.hotwire.core.turbo.config.context
import dev.hotwire.core.turbo.config.presentation
import dev.hotwire.core.turbo.nav.Presentation
import dev.hotwire.core.turbo.nav.PresentationContext
import dev.hotwire.core.turbo.visit.VisitAction
import dev.hotwire.navigation.R
import dev.hotwire.navigation.activities.HotwireActivity
import dev.hotwire.navigation.config.HotwireNavigation
import dev.hotwire.navigation.fragments.HotwireFragmentDelegate
import dev.hotwire.navigation.fragments.HotwireFragmentViewModel
import dev.hotwire.navigation.navigator.Navigator
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.hotwire.navigation.fragments

import dev.hotwire.navigation.logging.logEvent
import dev.hotwire.navigation.destinations.HotwireDestination
import dev.hotwire.navigation.logging.logEvent
import dev.hotwire.navigation.navigator.navigatorName
import dev.hotwire.navigation.session.SessionModalResult
import dev.hotwire.navigation.session.SessionViewModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package dev.hotwire.navigation.fragments
import android.view.View
import android.webkit.HttpAuthHandler
import dev.hotwire.core.turbo.errors.VisitError
import dev.hotwire.navigation.views.HotwireView
import dev.hotwire.core.turbo.webview.HotwireWebChromeClient
import dev.hotwire.core.turbo.webview.HotwireWebView
import dev.hotwire.navigation.views.HotwireView

/**
* Callback interface to be implemented by a [HotwireWebFragment],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,25 @@
package dev.hotwire.navigation.logging

import android.util.Log
import dev.hotwire.core.config.Hotwire

internal object NavigationLog {
private const val DEFAULT_TAG = "Hotwire-Navigation"

private val debugEnabled get() = Hotwire.config.debugLoggingEnabled

internal fun d(msg: String) = log(Log.DEBUG, msg)

internal fun w(msg: String) = log(Log.WARN, msg)

internal fun e(msg: String) = log(Log.ERROR, msg)

private fun log(logLevel: Int, msg: String) {
when (logLevel) {
Log.DEBUG -> if (debugEnabled) Log.d(DEFAULT_TAG, msg)
Log.WARN -> Log.w(DEFAULT_TAG, msg)
Log.ERROR -> Log.e(DEFAULT_TAG, msg)
}
}
}

private const val DEFAULT_TAG = "Hotwire-Navigation"
private const val PAD_END_LENGTH = 35

internal fun logEvent(event: String, details: String = "") {
NavigationLog.d("$event ".padEnd(PAD_END_LENGTH, '.') + " [$details]")
Hotwire.config.logger.d(DEFAULT_TAG, "$event ".padEnd(PAD_END_LENGTH, '.') + " [$details]")
}

internal fun logEvent(event: String, attributes: List<Pair<String, Any>>) {
val description = attributes.joinToString(prefix = "[", postfix = "]", separator = ", ") {
"${it.first}: ${it.second}"
}
NavigationLog.d("$event ".padEnd(PAD_END_LENGTH, '.') + " $description")
Hotwire.config.logger.d(DEFAULT_TAG, "$event ".padEnd(PAD_END_LENGTH, '.') + " $description")
}

internal fun logWarning(event: String, details: String) {
NavigationLog.w("$event ".padEnd(PAD_END_LENGTH, '.') + " [$details]")
Hotwire.config.logger.w(DEFAULT_TAG, "$event ".padEnd(PAD_END_LENGTH, '.') + " [$details]")
}

internal fun logError(event: String, error: Exception) {
NavigationLog.e("$event: ${error.stackTraceToString()}")
internal fun logError(event: String, throwable: Throwable) {
Hotwire.config.logger.e(DEFAULT_TAG, "$event: ${throwable.stackTraceToString()}", throwable)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.annotation.SuppressLint
import android.os.Bundle
import dev.hotwire.core.turbo.nav.PresentationContext
import dev.hotwire.navigation.logging.logError
import kotlin.text.uppercase

internal const val ARG_LOCATION = "location"
internal const val ARG_NAVIGATOR_NAME = "navigator-name"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dev.hotwire.navigation.routing
import dev.hotwire.navigation.activities.HotwireActivity
import dev.hotwire.navigation.logging.logEvent
import dev.hotwire.navigation.navigator.NavigatorConfiguration
import dev.hotwire.navigation.routing.Router.RouteDecisionHandler

/**
* Routes location urls within in-app navigation or with custom behaviors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import android.view.View
import dev.hotwire.navigation.logging.logError
import dev.hotwire.navigation.logging.logEvent
import dev.hotwire.navigation.views.HotwireView
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import android.view.ViewGroup
import android.webkit.WebView
import android.widget.FrameLayout
import android.widget.ImageView
import androidx.core.view.*
import androidx.core.view.ScrollingView
import androidx.core.view.contains
import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import dev.hotwire.navigation.R

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package dev.hotwire.navigation.navigator

import android.R.attr.host
import android.content.Intent
import android.os.Bundle
import androidx.core.os.bundleOf
import dev.hotwire.navigation.activities.HotwireActivity
import org.assertj.core.api.Assertions.assertThat
Expand Down