Skip to content

Commit

Permalink
Go back to just making FirebaseDataConnect.logLevel a MutableStateFlow
Browse files Browse the repository at this point in the history
  • Loading branch information
dconeybe committed Dec 11, 2024
1 parent 15dd7d8 commit a253cd2
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 465 deletions.
7 changes: 3 additions & 4 deletions firebase-dataconnect/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# Unreleased
* [feature] `FirebaseDataConnect.logging` added, to provide more advanced
control and access to the logcat logging facilities, such as a `Flow` to
collect the log level changes, `push()` to set and restore the log level,
and `withLevel()` to run a block with a specific log level.
* [changed] `FirebaseDataConnect.logLevel` type changed from `LogLevel` to
`MutableStateFlow<LogLevel>`. This enables apps to "collect" the flow to,
for example, update a UI component when the log level changes.
([#6586](https://github.com/firebase/firebase-android-sdk/pull/6586))

# 16.0.0-beta03
Expand Down
26 changes: 1 addition & 25 deletions firebase-dataconnect/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,6 @@ package com.google.firebase.dataconnect {
ctor public DataConnectException(@NonNull String message, @Nullable Throwable cause = null);
}

public interface DataConnectLogging {
method @NonNull public kotlinx.coroutines.flow.Flow<com.google.firebase.dataconnect.LogLevel> getFlow();
method @NonNull public com.google.firebase.dataconnect.LogLevel getLevel();
method @NonNull public com.google.firebase.dataconnect.DataConnectLogging.LogLevelStackFrame push(@NonNull com.google.firebase.dataconnect.LogLevel level);
method public void setLevel(@NonNull com.google.firebase.dataconnect.LogLevel);
property @NonNull public abstract kotlinx.coroutines.flow.Flow<com.google.firebase.dataconnect.LogLevel> flow;
property @NonNull public abstract com.google.firebase.dataconnect.LogLevel level;
}

public static interface DataConnectLogging.LogLevelStackFrame extends java.lang.AutoCloseable {
method public void close();
method @NonNull public com.google.firebase.dataconnect.LogLevel getNewLevel();
method @NonNull public com.google.firebase.dataconnect.LogLevel getOriginalLevel();
method @Nullable public suspend Object suspendingClose(@NonNull kotlin.coroutines.Continuation<? super kotlin.Unit>);
property @NonNull public abstract com.google.firebase.dataconnect.LogLevel newLevel;
property @NonNull public abstract com.google.firebase.dataconnect.LogLevel originalLevel;
}

public final class DataConnectLoggingKt {
method public static inline <T> T withLevel(@NonNull com.google.firebase.dataconnect.DataConnectLogging, @NonNull com.google.firebase.dataconnect.LogLevel level, @NonNull kotlin.jvm.functions.Function0<? extends T> block);
}

public final class DataConnectSettings {
ctor public DataConnectSettings(@NonNull String host = "firebasedataconnect.googleapis.com", boolean sslEnabled = true);
method @NonNull public String getHost();
Expand Down Expand Up @@ -134,9 +112,7 @@ package com.google.firebase.dataconnect {
public final class FirebaseDataConnectKt {
method @NonNull public static com.google.firebase.dataconnect.FirebaseDataConnect getInstance(@NonNull com.google.firebase.dataconnect.FirebaseDataConnect.Companion, @NonNull com.google.firebase.FirebaseApp app, @NonNull com.google.firebase.dataconnect.ConnectorConfig config, @NonNull com.google.firebase.dataconnect.DataConnectSettings settings = com.google.firebase.dataconnect.DataConnectSettings());
method @NonNull public static com.google.firebase.dataconnect.FirebaseDataConnect getInstance(@NonNull com.google.firebase.dataconnect.FirebaseDataConnect.Companion, @NonNull com.google.firebase.dataconnect.ConnectorConfig config, @NonNull com.google.firebase.dataconnect.DataConnectSettings settings = com.google.firebase.dataconnect.DataConnectSettings());
method @Deprecated @NonNull public static com.google.firebase.dataconnect.LogLevel getLogLevel(@NonNull com.google.firebase.dataconnect.FirebaseDataConnect.Companion);
method @NonNull public static com.google.firebase.dataconnect.DataConnectLogging getLogging(@NonNull com.google.firebase.dataconnect.FirebaseDataConnect.Companion);
method @Deprecated public static void setLogLevel(@NonNull com.google.firebase.dataconnect.FirebaseDataConnect.Companion, @NonNull com.google.firebase.dataconnect.LogLevel);
method @NonNull public static kotlinx.coroutines.flow.MutableStateFlow<com.google.firebase.dataconnect.LogLevel> getLogLevel(@NonNull com.google.firebase.dataconnect.FirebaseDataConnect.Companion);
}

@kotlinx.serialization.Serializable(with=LocalDateSerializer::class) public final class LocalDate {
Expand Down
1 change: 0 additions & 1 deletion firebase-dataconnect/firebase-dataconnect.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ dependencies {
testImplementation(libs.mockk)
testImplementation(libs.testonly.three.ten.abp)
testImplementation(libs.robolectric)
testImplementation(libs.turbine)

androidTestImplementation(project(":firebase-dataconnect:androidTestutil"))
androidTestImplementation(project(":firebase-dataconnect:connectors"))
Expand Down
2 changes: 1 addition & 1 deletion firebase-dataconnect/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version=16.0.0-beta99
version=16.0.0-beta04
latestReleasedVersion=16.0.0-beta03

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import android.annotation.SuppressLint
import com.google.firebase.Firebase
import com.google.firebase.FirebaseApp
import com.google.firebase.app
import com.google.firebase.dataconnect.core.DataConnectLoggingImpl
import com.google.firebase.dataconnect.core.FirebaseDataConnectFactory
import com.google.firebase.dataconnect.core.LoggerGlobals
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.SerializationStrategy
import kotlinx.serialization.modules.SerializersModule
Expand Down Expand Up @@ -61,8 +62,8 @@ import kotlinx.serialization.modules.SerializersModule
* Data Connect service) call [FirebaseDataConnect.useEmulator]. To create [QueryRef] and
* [MutationRef] instances for running queries and mutations, call [FirebaseDataConnect.query] and
* [FirebaseDataConnect.mutation], respectively. To enable debug logging, which is especially useful
* when reporting issues to Google, set [DataConnectLogging.level] in
* [FirebaseDataConnect.Companion.logging] to [LogLevel.DEBUG].
* when reporting issues to Google, set [FirebaseDataConnect.Companion.logLevel] to [LogLevel.DEBUG]
* .
*
* ### Integration with Kotlin Coroutines and Serialization
*
Expand Down Expand Up @@ -381,20 +382,13 @@ public fun FirebaseDataConnect.Companion.getInstance(
/**
* The log level used by all [FirebaseDataConnect] instances.
*
* As a [MutableStateFlow], the log level can be changed by assigning [MutableStateFlow.value].
* Also, the flow can be "collected" as a means of observing the log level, which may be useful in
* the case that a user interface shows a UI element, such as a checkbox, to represent whether debug
* logging is enabled.
*
* The default log level is [LogLevel.WARN]. Setting this to [LogLevel.DEBUG] will enable debug
* logging, which is especially useful when reporting issues to Google or investigating problems
* yourself. Setting it to [LogLevel.NONE] will disable all logging.
*/
@Deprecated(
message = "Will be removed when Data Connect graduates from beta",
replaceWith = ReplaceWith("logging.level", "com.google.firebase.dataconnect.logging")
)
public var FirebaseDataConnect.Companion.logLevel: LogLevel
get() = logging.level
set(level) {
logging.level = level
}

/** The logcat logging facilities used by all [FirebaseDataConnect] instances. */
public val FirebaseDataConnect.Companion.logging: DataConnectLogging<*>
get() = DataConnectLoggingImpl
public val FirebaseDataConnect.Companion.logLevel: MutableStateFlow<LogLevel> get() = LoggerGlobals.logLevel
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,11 @@ package com.google.firebase.dataconnect.core

import android.util.Log
import com.google.firebase.dataconnect.BuildConfig
import com.google.firebase.dataconnect.DataConnectLogging
import com.google.firebase.dataconnect.LogLevel
import com.google.firebase.dataconnect.core.LoggerGlobals.LOG_TAG
import com.google.firebase.dataconnect.core.LoggerGlobals.Logger
import com.google.firebase.util.nextAlphanumericString
import kotlin.random.Random
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asSharedFlow

internal object DataConnectLoggingImpl : DataConnectLogging<DataConnectLoggingImpl.StateImpl> {

private val _state = MutableStateFlow(StateImpl(level = LogLevel.WARN))

override val flow = _state.asSharedFlow()

override val state
get() = _state.value

override var level: LogLevel
get() = _state.value.level
set(newLevel) {
while (true) {
val oldState = _state.value
val newState = oldState.copy(level = newLevel)
if (_state.compareAndSet(oldState, newState)) {
break
}
}
}

data class StateImpl(override val level: LogLevel) : DataConnectLogging.State {
override fun restore() {
_state.value = this
}
}
}
import kotlin.random.Random

internal interface Logger {
val name: String
Expand Down Expand Up @@ -90,30 +59,30 @@ private class LoggerImpl(override val name: String) : Logger {
internal object LoggerGlobals {
const val LOG_TAG = "FirebaseDataConnect"

private val logLevel: LogLevel by DataConnectLoggingImpl::level
val logLevel = MutableStateFlow(LogLevel.WARN)

inline fun Logger.debug(message: () -> Any?) {
if (logLevel <= LogLevel.DEBUG) debug("${message()}")
if (logLevel.value <= LogLevel.DEBUG) debug("${message()}")
}

fun Logger.debug(message: String) {
if (logLevel <= LogLevel.DEBUG) log(null, LogLevel.DEBUG, message)
if (logLevel.value <= LogLevel.DEBUG) log(null, LogLevel.DEBUG, message)
}

inline fun Logger.warn(message: () -> Any?) {
if (logLevel <= LogLevel.WARN) warn("${message()}")
if (logLevel.value <= LogLevel.WARN) warn("${message()}")
}

inline fun Logger.warn(exception: Throwable?, message: () -> Any?) {
if (logLevel <= LogLevel.WARN) warn(exception, "${message()}")
if (logLevel.value <= LogLevel.WARN) warn(exception, "${message()}")
}

fun Logger.warn(message: String) {
warn(null, message)
}

fun Logger.warn(exception: Throwable?, message: String) {
if (logLevel <= LogLevel.WARN) log(exception, LogLevel.WARN, message)
if (logLevel.value <= LogLevel.WARN) log(exception, LogLevel.WARN, message)
}

fun Logger(name: String): Logger = LoggerImpl(name)
Expand Down
Loading

0 comments on commit a253cd2

Please sign in to comment.