@@ -3,12 +3,15 @@ package info.hannes.timber
33import android.annotation.SuppressLint
44import android.content.Context
55import android.os.Handler
6+ import android.os.Looper
67import android.util.Log
78import androidx.lifecycle.MutableLiveData
89import java.io.File
910import java.io.FileWriter
1011import java.text.SimpleDateFormat
11- import java.util.*
12+ import java.util.Date
13+ import java.util.Locale
14+ import java.util.UUID
1215
1316@Suppress(" unused" )
1417@SuppressLint(" LogNotTimber" )
@@ -45,12 +48,17 @@ open class FileLoggingTree(externalCacheDir: File, context: Context? = null, fil
4548 else -> " $priority "
4649 }
4750
51+ val writer = FileWriter (file, true )
4852 val textLine = " $priorityText $logTimeStamp$tag$message \n "
53+ writer.append(textLine)
54+ writer.flush()
55+ writer.close()
4956
50- if (! threadCheck && Thread .currentThread().name == " main" )
51- doFileLogging( textLine)
57+ if (Thread .currentThread().name == " main" )
58+ lastLogEntry.value = textLine
5259 else
53- Handler ().post { doFileLogging(textLine) }
60+ Handler (Looper .getMainLooper()).post { lastLogEntry.value = textLine }
61+
5462 } catch (e: Exception ) {
5563 // Log to prevent an endless loop
5664 if (! logImpossible) {
@@ -63,20 +71,11 @@ open class FileLoggingTree(externalCacheDir: File, context: Context? = null, fil
6371 super .log(priority, tag, message, t)
6472 }
6573
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-
7474 fun getFileName (): String = file.absolutePath
7575
7676 companion object {
7777 private val LOG_TAG = FileLoggingTree ::class .java.simpleName
7878 private var logImpossible = false
79- var threadCheck = false
8079 val lastLogEntry: MutableLiveData <String > = MutableLiveData <String >()
8180 }
8281}
0 commit comments