Skip to content

Commit

Permalink
Merge pull request #826 from embrace-io/inline-logcat-action
Browse files Browse the repository at this point in the history
Inline logcat action
  • Loading branch information
fractalwrench committed May 10, 2024
2 parents 79e2ce0 + 011a008 commit 2a8cb2d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import android.util.Log
import io.embrace.android.embracesdk.internal.ApkToolsConfig
import java.util.concurrent.CopyOnWriteArrayList

internal const val EMBRACE_TAG = "[Embrace]"

/**
* Wrapper for the Android [Log] utility.
* Can only be used internally, it's not part of the public API.
*/

// Suppressing "Nothing to inline". These functions are used all around the codebase, pretty often, so we want them to
// perform as fast as possible.
@Suppress("NOTHING_TO_INLINE")
internal class InternalEmbraceLogger {
private val logActions = CopyOnWriteArrayList<LogAction>(listOf(LogcatAction()))
private val logActions = CopyOnWriteArrayList<LogAction>(listOf())

internal fun interface LogAction {
fun log(msg: String, severity: Severity, throwable: Throwable?, logStacktrace: Boolean)
Expand All @@ -24,21 +25,21 @@ internal class InternalEmbraceLogger {
}

@JvmOverloads
inline fun logDebug(msg: String, throwable: Throwable? = null) {
fun logDebug(msg: String, throwable: Throwable? = null) {
log(msg, Severity.DEBUG, throwable, true)
}

inline fun logInfo(msg: String) {
fun logInfo(msg: String) {
log(msg, Severity.INFO, null, true)
}

@JvmOverloads
inline fun logWarning(msg: String, throwable: Throwable? = null, logStacktrace: Boolean = false) {
fun logWarning(msg: String, throwable: Throwable? = null, logStacktrace: Boolean = false) {
log(msg, Severity.WARNING, throwable, logStacktrace)
}

@JvmOverloads
inline fun logError(msg: String, throwable: Throwable? = null, logStacktrace: Boolean = false) {
fun logError(msg: String, throwable: Throwable? = null, logStacktrace: Boolean = false) {
log(msg, Severity.ERROR, throwable, logStacktrace)
}

Expand All @@ -55,14 +56,36 @@ internal class InternalEmbraceLogger {
* @param throwable exception, if any.
* @param logStacktrace should add the throwable to the logging
*/
fun log(msg: String, severity: Severity, throwable: Throwable?, logStacktrace: Boolean) {
@Suppress("NOTHING_TO_INLINE") // hot path - optimize by inlining
private inline fun log(msg: String, severity: Severity, throwable: Throwable?, logStacktrace: Boolean) {
if (severity >= Severity.INFO || ApkToolsConfig.IS_DEVELOPER_LOGGING_ENABLED) {
logcatImpl(throwable, logStacktrace, severity, msg)

logActions.forEach {
it.log(msg, severity, throwable, logStacktrace)
}
}
}

/**
* Logs a message to the Android logcat.
*/
@Suppress("NOTHING_TO_INLINE") // hot path - optimize by inlining
private inline fun logcatImpl(
throwable: Throwable?,
logStacktrace: Boolean,
severity: Severity,
msg: String
) {
val exception = throwable?.takeIf { logStacktrace }
when (severity) {
Severity.DEBUG -> Log.d(EMBRACE_TAG, msg, exception)
Severity.INFO -> Log.i(EMBRACE_TAG, msg, exception)
Severity.WARNING -> Log.w(EMBRACE_TAG, msg, exception)
Severity.ERROR -> Log.e(EMBRACE_TAG, msg, exception)
}
}

enum class Severity {
DEBUG, INFO, WARNING, ERROR
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,19 @@ internal class InternalEmbraceLoggerTest {
@Test
fun `a log with the same severity as the threshold triggers logging actions`() {
// when log is called with the same severity
val throwable = Exception()
logger.log("test", Severity.INFO, throwable, false)
logger.logInfo("test")

// then logger actions are triggered
val msg = action.msgQueue.single()
val expected = FakeLogAction.LogMessage("test", Severity.INFO, throwable, false)
val expected = FakeLogAction.LogMessage("test", Severity.INFO, null, true)
assertEquals(expected, msg)
}

@Test
fun `a log with a higher severity than the threshold triggers logging actions`() {
// when log is called with a higher severity
val throwable = Exception()
logger.log("test", Severity.WARNING, throwable, false)
logger.logWarning("test", throwable, false)

// then logger actions are triggered
val msg = action.msgQueue.single()
Expand All @@ -50,11 +49,11 @@ internal class InternalEmbraceLoggerTest {

// when log is called with a lower severity
val throwable = Exception()
logger.log("test", Severity.DEBUG, throwable, false)
logger.logDebug("test", throwable)

// then logger actions are triggered
val msg = action.msgQueue.single()
val expected = FakeLogAction.LogMessage("test", Severity.DEBUG, throwable, false)
val expected = FakeLogAction.LogMessage("test", Severity.DEBUG, throwable, true)
assertEquals(expected, msg)
}
}

0 comments on commit 2a8cb2d

Please sign in to comment.