Skip to content

Commit 9b3c708

Browse files
authored
Merge pull request #65 from AppDevNext/ThreadCheck
optional thread check in Filelogger
2 parents b73c419 + e6ff06d commit 9b3c708

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

LogcatCoreLib/src/main/java/info/hannes/timber/FileLoggingTree.kt

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package info.hannes.timber
22

33
import android.annotation.SuppressLint
44
import android.content.Context
5+
import android.os.Handler
56
import android.util.Log
67
import androidx.lifecycle.MutableLiveData
78
import 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

Comments
 (0)