Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 76f7ab65 authored by Rafael Tonholo's avatar Rafael Tonholo Committed by GitHub
Browse files

Merge pull request #9634 from rafaeltonholo/fix/9633/enable-debug-logs-no-useful-data

fix(logs): debug logs ignored when user enable it
parents 040a5e08 aaba16e3
Loading
Loading
Loading
Loading
+17 −16
Original line number Diff line number Diff line
@@ -2,9 +2,13 @@ package net.thunderbird.app.common.core

import android.content.Context
import kotlin.time.ExperimentalTime
import net.thunderbird.app.common.BuildConfig
import net.thunderbird.app.common.core.logging.DefaultLogLevelManager
import net.thunderbird.core.common.inject.getList
import net.thunderbird.core.common.inject.singleListOf
import net.thunderbird.core.logging.DefaultLogger
import net.thunderbird.core.logging.LogLevel
import net.thunderbird.core.logging.LogLevelManager
import net.thunderbird.core.logging.LogLevelProvider
import net.thunderbird.core.logging.LogSink
import net.thunderbird.core.logging.Logger
import net.thunderbird.core.logging.composite.CompositeLogSink
@@ -13,25 +17,22 @@ import net.thunderbird.core.logging.file.AndroidFileSystemManager
import net.thunderbird.core.logging.file.FileLogSink
import org.koin.core.module.Module
import org.koin.core.qualifier.named
import org.koin.dsl.bind
import org.koin.dsl.module

val appCommonCoreModule: Module = module {
    single<LogLevel> {
        if (BuildConfig.DEBUG) LogLevel.VERBOSE else LogLevel.INFO
    }
    single<LogLevelManager> {
        DefaultLogLevelManager()
    }.bind<LogLevelProvider>()

    single<List<LogSink>> {
        listOf(
            ConsoleLogSink(
                level = get(),
            ),
    singleListOf<LogSink>(
        { ConsoleLogSink(level = LogLevel.VERBOSE) },
    )
    }

    single<CompositeLogSink> {
        CompositeLogSink(
            level = get(),
            sinks = get(),
            logLevelProvider = get(),
            sinks = getList(),
        )
    }

@@ -44,8 +45,8 @@ val appCommonCoreModule: Module = module {

    single<CompositeLogSink>(named(SYNC_DEBUG_LOG)) {
        CompositeLogSink(
            level = get(),
            sinks = get(),
            logLevelProvider = get(),
            sinks = getList(),
        )
    }

+22 −0
Original line number Diff line number Diff line
package net.thunderbird.app.common.core.logging

import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
import net.thunderbird.app.common.BuildConfig
import net.thunderbird.core.logging.LogLevel
import net.thunderbird.core.logging.LogLevelManager

class DefaultLogLevelManager : LogLevelManager {
    private val defaultLevel = if (BuildConfig.DEBUG) LogLevel.VERBOSE else LogLevel.INFO
    private val logLevel = MutableStateFlow(defaultLevel)

    override fun override(level: LogLevel) {
        logLevel.update { level }
    }

    override fun restoreDefault() {
        override(defaultLevel)
    }

    override fun current(): LogLevel = logLevel.value
}
+64 −0
Original line number Diff line number Diff line
package net.thunderbird.core.common.inject

import org.koin.core.definition.Definition
import org.koin.core.module.KoinDslMarker
import org.koin.core.module.Module
import org.koin.core.parameter.parametersOf
import org.koin.core.qualifier.Qualifier
import org.koin.core.qualifier.named
import org.koin.core.scope.Scope

// This file must be deleted once https://github.com/InsertKoinIO/koin/pull/1951 is merged to
// Koin and released on 4.2.0

/**
 * Defines a singleton list of elements of type [T].
 *
 * This function creates a singleton definition for a mutable list of elements.
 * Each element in the list is resolved from the provided [items] definitions.
 *
 * @param T The type of elements in the list.
 * @param items Vararg of [Definition]s that will be resolved and added to the list.
 * @param qualifier Optional [Qualifier] to distinguish this list from others of the same type.
 *                  If null, a default qualifier based on the type [T] will be used.
 */
@KoinDslMarker
inline fun <reified T> Module.singleListOf(vararg items: Definition<T>, qualifier: Qualifier? = null) {
    single(qualifier ?: defaultListQualifier<T>(), createdAtStart = true) {
        items.map { definition -> definition(this, parametersOf()) }
    }
}

/**
 * Resolves a [List] of instances of type [T].
 * This is a helper function for Koin's multibinding feature.
 *
 * It uses the [defaultListQualifier] if no [qualifier] is provided.
 *
 * @param T The type of instances in the list.
 * @param qualifier An optional [Qualifier] to distinguish between different lists of the same type.
 * @return The resolved [MutableList] of instances of type [T].
 */
inline fun <reified T> Scope.getList(qualifier: Qualifier? = null) =
    get<List<T>>(qualifier ?: defaultListQualifier<T>())

/**
 * Creates a qualifier for a set of a specific type.
 *
 * This is used to differentiate between different sets of the same type when injecting dependencies.
 *
 * @param T The type of the elements in the set.
 * @return A qualifier for the set.
 */
inline fun <reified T> defaultListQualifier() =
    defaultCollectionQualifier<List<T>, T>()

/**
 * Creates a default [Qualifier] for a collection binding.
 *
 * @param TCollection The type of the collection (e.g., `List`, `List`).
 * @param T The type of the elements in the collection.
 * @return A [Qualifier] that can be used to identify the specific collection binding.
 */
inline fun <reified TCollection : Collection<T>, reified T> defaultCollectionQualifier() =
    named("${TCollection::class.qualifiedName}<${T::class.qualifiedName}>")
+28 −0
Original line number Diff line number Diff line
package net.thunderbird.core.logging

/**
 * Manages the log level for the application.
 *
 * This interface provides a way to update the log level dynamically.
 * Implementations of this interface are responsible for persisting the log level
 * and notifying listeners of changes.
 */
interface LogLevelManager : LogLevelProvider {
    /**
     * Overrides the current log level.
     *
     * This function allows for a temporary change in the log level,
     * typically for debugging or specific operational needs.
     * The original log level can be restored by calling [restoreDefault] function
     *
     * @param level The new log level to set
     */
    fun override(level: LogLevel)

    /**
     * Restores the log level to its default value.
     *
     * The default log level is defined by the specific implementation of this interface.
     */
    fun restoreDefault()
}
+15 −0
Original line number Diff line number Diff line
package net.thunderbird.core.logging

/**
 * Provides the current [LogLevel].
 *
 * This can be used to dynamically change the log level during runtime.
 */
fun interface LogLevelProvider {
    /**
     * Gets the current log level.
     *
     * @return The current log level.
     */
    fun current(): LogLevel
}
Loading