fix(logs): say in a recovered session folder whether the device restarted (#2752)

This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-11-17 13:35:04 -05:00
1 parent 28cf995d7b
commit 7909af74d6
2 files changed
+47

No files matched your search

@@ -544,6 +544,7 @@ object SessionArtifacts {
"session died of a crash, the entry is in there unless the device rebooted.\n" +
(if (newest) exitReasons(context) else "")
)
restartNote(File(dir, "device.txt").lastModified(), bootedAt())?.let { recoveryNote.appendText(it) }
if (newest && lastExitForMemory(context)) killedForMemory = true
}
if (recoveryPreviouslyAttempted && !runtimeCaptured.exists() &&
@@ -686,5 +687,21 @@ object SessionArtifacts {
return gone.size
}
private fun bootedAt(): Long = System.currentTimeMillis() - android.os.SystemClock.elapsedRealtime()
internal fun restartNote(sessionStartedAt: Long, bootedAt: Long): String? {
if (sessionStartedAt <= 0L) return null
val stamp = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US)
return if (bootedAt > sessionStartedAt + BOOT_CLOCK_SLACK_MS) {
"The device itself probably restarted after this session began (estimated boot ${stamp.format(Date(bootedAt))}, from the clock and uptime, so a clock change can fake it):\n" +
"if so, the whole system went down and crash.log is the new boot's buffer and says nothing about the session.\n"
} else {
"The device does not appear to have restarted since this session began (estimated boot ${stamp.format(Date(bootedAt))}, from the clock and uptime):\n" +
"only this app's processes ended, so crash.log and the exit records cover the end.\n"
}
}
private const val BOOT_CLOCK_SLACK_MS = 60_000L
private fun now(): String = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US).format(Date())
}
@@ -0,0 +1,30 @@
package com.droiddeck.launcher.session
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test
class SessionRestartNoteTest {
private val started = 1_731_100_000_000L
@Test fun aBootAfterTheSessionBeganIsReportedAsARestart() {
val note = SessionArtifacts.restartNote(started, started + 20 * 60_000L)
assertNotNull(note)
assertTrue(note!!.startsWith("The device itself probably restarted"))
}
@Test fun aBootBeforeTheSessionBeganMeansOnlyTheAppEnded() {
val note = SessionArtifacts.restartNote(started, started - 3 * 3_600_000L)
assertTrue(note!!.startsWith("The device does not appear to have restarted"))
}
@Test fun clockCorrectionsWithinAMinuteAreNotARestart() {
val note = SessionArtifacts.restartNote(started, started + 30_000L)
assertTrue(note!!.startsWith("The device does not appear to have restarted"))
}
@Test fun aFolderWithoutAStartTimeSaysNothing() {
assertNull(SessionArtifacts.restartNote(0L, started))
}
}