app: handle errors when saving logs

Saving the install, action or Magisk log didn't handle any error: if
creating or writing the file failed (e.g. storage full, or the media
store rejecting the file), the exception reached the default handler
and the app exited. On Android 10 and older, a module name containing
a slash also made the action log name point to a missing directory.

Save logs through a shared helper that replaces slashes in the name
and shows an error toast when saving fails.

Assisted-by: Claude Opus 5.5
This commit is contained in:
topjohnwu
2026-10-24 13:01:45 -07:00
committed by John Wu
parent 9f512a6a05
commit 6fa7eed138
5 changed files with 70 additions and 61 deletions
@@ -3,12 +3,21 @@ package com.topjohnwu.magisk.arch
import android.widget.Toast
import androidx.annotation.StringRes
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.topjohnwu.magisk.core.AppContext
import com.topjohnwu.magisk.core.R
import com.topjohnwu.magisk.core.ktx.toast
import com.topjohnwu.magisk.core.utils.MediaStoreUtils
import com.topjohnwu.magisk.core.utils.MediaStoreUtils.outputStream
import com.topjohnwu.magisk.ui.navigation.Route
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.launch
import timber.log.Timber
import java.io.Writer
abstract class BaseViewModel : ViewModel() {
@@ -28,4 +37,25 @@ abstract class BaseViewModel : ViewModel() {
fun navigateTo(route: Route) {
_navEvents.trySend(route)
}
/**
* Save a log to the download folder with [write], then show where it was saved. Saving can
* fail for many reasons (e.g. storage full, or the media store rejecting the file), so
* report failures instead of letting them crash the app.
*/
protected fun saveLogFile(name: String, write: (Writer) -> Unit) {
viewModelScope.launch(Dispatchers.IO) {
try {
// Names can contain parts like module names, which must not create directories
val file = MediaStoreUtils.getFile(name.replace('/', '_'))
file.uri.outputStream().bufferedWriter().use(write)
showToast(file.toString())
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
Timber.e(e)
showToast(R.string.log_save_failed)
}
}
}
}
@@ -14,10 +14,8 @@ import com.topjohnwu.magisk.core.ktx.timeFormatStandard
import com.topjohnwu.magisk.core.ktx.toTime
import com.topjohnwu.magisk.core.ktx.writeTo
import com.topjohnwu.magisk.core.tasks.MagiskInstaller
import com.topjohnwu.magisk.core.utils.MediaStoreUtils
import com.topjohnwu.magisk.core.utils.MediaStoreUtils.displayName
import com.topjohnwu.magisk.core.utils.MediaStoreUtils.inputStream
import com.topjohnwu.magisk.core.utils.MediaStoreUtils.outputStream
import com.topjohnwu.magisk.terminal.TerminalEmulator
import com.topjohnwu.magisk.terminal.appendLineOnMain
import com.topjohnwu.magisk.terminal.runSuCommand
@@ -196,25 +194,20 @@ class FlashViewModel(private val savedState: SavedStateHandle) : BaseViewModel()
}
fun saveLog() {
viewModelScope.launch(Dispatchers.IO) {
val name = "magisk_install_log_%s.log".format(
System.currentTimeMillis().toTime(timeFormatStandard)
)
val file = MediaStoreUtils.getFile(name)
file.uri.outputStream().bufferedWriter().use { writer ->
val transcript = emulator.screen.transcriptText
if (flashAction == Const.Value.FLASH_ZIP) {
writer.write(transcript)
} else {
synchronized(logItems) {
logItems.forEach {
writer.write(it)
writer.newLine()
}
val name = "magisk_install_log_%s.log".format(
System.currentTimeMillis().toTime(timeFormatStandard)
)
saveLogFile(name) { writer ->
if (flashAction == Const.Value.FLASH_ZIP) {
writer.write(emulator.screen.transcriptText)
} else {
synchronized(logItems) {
logItems.forEach {
writer.write(it)
writer.write("\n")
}
}
}
showToast(file.toString())
}
}
@@ -11,8 +11,6 @@ import com.topjohnwu.magisk.core.ktx.toTime
import com.topjohnwu.magisk.core.model.su.SuLog
import com.topjohnwu.magisk.core.repository.LogRepository
import com.topjohnwu.magisk.core.su.SuEvents
import com.topjohnwu.magisk.core.utils.MediaStoreUtils
import com.topjohnwu.magisk.core.utils.MediaStoreUtils.outputStream
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
@@ -63,38 +61,34 @@ class LogViewModel(
}
fun saveMagiskLog() {
viewModelScope.launch(Dispatchers.IO) {
val filename = "magisk_log_%s.log".format(
System.currentTimeMillis().toTime(timeFormatStandard))
val logFile = MediaStoreUtils.getFile(filename)
logFile.uri.outputStream().bufferedWriter().use { file ->
file.write("---Detected Device Info---\n\n")
file.write("isAB=${Info.isAB}\n")
file.write("isSAR=${Info.isSAR}\n")
file.write("ramdisk=${Info.ramdisk}\n")
val uname = Os.uname()
file.write("kernel=${uname.sysname} ${uname.machine} ${uname.release} ${uname.version}\n")
val filename = "magisk_log_%s.log".format(
System.currentTimeMillis().toTime(timeFormatStandard))
saveLogFile(filename) { file ->
file.write("---Detected Device Info---\n\n")
file.write("isAB=${Info.isAB}\n")
file.write("isSAR=${Info.isSAR}\n")
file.write("ramdisk=${Info.ramdisk}\n")
val uname = Os.uname()
file.write("kernel=${uname.sysname} ${uname.machine} ${uname.release} ${uname.version}\n")
file.write("\n\n---System Properties---\n\n")
ProcessBuilder("getprop").start()
.inputStream.reader().use { it.copyTo(file) }
file.write("\n\n---System Properties---\n\n")
ProcessBuilder("getprop").start()
.inputStream.reader().use { it.copyTo(file) }
file.write("\n\n---Environment Variables---\n\n")
System.getenv().forEach { (key, value) -> file.write("${key}=${value}\n") }
file.write("\n\n---Environment Variables---\n\n")
System.getenv().forEach { (key, value) -> file.write("${key}=${value}\n") }
file.write("\n\n---System MountInfo---\n\n")
FileInputStream("/proc/self/mountinfo").reader().use { it.copyTo(file) }
file.write("\n\n---System MountInfo---\n\n")
FileInputStream("/proc/self/mountinfo").reader().use { it.copyTo(file) }
file.write("\n---Magisk Logs---\n")
file.write("${Info.env.versionString} (${Info.env.versionCode})\n\n")
if (Info.env.isActive) file.write(magiskLogRaw)
file.write("\n---Magisk Logs---\n")
file.write("${Info.env.versionString} (${Info.env.versionCode})\n\n")
if (Info.env.isActive) file.write(magiskLogRaw)
file.write("\n---Manager Logs---\n")
file.write("${BuildConfig.APP_VERSION_NAME} (${BuildConfig.APP_VERSION_CODE})\n\n")
ProcessBuilder("logcat", "-d").start()
.inputStream.reader().use { it.copyTo(file) }
}
showToast(logFile.toString())
file.write("\n---Manager Logs---\n")
file.write("${BuildConfig.APP_VERSION_NAME} (${BuildConfig.APP_VERSION_CODE})\n\n")
ProcessBuilder("logcat", "-d").start()
.inputStream.reader().use { it.copyTo(file) }
}
}
@@ -5,8 +5,6 @@ import androidx.lifecycle.viewModelScope
import com.topjohnwu.magisk.arch.BaseViewModel
import com.topjohnwu.magisk.core.ktx.timeFormatStandard
import com.topjohnwu.magisk.core.ktx.toTime
import com.topjohnwu.magisk.core.utils.MediaStoreUtils
import com.topjohnwu.magisk.core.utils.MediaStoreUtils.outputStream
import com.topjohnwu.magisk.terminal.TerminalEmulator
import com.topjohnwu.magisk.terminal.runSuCommand
import com.topjohnwu.magisk.terminal.shellQuoted
@@ -70,18 +68,11 @@ class ActionViewModel(private val savedState: SavedStateHandle) : BaseViewModel(
}
fun saveLog() {
viewModelScope.launch(Dispatchers.IO) {
val name = "%s_action_log_%s.log".format(
actionName,
System.currentTimeMillis().toTime(timeFormatStandard)
)
val file = MediaStoreUtils.getFile(name)
file.uri.outputStream().bufferedWriter().use { writer ->
val transcript = emulator.screen.transcriptText
writer.write(transcript)
}
showToast(file.toString())
}
val name = "%s_action_log_%s.log".format(
actionName,
System.currentTimeMillis().toTime(timeFormatStandard)
)
saveLogFile(name) { it.write(emulator.screen.transcriptText) }
}
companion object {
+1
View File
@@ -89,6 +89,7 @@
<string name="menuSaveLog">Save log</string>
<string name="menuClearLog">Clear log</string>
<string name="logs_cleared">Log successfully cleared</string>
<string name="log_save_failed">Failed to save the log</string>
<string name="pid">PID: %1$d</string>
<string name="target_uid">Target UID: %1$d</string>
<string name="target_pid">Target PID: %s</string>