@@ -2,6 +2,7 @@ package info.hannes.timber
22
33import android.annotation.SuppressLint
44import android.content.Context
5+ import android.os.Handler
56import android.util.Log
67import androidx.lifecycle.MutableLiveData
78import java.io.File
@@ -44,12 +45,12 @@ open class FileLoggingTree(externalCacheDir: File, context: Context? = null, fil
4445 else -> " $priority "
4546 }
4647
47- val writer = FileWriter (file, true )
4848 val textLine = " $priorityText $logTimeStamp$tag$message \n "
49- writer.append(textLine)
50- writer.flush()
51- writer.close()
52- lastLogEntry.value = textLine
49+
50+ if (! threadCheck && Thread .currentThread().name == " main" )
51+ doFileLogging(textLine)
52+ else
53+ Handler ().post { doFileLogging(textLine) }
5354 } catch (e: Exception ) {
5455 // Log to prevent an endless loop
5556 if (! logImpossible) {
@@ -62,11 +63,20 @@ open class FileLoggingTree(externalCacheDir: File, context: Context? = null, fil
6263 super .log(priority, tag, message, t)
6364 }
6465
66+ private fun doFileLogging (textLine : String ) {
67+ val writer = FileWriter (file, true )
68+ writer.append(textLine)
69+ writer.flush()
70+ writer.close()
71+ lastLogEntry.value = textLine
72+ }
73+
6574 fun getFileName (): String = file.absolutePath
6675
6776 companion object {
6877 private val LOG_TAG = FileLoggingTree ::class .java.simpleName
6978 private var logImpossible = false
79+ var threadCheck = false
7080 val lastLogEntry: MutableLiveData <String > = MutableLiveData <String >()
7181 }
7282}
0 commit comments