Skip to content

Commit

Permalink
refactor: remove dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench committed May 9, 2024
1 parent e67ea60 commit 79e2ce0
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1193,7 +1193,7 @@ private boolean checkSdkStartedAndLogPublicApiUsage(@NonNull String action) {
private boolean checkSdkStarted(@NonNull String action, boolean logPublicApiUsage) {
boolean isStarted = isStarted();
if (!isStarted) {
internalEmbraceLogger.logSDKNotInitialized(action);
internalEmbraceLogger.logSdkNotInitialized(action);

Check warning on line 1196 in embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/EmbraceImpl.java

View check run for this annotation

Codecov / codecov/patch

embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/EmbraceImpl.java#L1196

Added line #L1196 was not covered by tests
}
if (telemetryService != null && logPublicApiUsage) {
telemetryService.onPublicApiCalled(action);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal class FlutterInternalInterfaceImpl(
hostedSdkVersionInfo.hostedSdkVersion = version
}
} else {
logger.logSDKNotInitialized("setEmbraceFlutterSdkVersion")
logger.logSdkNotInitialized("setEmbraceFlutterSdkVersion")
}
}

Expand All @@ -27,7 +27,7 @@ internal class FlutterInternalInterfaceImpl(
hostedSdkVersionInfo.hostedPlatformVersion = version
}
} else {
logger.logSDKNotInitialized("setDartVersion")
logger.logSdkNotInitialized("setDartVersion")
}
}

Expand Down Expand Up @@ -73,7 +73,7 @@ internal class FlutterInternalInterfaceImpl(
message
)
} else {
logger.logSDKNotInitialized("logDartError")
logger.logSdkNotInitialized("logDartError")

Check warning on line 76 in embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/FlutterInternalInterfaceImpl.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/FlutterInternalInterfaceImpl.kt#L76

Added line #L76 was not covered by tests
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal class ReactNativeInternalInterfaceImpl(
val exception = JsException(name, message, type, stacktrace)
crashService.logUnhandledJsException(exception)
} else {
logger.logSDKNotInitialized("log JS exception")
logger.logSdkNotInitialized("log JS exception")
}
}

Expand All @@ -51,7 +51,7 @@ internal class ReactNativeInternalInterfaceImpl(
null
)
} else {
logger.logSDKNotInitialized("log JS exception")
logger.logSdkNotInitialized("log JS exception")

Check warning on line 54 in embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/ReactNativeInternalInterfaceImpl.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/ReactNativeInternalInterfaceImpl.kt#L54

Added line #L54 was not covered by tests
}
}

Expand All @@ -67,15 +67,15 @@ internal class ReactNativeInternalInterfaceImpl(
}
hostedSdkVersionInfo.javaScriptPatchNumber = number
} else {
logger.logSDKNotInitialized("set JavaScript patch number")
logger.logSdkNotInitialized("set JavaScript patch number")
}
}

override fun setReactNativeSdkVersion(version: String?) {
if (embrace.isStarted) {
hostedSdkVersionInfo.hostedSdkVersion = version
} else {
logger.logSDKNotInitialized("set React Native SDK version")
logger.logSdkNotInitialized("set React Native SDK version")

Check warning on line 78 in embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/ReactNativeInternalInterfaceImpl.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/ReactNativeInternalInterfaceImpl.kt#L78

Added line #L78 was not covered by tests
}
}

Expand All @@ -91,7 +91,7 @@ internal class ReactNativeInternalInterfaceImpl(
}
hostedSdkVersionInfo.hostedPlatformVersion = version
} else {
logger.logSDKNotInitialized("set React Native version number")
logger.logSdkNotInitialized("set React Native version number")
}
}

Expand All @@ -114,7 +114,7 @@ internal class ReactNativeInternalInterfaceImpl(
}
metadataService.setReactNativeBundleId(context, url, didUpdate)
} else {
logger.logSDKNotInitialized("set JavaScript bundle URL")
logger.logSdkNotInitialized("set JavaScript bundle URL")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal class UnityInternalInterfaceImpl(
hostedSdkVersionInfo.unityBuildIdNumber = buildGuid
}
} else {
logger.logSDKNotInitialized("set Unity metadata")
logger.logSdkNotInitialized("set Unity metadata")
}
}

