Skip to content

Commit

Permalink
Extract common methods from LogServie and LogMessageService to a comm…
Browse files Browse the repository at this point in the history
…on BaseLogService interface.
  • Loading branch information
leandro-godon committed Mar 19, 2024
1 parent c2b4ace commit edf4662
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package io.embrace.android.embracesdk.event
import io.embrace.android.embracesdk.Embrace
import io.embrace.android.embracesdk.EventType
import io.embrace.android.embracesdk.LogExceptionType
import io.embrace.android.embracesdk.internal.logs.BaseLogService
import io.embrace.android.embracesdk.payload.NetworkCapturedCall
import io.embrace.android.embracesdk.session.MemoryCleanerListener

/**
* Logs messages remotely, so that they can be viewed as events during a user's session.
*/
internal interface LogMessageService : MemoryCleanerListener {
internal interface LogMessageService : BaseLogService {

/**
* Creates a network event.
Expand Down Expand Up @@ -45,33 +45,6 @@ internal interface LogMessageService : MemoryCleanerListener {
exceptionMessage: String?
)

/**
* Finds all IDs of log events at info level within the given time window.
*
* @param startTime the beginning of the time window
* @param endTime the end of the time window
* @return the list of log IDs within the specified range
*/
fun findInfoLogIds(startTime: Long, endTime: Long): List<String>

/**
* Finds all IDs of log events at warning level within the given time window.
*
* @param startTime the beginning of the time window
* @param endTime the end of the time window
* @return the list of log IDs within the specified range
*/
fun findWarningLogIds(startTime: Long, endTime: Long): List<String>

/**
* Finds all IDs of log events at error level within the given time window.
*
* @param startTime the beginning of the time window
* @param endTime the end of the time window
* @return the list of log IDs within the specified range
*/
fun findErrorLogIds(startTime: Long, endTime: Long): List<String>

/**
* Finds all IDs of log network events within the given time window.
*
Expand All @@ -80,21 +53,4 @@ internal interface LogMessageService : MemoryCleanerListener {
* @return the list of log IDs within the specified range
*/
fun findNetworkLogIds(startTime: Long, endTime: Long): List<String>

/**
* The total number of info logs that the app attempted to send.
*/
fun getInfoLogsAttemptedToSend(): Int

/**
* The total number of warning logs that the app attempted to send.
*/
fun getWarnLogsAttemptedToSend(): Int

/**
* The total number of error logs that the app attempted to send.
*/
fun getErrorLogsAttemptedToSend(): Int

fun getUnhandledExceptionsSent(): Int
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.embrace.android.embracesdk.internal.logs

import io.embrace.android.embracesdk.session.MemoryCleanerListener

internal interface BaseLogService : MemoryCleanerListener {
/**
* Finds all IDs of log events at info level within the given time window.
*
* @param startTime the beginning of the time window
* @param endTime the end of the time window
* @return the list of log IDs within the specified range
*/
fun findInfoLogIds(startTime: Long, endTime: Long): List<String>

/**
* Finds all IDs of log events at warning level within the given time window.
*
* @param startTime the beginning of the time window
* @param endTime the end of the time window
* @return the list of log IDs within the specified range
*/
fun findWarningLogIds(startTime: Long, endTime: Long): List<String>

/**
* Finds all IDs of log events at error level within the given time window.
*
* @param startTime the beginning of the time window
* @param endTime the end of the time window
* @return the list of log IDs within the specified range
*/
fun findErrorLogIds(startTime: Long, endTime: Long): List<String>

/**
* The total number of info logs that the app attempted to send.
*/
fun getInfoLogsAttemptedToSend(): Int

/**
* The total number of warning logs that the app attempted to send.
*/
fun getWarnLogsAttemptedToSend(): Int

/**
* The total number of error logs that the app attempted to send.
*/
fun getErrorLogsAttemptedToSend(): Int

/**
* The total number of unhandled exceptions sent.
*/
fun getUnhandledExceptionsSent(): Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ internal class EmbraceLogService(
)
)

private var unhandledExceptionsCount = 0

override fun log(
message: String,
severity: Severity,
Expand All @@ -71,6 +73,9 @@ internal class EmbraceLogService(
exceptionName: String?,
exceptionMessage: String?
) {
if (logExceptionType == LogExceptionType.UNHANDLED) {
unhandledExceptionsCount++
}
val attributes = EmbraceLogAttributes(properties)
attributes.setExceptionType(logExceptionType)
attributes.setAppFramework(framework)
Expand Down Expand Up @@ -106,6 +111,10 @@ internal class EmbraceLogService(
return logCounters.getValue(Severity.ERROR).findLogIds(startTime, endTime)
}

override fun getUnhandledExceptionsSent(): Int {
return unhandledExceptionsCount
}

override fun cleanCollections() {
logCounters.forEach { it.value.clear() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package io.embrace.android.embracesdk.internal.logs
import io.embrace.android.embracesdk.Embrace.AppFramework
import io.embrace.android.embracesdk.LogExceptionType
import io.embrace.android.embracesdk.Severity
import io.embrace.android.embracesdk.session.MemoryCleanerListener

/**
* Creates log records to be sent using the Open Telemetry Logs data model.
*/
internal interface LogService : MemoryCleanerListener {
internal interface LogService : BaseLogService {

/**
* Creates a log record.
Expand Down Expand Up @@ -51,46 +50,4 @@ internal interface LogService : MemoryCleanerListener {
exceptionName: String?,
exceptionMessage: String?
)

/**
* Finds all IDs of log events at info level within the given time window.
*
* @param startTime the beginning of the time window
* @param endTime the end of the time window
* @return the list of log IDs within the specified range
*/
fun findInfoLogIds(startTime: Long, endTime: Long): List<String>

/**
* Finds all IDs of log events at warning level within the given time window.
*
* @param startTime the beginning of the time window
* @param endTime the end of the time window
* @return the list of log IDs within the specified range
*/
fun findWarningLogIds(startTime: Long, endTime: Long): List<String>

/**
* Finds all IDs of log events at error level within the given time window.
*
* @param startTime the beginning of the time window
* @param endTime the end of the time window
* @return the list of log IDs within the specified range
*/
fun findErrorLogIds(startTime: Long, endTime: Long): List<String>

/**
* The total number of info logs that the app attempted to send.
*/
fun getInfoLogsAttemptedToSend(): Int

/**
* The total number of warning logs that the app attempted to send.
*/
fun getWarnLogsAttemptedToSend(): Int

/**
* The total number of error logs that the app attempted to send.
*/
fun getErrorLogsAttemptedToSend(): Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ internal class EmbraceLogServiceTest {
logService.logException(
"Hello world",
Severity.WARNING,
LogExceptionType.NONE,
LogExceptionType.HANDLED,
null,
exception.stackTrace,
null,
Expand All @@ -128,14 +128,47 @@ internal class EmbraceLogServiceTest {
)

val log = logWriter.logEvents.single()
assertEquals(0, logService.getUnhandledExceptionsSent())
assertEquals("Hello world", log.message)
assertEquals(Severity.WARNING, log.severity)
assertEquals("NullPointerException", log.attributes["emb.exception_name"])
assertEquals("exception message", log.attributes["emb.exception_message"])
assertEquals(AppFramework.NATIVE.value.toString(), log.attributes["emb.app_framework"])
assertNotNull(log.attributes["emb.log_id"])
assertEquals("session-123", log.attributes["emb.session_id"])
assertEquals("none", log.attributes["emb.exception_type"])
assertEquals(LogExceptionType.HANDLED.value, log.attributes["emb.exception_type"])
log.assertIsType(EmbType.System.Log)
}

@Test
fun testUnhandledExceptionLog() {
val logService = getLogService()
val exception = NullPointerException("exception message")

logService.logException(
"Hello world",
Severity.WARNING,
LogExceptionType.UNHANDLED,
null,
exception.stackTrace,
null,
AppFramework.UNITY,
null,
null,
exception.javaClass.simpleName,
exception.message,
)

val log = logWriter.logEvents.single()
assertEquals(1, logService.getUnhandledExceptionsSent())
assertEquals("Hello world", log.message)
assertEquals(Severity.WARNING, log.severity)
assertEquals("NullPointerException", log.attributes["emb.exception_name"])
assertEquals("exception message", log.attributes["emb.exception_message"])
assertEquals(AppFramework.UNITY.value.toString(), log.attributes["emb.app_framework"])
assertNotNull(log.attributes["emb.log_id"])
assertEquals("session-123", log.attributes["emb.session_id"])
assertEquals(LogExceptionType.UNHANDLED.value, log.attributes["emb.exception_type"])
log.assertIsType(EmbType.System.Log)
}

Expand Down

0 comments on commit edf4662

Please sign in to comment.