Skip to content

Commit

Permalink
feature(exitInfo) add log messages to metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
YYChen01988 committed Sep 20, 2023
1 parent fa51e42 commit 55ffb09
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 36 deletions.
7 changes: 6 additions & 1 deletion bugsnag-plugin-android-exitinfo/detekt-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
<SmellBaseline>
<ManuallySuppressedIssues/>
<CurrentIssues>
<ID>LongParameterList:TombstoneParser.kt$TombstoneParser$( exitInfo: ApplicationExitInfo, listOpenFds: Boolean, includeLogcat: Boolean, threadConsumer: (BugsnagThread) -> Unit, fileDescriptorConsumer: (Int, String, String) -> Unit, logcatConsumer: (String) -> Unit )</ID>
<ID>MagicNumber:TombstoneParser.kt$TombstoneParser$3</ID>
<ID>MagicNumber:TombstoneParser.kt$TombstoneParser$4</ID>
<ID>MagicNumber:TombstoneParser.kt$TombstoneParser$5</ID>
<ID>MagicNumber:TombstoneParser.kt$TombstoneParser$6</ID>
<ID>MagicNumber:TombstoneParser.kt$TombstoneParser$7</ID>
<ID>MagicNumber:TraceParser.kt$TraceParser$16</ID>
<ID>MagicNumber:TraceParser.kt$TraceParser$3</ID>
<ID>MaxLineLength:TraceParserInvalidStackframesTest.kt$TraceParserInvalidStackframesTest.Companion$"#01 pc 0000000000000c5c /data/app/~~sKQbJGqVJA5glcnvEjZCMg==/com.example.bugsnag.android-fVuoJh5GpAL7sRAeI3vjSw==/lib/arm64/libentrypoint.so "</ID>
Expand All @@ -24,6 +30,5 @@
<ID>NestedBlockDepth:TraceParser.kt$TraceParser$private fun parseThreadAttributes(line: String)</ID>
<ID>ReturnCount:TraceParser.kt$TraceParser$@VisibleForTesting internal fun parseNativeFrame(line: String): Stackframe?</ID>
<ID>SwallowedException:ExitInfoCallback.kt$ExitInfoCallback$exc: Throwable</ID>
<ID>UnusedPrivateProperty:BugsnagExitInfoPlugin.kt$BugsnagExitInfoPlugin$/** * Whether to report stored logcat messages metadata */ private val includeLogcat: Boolean = false</ID>
</CurrentIssues>
</SmellBaseline>
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class BugsnagExitInfoPlugin @JvmOverloads constructor(
/**
* Whether to report stored logcat messages metadata
*/
private val includeLogcat: Boolean = false,
private val includeLogcat: Boolean = true,

/**
* Turn of event correlation based on the processStateSummary field, this can
Expand All @@ -42,7 +42,7 @@ class BugsnagExitInfoPlugin @JvmOverloads constructor(

exitInfoCallback = ExitInfoCallback(
client.appContext,
TombstoneEventEnhancer(client.logger, listOpenFds),
TombstoneEventEnhancer(client.logger, listOpenFds, includeLogcat),
TraceEventEnhancer(client.logger, client.immutableConfig.projectPackages)
)
client.addOnSend(exitInfoCallback)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import com.bugsnag.android.Thread as BugsnagThread

internal class TombstoneEventEnhancer(
private val logger: Logger,
private val listOpenFds: Boolean
private val listOpenFds: Boolean,
private val includeLogcat: Boolean
) : (Event, ApplicationExitInfo) -> Unit {
@RequiresApi(Build.VERSION_CODES.R)
override fun invoke(event: Event, exitInfo: ApplicationExitInfo) {
try {
TombstoneParser(logger).parse(
exitInfo,
listOpenFds,
includeLogcat,
threadConsumer = { thread ->
mergeThreadIntoEvent(
thread,
Expand All @@ -27,6 +29,9 @@ internal class TombstoneEventEnhancer(
"owner" to owner
) else mapOf("path" to path)
event.addMetadata("Open FileDescriptors", fd.toString(), fdInfo)
},
{ log ->
event.addMetadata("Log Messages", "Log Messages", log)
}
)
} catch (ex: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ internal class TombstoneParser(
fun parse(
exitInfo: ApplicationExitInfo,
listOpenFds: Boolean,
includeLogcat: Boolean,
threadConsumer: (BugsnagThread) -> Unit,
fileDescriptorConsumer: (Int, String, String) -> Unit
fileDescriptorConsumer: (Int, String, String) -> Unit,
logcatConsumer: (String) -> Unit
) {
try {
val trace: Tombstone = exitInfo.traceInputStream?.use {
Expand All @@ -28,11 +30,42 @@ internal class TombstoneParser(
if (listOpenFds) {
extractTombstoneFd(trace.openFdsList, fileDescriptorConsumer)
}

if (includeLogcat) {
extractTombstoneLogBuffers(trace.logBuffersList, logcatConsumer)
}
} catch (ex: Throwable) {
logger.w("Tombstone input stream threw an Exception", ex)
}
}

private fun extractTombstoneLogBuffers(
logBuffersList: List<TombstoneProtos.LogBuffer>,
logcatConsumer: (String) -> Unit
) {
val newLogList = mutableListOf<String>()
var priorityType: String
logBuffersList.forEach { logs ->
logs.logsList.forEach {
priorityType = when (it.priority) {
2 -> "VERBOSE"
3 -> "DEBUG"
4 -> "INFO"
5 -> "WARN"
6 -> "ERROR"
7 -> "ASSERT"
else -> it.priority.toString()
}

newLogList.add(
0,
"\n${it.timestamp} ${it.tid} ${it.tag} $priorityType ${it.message}"
)
}
}
logcatConsumer(newLogList.toString())
}

private fun extractTombstoneFd(
fdsList: List<TombstoneProtos.FD>,
fDConsumer: (Int, String, String) -> Unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ internal class TombstoneEventEnhancerTest {

private val logger = mock(Logger::class.java)

private val tombstoneEventEnhancer = TombstoneEventEnhancer(logger, true)
private val tombstoneEventEnhancer = TombstoneEventEnhancer(
logger = logger,
listOpenFds = true,
includeLogcat = true
)

@Test
fun testTombstoneEnhancer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,27 @@ internal class TombstoneParserTest {
`when`(exitInfo.traceInputStream).thenReturn(file)
val threads = mutableListOf<Thread>()
val fileDescriptors = ArrayList<Map<String, Any>>()
TombstoneParser(logger).parse(exitInfo, true, { thread ->
threads.add(thread)
}, { fd, path, owner ->
fileDescriptors.add(
mapOf(
"fd" to fd,
"path" to path,
"owner" to owner,
val logMessage = mutableListOf<String>()
TombstoneParser(logger).parse(
exitInfo = exitInfo,
listOpenFds = true,
includeLogcat = true,
threadConsumer = { thread ->
threads.add(thread)
},
fileDescriptorConsumer = { fd, path, owner ->
fileDescriptors.add(
mapOf(
"fd" to fd,
"path" to path,
"owner" to owner,
)
)
)
})
},
logcatConsumer = { logs ->
logMessage.add(0, logs)
}
)

assertEquals("30640", threads.first().id)
assertEquals("30639", threads.last().id)
Expand All @@ -60,17 +70,27 @@ internal class TombstoneParserTest {
`when`(exitInfo.traceInputStream).thenReturn(null)
val threads = mutableListOf<Thread>()
val fileDescriptors = ArrayList<Map<String, Any>>()
TombstoneParser(logger).parse(exitInfo, true, { thread ->
threads.add(thread)
}, { fd, path, owner ->
fileDescriptors.add(
mapOf(
"fd" to fd,
"path" to path,
"owner" to owner,
val logMessage = mutableListOf<String>()
TombstoneParser(logger).parse(
exitInfo = exitInfo,
listOpenFds = true,
includeLogcat = true,
threadConsumer = { thread ->
threads.add(thread)
},
fileDescriptorConsumer = { fd, path, owner ->
fileDescriptors.add(
mapOf(
"fd" to fd,
"path" to path,
"owner" to owner,
)
)
)
})
},
logcatConsumer = { logs ->
logMessage.add(0, logs)
}
)
verify(exitInfo, times(1)).traceInputStream
assertEquals(0, threads.size)
assertEquals(0, fileDescriptors.size)
Expand All @@ -82,17 +102,27 @@ internal class TombstoneParserTest {
`when`(exitInfo.traceInputStream).thenReturn(junkData.inputStream())
val threads = mutableListOf<Thread>()
val fileDescriptors = ArrayList<Map<String, Any>>()
TombstoneParser(logger).parse(exitInfo, true, { thread ->
threads.add(thread)
}, { fd, path, owner ->
fileDescriptors.add(
mapOf(
"fd" to fd,
"path" to path,
"owner" to owner,
val logMessage = mutableListOf<String>()
TombstoneParser(logger).parse(
exitInfo = exitInfo,
listOpenFds = true,
includeLogcat = true,
threadConsumer = { thread ->
threads.add(thread)
},
fileDescriptorConsumer = { fd, path, owner ->
fileDescriptors.add(
mapOf(
"fd" to fd,
"path" to path,
"owner" to owner,
)
)
)
})
},
logcatConsumer = { logs ->
logMessage.add(0, logs)
}
)
verify(exitInfo, times(1)).traceInputStream
assertEquals(0, threads.size)
assertEquals(0, fileDescriptors.size)
Expand Down

0 comments on commit 55ffb09

Please sign in to comment.