Merge pull request #374 from gavine99/fix-internal-file-logging

fix internal file logging to work on all sdk versions
This commit is contained in:
octoshrimpy
2025-03-19 23:04:27 -04:00
committed by GitHub
@@ -18,13 +18,19 @@
*/ */
package dev.octoshrimpy.quik.common.util package dev.octoshrimpy.quik.common.util
import android.content.Context
import android.net.Uri
import android.os.Build
import android.os.Environment import android.os.Environment
import android.provider.MediaStore
import android.util.Log import android.util.Log
import androidx.core.content.contentValuesOf
import dev.octoshrimpy.quik.util.Preferences import dev.octoshrimpy.quik.util.Preferences
import dev.octoshrimpy.quik.util.tryOrNull
import io.reactivex.schedulers.Schedulers import io.reactivex.schedulers.Schedulers
import timber.log.Timber import timber.log.Timber
import java.io.File import java.io.File
import java.io.FileOutputStream import java.io.FileNotFoundException
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.* import java.util.*
import javax.inject.Inject import javax.inject.Inject
@@ -34,40 +40,86 @@ import javax.inject.Singleton
* Based off Vipin Kumar's FileLoggingTree: https://medium.com/@vicky7230/file-logging-with-timber-4e63a1b86a66 * Based off Vipin Kumar's FileLoggingTree: https://medium.com/@vicky7230/file-logging-with-timber-4e63a1b86a66
*/ */
@Singleton @Singleton
class FileLoggingTree @Inject constructor(private val prefs: Preferences) : Timber.DebugTree() { class FileLoggingTree @Inject constructor(
private val prefs: Preferences,
private val context: Context
) : Timber.DebugTree() {
companion object {
val TAG: String? = FileLoggingTree::class.simpleName
}
private val fileLock: Boolean = false private var logFileUri: Uri? = null
override fun log(priority: Int, tag: String, message: String, t: Throwable?) { override fun log(priority: Int, tag: String, message: String, t: Throwable?) {
if (!prefs.logging.get()) return if (!prefs.logging.get()) return
Schedulers.io().scheduleDirect { Schedulers.io().scheduleDirect {
val timestamp = SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS", Locale.getDefault()).format(System.currentTimeMillis()) synchronized(this) { // one thread can access file at a time
val priorityString = when (priority) { val logItem =
"${ // date/time
SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss:SSS",
Locale.getDefault()
).format(System.currentTimeMillis())
} ${ // priority
when (priority) {
Log.VERBOSE -> "V" Log.VERBOSE -> "V"
Log.DEBUG -> "D" Log.DEBUG -> "D"
Log.INFO -> "I" Log.INFO -> "I"
Log.WARN -> "W" Log.WARN -> "W"
Log.ERROR -> "E" Log.ERROR -> "E"
else -> "WTF" else -> "?"
}
}/${ // tag
tag
}: ${ // message
message
}${ // stack trace
Log.getStackTraceString(t)
}\n"
try {
// if uri of log file not yet determined, get one now
if (logFileUri == null) {
val filename = "Quik-log-${
SimpleDateFormat(
"yyyy-MM-dd",
Locale.getDefault()
).format(System.currentTimeMillis())
}.log"
logFileUri =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
// use media store
context.contentResolver.insert(
MediaStore.Downloads.EXTERNAL_CONTENT_URI,
contentValuesOf(
MediaStore.MediaColumns.MIME_TYPE to "text/plain",
MediaStore.MediaColumns.RELATIVE_PATH to
Environment.DIRECTORY_DOWNLOADS,
MediaStore.MediaColumns.DISPLAY_NAME to filename,
)
)
else
// use direct access to 'external' dir
File(Environment.getExternalStorageDirectory(), filename).let {
tryOrNull { it.createNewFile() }
Uri.fromFile(it)
}
} }
// Format the log to be written to the file logFileUri?.let {
val log = "$timestamp $priorityString/$tag: $message ${Log.getStackTraceString(t)}\n".toByteArray() context.contentResolver.openOutputStream(it, "wa")?.use {
// write the log entry
// Ensure that only one thread is writing to the file at a time it.write(logItem.toByteArray())
synchronized(fileLock) { }
try { }
// Create the directory } catch (e: FileNotFoundException) {
val dir = File(Environment.getExternalStorageDirectory(), "QKSMS/Logs").apply { mkdirs() } Log.e(TAG, "Log file went away. Lost log file item: $logItem")
// log file seems to have gone away. start a new file next time through
// Create the file logFileUri = null
val file = File(dir, "${SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(System.currentTimeMillis())}.log")
// Write the log to the file
FileOutputStream(file, true).use { fileOutputStream -> fileOutputStream.write(log) }
} catch (e: Exception) { } catch (e: Exception) {
Log.e("FileLoggingTree", "Error while logging into file", e) Log.e(TAG, "Error while logging into file", e)
} }
} }
} }