Expand Down Expand Up @@ -67,7 +67,7 @@ internal class UnityInternalInterfaceImpl(
message
)
} else {
logger.logSDKNotInitialized("log Unity exception")
logger.logSdkNotInitialized("log Unity exception")

Check warning on line 70 in embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/UnityInternalInterfaceImpl.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/UnityInternalInterfaceImpl.kt#L70

Added line #L70 was not covered by tests
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ internal class AeiDataSourceImpl(
try {
processApplicationExitInfo()
} catch (exc: Throwable) {
logger.logWarningWithException(
logger.logWarning(
"AEI - Failed to process AEIs due to unexpected error",
exc,
true
Expand All @@ -77,7 +77,7 @@ internal class AeiDataSourceImpl(
backgroundExecution?.cancel(true)
backgroundExecution = null
} catch (t: Throwable) {
logger.logWarningWithException(
logger.logWarning(

Check warning on line 80 in embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/capture/aei/AeiDataSourceImpl.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/capture/aei/AeiDataSourceImpl.kt#L80

Added line #L80 was not covered by tests
"AEI - Failed to disable EmbraceApplicationExitInfoService work",
t
)
Expand Down Expand Up @@ -124,7 +124,6 @@ internal class AeiDataSourceImpl(
?: return emptyList()

if (historicalProcessExitReasons.size > SDK_AEI_SEND_LIMIT) {
logger.logInfoWithException("AEI - size greater than $SDK_AEI_SEND_LIMIT")
historicalProcessExitReasons = historicalProcessExitReasons.take(SDK_AEI_SEND_LIMIT)
}

Expand Down Expand Up @@ -227,19 +226,18 @@ internal class AeiDataSourceImpl(

val traceMaxLimit = appExitInfoBehavior.getTraceMaxLimit()
if (trace.length > traceMaxLimit) {
logger.logInfoWithException("AEI - Blob size was reduced. Current size is ${trace.length} and the limit is $traceMaxLimit")
return AppExitInfoBehavior.CollectTracesResult.TooLarge(trace.take(traceMaxLimit))
}

return AppExitInfoBehavior.CollectTracesResult.Success(trace)
} catch (e: IOException) {
logger.logWarningWithException("AEI - IOException: ${e.message}", e, true)
logger.logWarning("AEI - IOException: ${e.message}", e, true)
return AppExitInfoBehavior.CollectTracesResult.TraceException(("ioexception: ${e.message}"))
} catch (e: OutOfMemoryError) {
logger.logWarningWithException("AEI - Out of Memory: ${e.message}", e, true)
logger.logWarning("AEI - Out of Memory: ${e.message}", e, true)
return AppExitInfoBehavior.CollectTracesResult.TraceException(("oom: ${e.message}"))
} catch (tr: Throwable) {
logger.logWarningWithException("AEI - An error occurred: ${tr.message}", tr, true)
logger.logWarning("AEI - An error occurred: ${tr.message}", tr, true)
return AppExitInfoBehavior.CollectTracesResult.TraceException(("error: ${tr.message}"))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import java.util.concurrent.CopyOnWriteArrayList
@Suppress("NOTHING_TO_INLINE")
internal class InternalEmbraceLogger {
private val logActions = CopyOnWriteArrayList<LogAction>(listOf(LogcatAction()))
private var threshold = Severity.INFO

internal fun interface LogAction {
fun log(msg: String, severity: Severity, throwable: Throwable?, logStacktrace: Boolean)
Expand Down Expand Up @@ -43,24 +42,9 @@ internal class InternalEmbraceLogger {
log(msg, Severity.ERROR, throwable, logStacktrace)
}

// Log with INFO severity that always contains a throwable as an internal exception to be sent to Grafana
inline fun logInfoWithException(msg: String, throwable: Throwable? = null, logStacktrace: Boolean = false) {
log(msg, Severity.INFO, throwable ?: InternalErrorServiceAction.NotAnException(msg), logStacktrace)
}

// Log with WARNING severity that always contains a throwable as an internal exception to be sent to Grafana
inline fun logWarningWithException(msg: String, throwable: Throwable? = null, logStacktrace: Boolean = false) {
log(msg, Severity.WARNING, throwable ?: InternalErrorServiceAction.NotAnException(msg), logStacktrace)
}

fun logSDKNotInitialized(action: String) {
fun logSdkNotInitialized(action: String) {
val msg = "Embrace SDK is not initialized yet, cannot $action."
log(
msg,
Severity.WARNING,
Throwable(msg),
true
)
log(msg, Severity.WARNING, Throwable(msg), true)

Check warning on line 47 in embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/logging/InternalEmbraceLogger.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/logging/InternalEmbraceLogger.kt#L47

Added line #L47 was not covered by tests
}

/**
Expand All @@ -71,22 +55,15 @@ 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) {
if (shouldTriggerLoggerActions(severity)) {
if (severity >= Severity.INFO || ApkToolsConfig.IS_DEVELOPER_LOGGING_ENABLED) {
logActions.forEach {
it.log(msg, severity, throwable, logStacktrace)
}
}
}

fun setThreshold(severity: Severity) {
threshold = severity
}

private fun shouldTriggerLoggerActions(severity: Severity) = ApkToolsConfig.IS_DEVELOPER_LOGGING_ENABLED || severity >= threshold

enum class Severity {
DEBUG, INFO, WARNING, ERROR, NONE
DEBUG, INFO, WARNING, ERROR
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,4 @@ internal class InternalErrorServiceAction(
}

class InternalError(msg: String) : Exception(msg)
class NotAnException(msg: String) : Exception(msg)
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ internal class EmbraceNdkService(
""".trimIndent()
val exc = RuntimeException(errMsg)
exc.stackTrace = arrayOfNulls(0)
logger.logWarningWithException(errMsg, exc, false)
logger.logWarning(errMsg, exc, false)

Check warning on line 180 in embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/ndk/EmbraceNdkService.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/ndk/EmbraceNdkService.kt#L180

Added line #L180 was not covered by tests
delegate._reinstallSignalHandlers()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal class FlutterInternalInterfaceImplTest {
every { embrace.isStarted } returns false
impl.setEmbraceFlutterSdkVersion("2.12")
verify(exactly = 1) {
logger.logSDKNotInitialized(any())
logger.logSdkNotInitialized(any())
}
}

Expand All @@ -46,7 +46,7 @@ internal class FlutterInternalInterfaceImplTest {
every { embrace.isStarted } returns false
impl.setDartVersion("2.12")
verify(exactly = 1) {
logger.logSDKNotInitialized(any())
logger.logSdkNotInitialized(any())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal class ReactNativeInternalInterfaceImplTest {
every { embrace.isStarted } returns false
impl.setJavaScriptPatchNumber("28.9.1")
verify(exactly = 1) {
logger.logSDKNotInitialized(any())
logger.logSdkNotInitialized(any())
}
}

Expand Down Expand Up @@ -89,7 +89,7 @@ internal class ReactNativeInternalInterfaceImplTest {
every { embrace.isStarted } returns false
impl.setReactNativeVersionNumber("0.69.1")
verify(exactly = 1) {
logger.logSDKNotInitialized(any())
logger.logSdkNotInitialized(any())
}
}

Expand Down Expand Up @@ -127,7 +127,7 @@ internal class ReactNativeInternalInterfaceImplTest {
every { embrace.isStarted } returns false
impl.setJavaScriptBundleUrl(context, "index.android.bundle")
verify(exactly = 1) {
logger.logSDKNotInitialized(any())
logger.logSdkNotInitialized(any())
}
}

Expand Down Expand Up @@ -210,7 +210,7 @@ internal class ReactNativeInternalInterfaceImplTest {
every { embrace.isStarted } returns false
impl.logUnhandledJsException("name", "message", "type", "stack")
verify(exactly = 1) {
logger.logSDKNotInitialized(any())
logger.logSdkNotInitialized(any())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal class UnityInternalInterfaceImplTest {
every { embrace.isStarted } returns false
impl.setUnityMetaData("unityVersion", "buildGuid", "unitySdkVersion")
verify(exactly = 1) {
logger.logSDKNotInitialized(any())
logger.logSdkNotInitialized(any())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import io.embrace.android.embracesdk.fakes.FakeLogAction
import io.embrace.android.embracesdk.internal.ApkToolsConfig
import io.embrace.android.embracesdk.logging.InternalEmbraceLogger.Severity
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test

Expand All @@ -20,23 +19,8 @@ internal class InternalEmbraceLoggerTest {
ApkToolsConfig.IS_DEVELOPER_LOGGING_ENABLED = false
}

@Test
fun `a log with a severity that does not surpass the threshold does not trigger actions`() {
// given the threshold is .INFO
logger.setThreshold(Severity.INFO)

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

// then logger actions are not triggered
assertTrue(action.msgQueue.isEmpty())
}

@Test
fun `a log with the same severity as the threshold triggers logging actions`() {
// given the threshold is .INFO
logger.setThreshold(Severity.INFO)

// when log is called with the same severity
val throwable = Exception()
logger.log("test", Severity.INFO, throwable, false)
Expand All @@ -49,9 +33,6 @@ internal class InternalEmbraceLoggerTest {

@Test
fun `a log with a higher severity than the threshold triggers logging actions`() {
// given the threshold is .INFO
logger.setThreshold(Severity.INFO)

// when log is called with a higher severity
val throwable = Exception()
logger.log("test", Severity.WARNING, throwable, false)
Expand All @@ -65,7 +46,6 @@ internal class InternalEmbraceLoggerTest {
@Test
fun `a log with lower severity than the threshold triggers actions when developer logging is enabled`() {
// given the threshold is .INFO and developer logging is enabled
logger.setThreshold(Severity.INFO)
ApkToolsConfig.IS_DEVELOPER_LOGGING_ENABLED = true

// when log is called with a lower severity
Expand Down

0 comments on commit 79e2ce0

Please sign in to comment.