Skip to content

Commit 65f75e6

Browse files
committed
Debug log: prepare database for persisting across app restarts
1 parent 1311278 commit 65f75e6

File tree

2 files changed

+108
-0
lines changed
  • app
    • schemas/com.battlelancer.seriesguide.diagnostics.DebugLogDatabase
    • src/main/java/com/battlelancer/seriesguide/diagnostics

2 files changed

+108
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"formatVersion": 1,
3+
"database": {
4+
"version": 1,
5+
"identityHash": "21481ab4ef726d3a2527dbc9c9835889",
6+
"entities": [
7+
{
8+
"tableName": "debug_log",
9+
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `priority` INTEGER, `tag` TEXT, `message` TEXT, `timestamp` INTEGER, PRIMARY KEY(`id`))",
10+
"fields": [
11+
{
12+
"fieldPath": "id",
13+
"columnName": "id",
14+
"affinity": "INTEGER",
15+
"notNull": true
16+
},
17+
{
18+
"fieldPath": "priority",
19+
"columnName": "priority",
20+
"affinity": "INTEGER"
21+
},
22+
{
23+
"fieldPath": "tag",
24+
"columnName": "tag",
25+
"affinity": "TEXT"
26+
},
27+
{
28+
"fieldPath": "message",
29+
"columnName": "message",
30+
"affinity": "TEXT"
31+
},
32+
{
33+
"fieldPath": "timestamp",
34+
"columnName": "timestamp",
35+
"affinity": "INTEGER"
36+
}
37+
],
38+
"primaryKey": {
39+
"autoGenerate": false,
40+
"columnNames": [
41+
"id"
42+
]
43+
}
44+
}
45+
],
46+
"setupQueries": [
47+
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
48+
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '21481ab4ef726d3a2527dbc9c9835889')"
49+
]
50+
}
51+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: Copyright © 2025 Uwe Trottmann <[email protected]>
3+
4+
package com.battlelancer.seriesguide.diagnostics
5+
6+
import android.content.Context
7+
import androidx.room.ColumnInfo
8+
import androidx.room.Dao
9+
import androidx.room.Database
10+
import androidx.room.Entity
11+
import androidx.room.Insert
12+
import androidx.room.PrimaryKey
13+
import androidx.room.Query
14+
import androidx.room.Room
15+
import androidx.room.RoomDatabase
16+
17+
@Database(
18+
entities = [DbDebugLogEntry::class],
19+
version = 1
20+
)
21+
abstract class DebugLogDatabase : RoomDatabase() {
22+
abstract fun debugLogHelper(): DebugLogHelper
23+
24+
companion object {
25+
26+
private const val DATABASE_NAME = "debug-log.db"
27+
28+
fun build(context: Context): DebugLogDatabase {
29+
return Room
30+
.databaseBuilder(context, DebugLogDatabase::class.java, DATABASE_NAME)
31+
.build()
32+
}
33+
}
34+
}
35+
36+
@Entity(tableName = "debug_log")
37+
data class DbDebugLogEntry(
38+
@PrimaryKey val id: Int,
39+
@ColumnInfo(name = "priority") val priority: Int?,
40+
@ColumnInfo(name = "tag") val tag: String?,
41+
@ColumnInfo(name = "message") val message: String?,
42+
@ColumnInfo(name = "timestamp") val timestamp: Long?
43+
)
44+
45+
@Dao
46+
interface DebugLogHelper {
47+
48+
@Insert
49+
suspend fun insert(entry: DbDebugLogEntry)
50+
51+
@Query("SELECT * FROM debug_log")
52+
suspend fun getAll(): List<DbDebugLogEntry>
53+
54+
@Query("DELETE FROM debug_log WHERE timestamp < :deleteIfOlderThan")
55+
suspend fun deleteOld(deleteIfOlderThan: Long)
56+
57+
}

0 commit comments

Comments
 (0)