package com.ustadmobile.testservercontroller.util import com.ustadmobile.testservercontroller.util.ext.fileNameSuffix import kotlinx.datetime.TimeZone import kotlinx.datetime.toLocalDateTime import java.io.File import java.util.concurrent.TimeUnit import kotlin.time.Clock import kotlin.time.ExperimentalTime /** * Dump adb logcat output to a file. The ADB logcat dump (-d) command will dump to a file on the Android emulator/device, * this function runs that command, then uses the adb pull command to copy it to the adb host, and deletes the temporary * file. * * @param adbPath the path to the adb command * @param destFile */ @OptIn(ExperimentalTime::class) fun dumpLogcatToFile( adbPath: File, destFile: File, ) { val dateTimeNow = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()) val tmpFileName = "/sdcard/${dateTimeNow.fileNameSuffix()}.txt" ProcessBuilder(listOf(adbPath.absolutePath, "logcat", "-d", "-f", tmpFileName)).start() .waitFor(2_000, TimeUnit.MILLISECONDS) ProcessBuilder( listOf(adbPath.absolutePath, "pull", tmpFileName, destFile.absolutePath) ).start().waitFor(2_000, TimeUnit.MILLISECONDS) ProcessBuilder( listOf(adbPath.absolutePath, "shell", "rm", tmpFileName) ).start().waitFor(2_000, TimeUnit.MILLISECONDS) }