Skip to content

Commit 944340f

Browse files
authored
fix(logger): handled logs pushed before initialisation of plugin (#249)
* fix(logger): handled logs pushed before initialisation of plugin * fix(logger): minor refactor * fix(logger): minor refactor
1 parent 9cc1f73 commit 944340f

File tree

4 files changed

+43
-18
lines changed

4 files changed

+43
-18
lines changed

pluto-plugins/plugins/logger/lib/src/main/java/com/pluto/plugins/logger/PlutoLog.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,44 @@ class PlutoLog private constructor() {
1212
companion object {
1313

1414
@JvmStatic
15+
@JvmOverloads
1516
fun v(tag: String, message: String, tr: Throwable? = null) {
1617
process(Log.VERBOSE, tag, message, tr, Thread.currentThread().getStackTraceElement())
1718
}
1819

1920
@JvmStatic
21+
@JvmOverloads
2022
fun d(tag: String, message: String, tr: Throwable? = null) {
2123
process(Log.DEBUG, tag, message, tr, Thread.currentThread().getStackTraceElement())
2224
}
2325

2426
@JvmStatic
27+
@JvmOverloads
2528
fun i(tag: String, message: String, tr: Throwable? = null) {
2629
process(Log.INFO, tag, message, tr, Thread.currentThread().getStackTraceElement())
2730
}
2831

2932
@JvmStatic
33+
@JvmOverloads
3034
fun w(tag: String, message: String, tr: Throwable? = null) {
3135
process(Log.WARN, tag, message, tr, Thread.currentThread().getStackTraceElement())
3236
}
3337

3438
@JvmStatic
39+
@JvmOverloads
3540
fun e(tag: String, message: String, tr: Throwable? = null) {
3641
process(Log.ERROR, tag, message, tr, Thread.currentThread().getStackTraceElement())
3742
}
3843

3944
@JvmStatic
45+
@JvmOverloads
4046
fun wtf(tag: String, message: String, tr: Throwable? = null) {
4147
process(Log.ASSERT, tag, message, tr, Thread.currentThread().getStackTraceElement())
4248
}
4349

4450
@JvmStatic
45-
fun event(tag: String, event: String, attributes: HashMap<String, Any?>?) {
51+
@JvmOverloads
52+
fun event(tag: String, event: String, attributes: HashMap<String, Any?>? = null) {
4653
processEvent(tag, event, attributes, Thread.currentThread().getStackTraceElement())
4754
}
4855

pluto-plugins/plugins/logger/lib/src/main/java/com/pluto/plugins/logger/internal/LogsProcessor.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ internal class LogsProcessor private constructor() {
1414

1515
fun process(priority: Int, tag: String, message: String, tr: Throwable?, stackTraceElement: StackTraceElement) {
1616
val stackTrace = stackTraceElement.stackTrace()
17-
LogDBHandler.persist(priority2Level(priority), tag, message, tr, stackTrace)
17+
LogDBHandler.persistLog(priority2Level(priority), tag, message, tr, stackTrace)
1818
consolePrint(priority2Level(priority), tag, message, tr, stackTrace)
1919
}
2020

2121
fun processEvent(tag: String, event: String, attr: HashMap<String, Any?>?, stackTraceElement: StackTraceElement) {
2222
val moshi = Moshi.Builder().build()
2323
val moshiAdapter: JsonAdapter<Map<String, Any?>?> = moshi.adapter(Types.newParameterizedType(Map::class.java, String::class.java, Any::class.java))
2424
val stackTrace = stackTraceElement.stackTrace()
25-
LogDBHandler.persist(Level.Event, tag, event, attr, stackTrace)
25+
LogDBHandler.persistEvent(Level.Event, tag, event, attr, stackTrace)
2626
consolePrint(Level.Event, tag, "$event => ${moshiAdapter.toJson(attr)}", null, stackTrace)
2727
}
2828

pluto-plugins/plugins/logger/lib/src/main/java/com/pluto/plugins/logger/internal/persistence/LogDBHandler.kt

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,57 @@ import kotlinx.coroutines.launch
1313

1414
internal object LogDBHandler {
1515

16-
private lateinit var logDao: LogDao
16+
private var logDao: LogDao? = null
1717
private val coroutineScope = CoroutineScope(Dispatchers.IO)
1818

19+
/**
20+
* Store log temporarily till the LogDBHandler is not initialised.
21+
*/
22+
private val tempList = arrayListOf<LogEntity>()
23+
1924
fun initialize(context: Context) {
2025
logDao = DatabaseManager(context).db.exceptionDao()
26+
processTempList()
2127
}
2228

23-
fun persist(level: Level, tag: String, message: String?, tr: Throwable?, ele: StackTrace) {
29+
fun persistLog(level: Level, tag: String, message: String?, tr: Throwable?, ele: StackTrace) {
2430
coroutineScope.launch {
25-
logDao.save(
26-
LogEntity(
27-
timestamp = System.currentTimeMillis(),
28-
data = LogData(level, tag, message ?: "", tr?.asExceptionData(), ele)
29-
),
31+
val logEntity = LogEntity(
32+
timestamp = System.currentTimeMillis(),
33+
data = LogData(level, tag, message ?: "", tr?.asExceptionData(), ele)
3034
)
35+
logDao?.save(logEntity) ?: run { pushToTempList(logEntity) }
3136
}
3237
}
3338

34-
fun persist(level: Level, tag: String, event: String, attr: HashMap<String, Any?>?, ele: StackTrace) {
39+
fun persistEvent(level: Level, tag: String, event: String, attr: HashMap<String, Any?>?, ele: StackTrace) {
3540
coroutineScope.launch {
36-
logDao.save(
37-
LogEntity(
38-
timestamp = System.currentTimeMillis(),
39-
data = LogData(level, tag, event ?: "", null, ele, attr)
40-
),
41+
val logEntity = LogEntity(
42+
timestamp = System.currentTimeMillis(),
43+
data = LogData(level, tag, event, null, ele, attr)
4144
)
45+
logDao?.save(logEntity) ?: run { pushToTempList(logEntity) }
4246
}
4347
}
4448

4549
suspend fun fetchAll(): List<LogEntity>? {
46-
return logDao.fetchAll()
50+
return logDao?.fetchAll()
4751
}
4852

4953
fun flush() {
5054
coroutineScope.launch {
51-
logDao.deleteAll()
55+
logDao?.deleteAll()
56+
}
57+
}
58+
59+
private fun pushToTempList(logEntity: LogEntity) {
60+
tempList.add(logEntity)
61+
}
62+
63+
private fun processTempList() {
64+
coroutineScope.launch {
65+
logDao?.saveAll(tempList)
66+
tempList.clear()
5267
}
5368
}
5469
}

pluto-plugins/plugins/logger/lib/src/main/java/com/pluto/plugins/logger/internal/persistence/LogDao.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ internal interface LogDao {
1111
@Insert(onConflict = OnConflictStrategy.REPLACE)
1212
suspend fun save(entity: LogEntity)
1313

14+
@Insert(onConflict = OnConflictStrategy.REPLACE)
15+
suspend fun saveAll(entities: List<LogEntity>)
16+
1417
@Query("SELECT * FROM logs where id is :id")
1518
suspend fun fetch(id: Int): LogEntity?
1619

0 commit comments

Comments
 (0)