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 =
Log.VERBOSE -> "V" "${ // date/time
Log.DEBUG -> "D" SimpleDateFormat(
Log.INFO -> "I" "yyyy-MM-dd HH:mm:ss:SSS",
Log.WARN -> "W" Locale.getDefault()
Log.ERROR -> "E" ).format(System.currentTimeMillis())
else -> "WTF" } ${ // priority
} when (priority) {
Log.VERBOSE -> "V"
Log.DEBUG -> "D"
Log.INFO -> "I"
Log.WARN -> "W"
Log.ERROR -> "E"
else -> "?"
}
}/${ // tag
tag
}: ${ // message
message
}${ // stack trace
Log.getStackTraceString(t)
}\n"
// Format the log to be written to the file
val log = "$timestamp $priorityString/$tag: $message ${Log.getStackTraceString(t)}\n".toByteArray()
// Ensure that only one thread is writing to the file at a time
synchronized(fileLock) {
try { try {
// Create the directory // if uri of log file not yet determined, get one now
val dir = File(Environment.getExternalStorageDirectory(), "QKSMS/Logs").apply { mkdirs() } if (logFileUri == null) {
val filename = "Quik-log-${
SimpleDateFormat(
"yyyy-MM-dd",
Locale.getDefault()
).format(System.currentTimeMillis())
}.log"
// Create the file logFileUri =
val file = File(dir, "${SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(System.currentTimeMillis())}.log") 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)
}
}
// Write the log to the file logFileUri?.let {
FileOutputStream(file, true).use { fileOutputStream -> fileOutputStream.write(log) } context.contentResolver.openOutputStream(it, "wa")?.use {
// write the log entry
it.write(logItem.toByteArray())
}
}
} catch (e: FileNotFoundException) {
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
logFileUri = null
} catch (e: Exception) { } catch (e: Exception) {
Log.e("FileLoggingTree", "Error while logging into file", e) Log.e(TAG, "Error while logging into file", e)
} }
} }
} }