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

Unverified Commit b364fbd8 authored by Rafael Tonholo's avatar Rafael Tonholo
Browse files

feat(logging): add log level provider and manager

parent b71249d5
Loading
Loading
Loading
Loading
+10 −3
Original line number Diff line number Diff line
@@ -2,9 +2,11 @@ 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.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,11 +15,16 @@ 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<LogLevelManager> {
        DefaultLogLevelManager()
    }.bind<LogLevelProvider>()

    single<LogLevel> {
        if (BuildConfig.DEBUG) LogLevel.VERBOSE else LogLevel.INFO
        get<LogLevelProvider>().current()
    }

    single<List<LogSink>> {
+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
}
+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
}
+17 −0
Original line number Diff line number Diff line
package net.thunderbird.core.logging.testing

import net.thunderbird.core.logging.LogLevel
import net.thunderbird.core.logging.LogLevelManager

class TestLogLevelManager : LogLevelManager {
    var logLevel = LogLevel.VERBOSE
    override fun override(level: LogLevel) {
        logLevel = level
    }

    override fun restoreDefault() {
        logLevel = LogLevel.VERBOSE
    }

    override fun current(): LogLevel = logLevel
}
Loading