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 28, 2023
1 parent 23c5062 commit 515043e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,3 @@ public final class com/bugsnag/android/BugsnagExitInfoPlugin : com/bugsnag/andro
public fun unload ()V
}

public final class com/bugsnag/android/TombstoneParserKt {
public static final field ASSERT I
public static final field DEBUG I
public static final field ERROR I
public static final field INFO I
public static final field VERBOSE I
public static final field WARN I
}

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 = true,
private val includeLogcat: Boolean = false,

/**
* Turn of event correlation based on the processStateSummary field, this can
Expand All @@ -40,8 +40,13 @@ class BugsnagExitInfoPlugin @JvmOverloads constructor(
)
}

val exitInfoPluginStore = ExitInfoPluginStore(client.immutableConfig)
val oldPid = exitInfoPluginStore.load()
exitInfoPluginStore.persist(android.os.Process.myPid())

exitInfoCallback = ExitInfoCallback(
client.appContext,
oldPid,
TombstoneEventEnhancer(client.logger, listOpenFds, includeLogcat),
TraceEventEnhancer(client.logger, client.immutableConfig.projectPackages)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.annotation.RequiresApi

internal class ExitInfoCallback(
private val context: Context,
private val pid: Int?,
private val nativeEnhancer: (Event, ApplicationExitInfo) -> Unit,
private val anrEventEnhancer: (Event, ApplicationExitInfo) -> Unit
) : OnSendCallback {
Expand All @@ -18,8 +19,10 @@ internal class ExitInfoCallback(
val allExitInfo = am.getHistoricalProcessExitReasons(context.packageName, 0, MAX_EXIT_INFO)
val sessionIdBytes = event.session?.id?.toByteArray() ?: return true
val exitInfo =
allExitInfo.find { it.processStateSummary?.contentEquals(sessionIdBytes) == true }
?: return true
allExitInfo.find {
it.processStateSummary?.contentEquals(sessionIdBytes) == true
}
?: allExitInfo.find { it.pid == pid } ?: return true

try {
if (exitInfo.reason == ApplicationExitInfo.REASON_CRASH_NATIVE ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@ import com.bugsnag.android.repackaged.server.os.TombstoneProtos
import com.bugsnag.android.repackaged.server.os.TombstoneProtos.Tombstone
import com.bugsnag.android.Thread as BugsnagThread

/**
* from android.util.Log
*/
const val VERBOSE = 2
const val DEBUG = 3
const val INFO = 4
const val WARN = 5
const val ERROR = 6
const val ASSERT = 7

internal class TombstoneParser(
private val logger: Logger
) {
Expand Down Expand Up @@ -56,23 +46,28 @@ internal class TombstoneParser(
val newLogList = StringBuilder()
logBuffersList.forEach { logs ->
logs.logsList.forEach {
val priorityType = when (it.priority) {
VERBOSE -> "VERBOSE"
DEBUG -> "DEBUG"
INFO -> "INFO"
WARN -> "WARN"
ERROR -> "ERROR"
ASSERT -> "ASSERT"
else -> it.priority.toString()
}
newLogList.append(
"\n${it.timestamp} ${it.tid} ${it.tag} $priorityType ${it.message}"
)
newLogList.append(it.timestamp).append(' ')
.append(it.tid).append(' ')
.append(it.tag).append(' ')
.append(priorityType(it)).append(' ')
.append(it.message)
.append('\n')
}
}
logcatConsumer(newLogList.toString())
}

private fun priorityType(it: TombstoneProtos.LogMessage): String =
when (it.priority) {
VERBOSE -> "VERBOSE"
DEBUG -> "DEBUG"
INFO -> "INFO"
WARN -> "WARN"
ERROR -> "ERROR"
ASSERT -> "ASSERT"
else -> it.priority.toString()
}

private fun extractTombstoneFd(
fdsList: List<TombstoneProtos.FD>,
fDConsumer: (Int, String, String) -> Unit
Expand All @@ -89,10 +84,10 @@ internal class TombstoneParser(
values.forEach { thread ->
val stacktrace = thread.currentBacktraceList.map { tombstoneTraceFrame ->
val stackFrame = Stackframe(
tombstoneTraceFrame.functionName,
tombstoneTraceFrame.fileName,
tombstoneTraceFrame.relPc,
null
method = tombstoneTraceFrame.functionName,
file = tombstoneTraceFrame.fileName,
lineNumber = tombstoneTraceFrame.relPc,
inProject = null
)
stackFrame.symbolAddress = tombstoneTraceFrame.functionOffset
stackFrame.loadAddress = tombstoneTraceFrame.fileMapOffset
Expand All @@ -112,4 +107,16 @@ internal class TombstoneParser(
threadConsumer(bugsnagThread)
}
}

/**
* from android.util.Log
*/
companion object {
private const val VERBOSE = 2
private const val DEBUG = 3
private const val INFO = 4
private const val WARN = 5
private const val ERROR = 6
private const val ASSERT = 7
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal class ExitInfoCallbackTest {

@Before
fun setUp() {
exitInfoCallback = ExitInfoCallback(context, nativeEnhancer, anrEventEnhancer)
exitInfoCallback = ExitInfoCallback(context, 100, nativeEnhancer, anrEventEnhancer)
exitInfos = listOf(exitInfo1)
`when`(context.getSystemService(any())).thenReturn(am)
`when`(am.getHistoricalProcessExitReasons(any(), anyInt(), anyInt()))
Expand Down

0 comments on commit 515043e

Please sign in to comment.