-
-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5335 from neonowy/kz-add-in-app-logger
Add in-app log viewer
- Loading branch information
Showing
66 changed files
with
1,333 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
app/src/androidTest/java/de/westnordost/streetcomplete/data/logs/LogsDaoTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package de.westnordost.streetcomplete.data.logs | ||
|
||
import de.westnordost.streetcomplete.data.ApplicationDbTestCase | ||
import de.westnordost.streetcomplete.data.logs.LogLevel.* | ||
import de.westnordost.streetcomplete.util.ktx.containsExactlyInAnyOrder | ||
import kotlin.test.BeforeTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertTrue | ||
|
||
class LogsDaoTest : ApplicationDbTestCase() { | ||
private lateinit var dao: LogsDao | ||
|
||
@BeforeTest fun createDao() { | ||
dao = LogsDao(database) | ||
} | ||
|
||
@Test fun getAll_sorts_by_timestamp_descending() { | ||
val m1 = createMessage("1", timestamp = 1) | ||
val m2 = createMessage("2", timestamp = 100) | ||
val m3 = createMessage("3", timestamp = 10) | ||
|
||
listOf(m1, m2, m3).forEach { dao.add(it) } | ||
|
||
// sorted by timestamp ascending | ||
assertEquals(listOf(m1, m3, m2), dao.getAll()) | ||
} | ||
|
||
@Test fun getAll_filters_by_levels() { | ||
val m1 = createMessage("1", level = VERBOSE) | ||
val m2 = createMessage("2", level = WARNING) | ||
val m3 = createMessage("3", level = ERROR) | ||
|
||
listOf(m1, m2, m3).forEach { dao.add(it) } | ||
|
||
assertTrue(dao.getAll(levels = setOf(WARNING, ERROR)).containsExactlyInAnyOrder(listOf(m2, m3))) | ||
} | ||
|
||
@Test fun getAll_filters_containing_string() { | ||
val m1 = createMessage("foo") | ||
val m2 = createMessage("bar") | ||
val m3 = createMessage("foobar") | ||
|
||
listOf(m1, m2, m3).forEach { dao.add(it) } | ||
|
||
assertTrue(dao.getAll(messageContains = "foo").containsExactlyInAnyOrder(listOf(m1, m3))) | ||
} | ||
|
||
@Test fun getAll_filters_older_than_timestamp() { | ||
val m1 = createMessage("1", timestamp = 1) | ||
val m2 = createMessage("2", timestamp = 10) | ||
|
||
listOf(m1, m2).forEach { dao.add(it) } | ||
|
||
assertEquals(listOf(m1), dao.getAll(olderThan = 10)) | ||
} | ||
|
||
@Test fun getAll_filters_newer_than_timestamp() { | ||
val m1 = createMessage("1", timestamp = 1) | ||
val m2 = createMessage("2", timestamp = 10) | ||
|
||
listOf(m1, m2).forEach { dao.add(it) } | ||
|
||
assertEquals(listOf(m2), dao.getAll(newerThan = 1)) | ||
} | ||
} | ||
|
||
private fun createMessage( | ||
message: String, | ||
level: LogLevel = VERBOSE, | ||
timestamp: Long = 1 | ||
) = LogMessage( | ||
level, | ||
TAG, | ||
message, | ||
null, | ||
timestamp | ||
) | ||
|
||
private const val TAG = "LogsDaoTest" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import android.content.res.Resources | |
import androidx.preference.PreferenceManager | ||
import de.westnordost.streetcomplete.util.CrashReportExceptionHandler | ||
import de.westnordost.streetcomplete.util.SoundFx | ||
import de.westnordost.streetcomplete.util.logs.DatabaseLogger | ||
import org.koin.android.ext.koin.androidContext | ||
import org.koin.dsl.module | ||
|
||
|
@@ -14,6 +15,7 @@ val appModule = module { | |
factory<Resources> { androidContext().resources } | ||
factory<SharedPreferences> { PreferenceManager.getDefaultSharedPreferences(androidContext()) } | ||
|
||
single { CrashReportExceptionHandler(androidContext(), "[email protected]", "crashreport.txt") } | ||
single { CrashReportExceptionHandler(androidContext(), get(), "[email protected]", "crashreport.txt") } | ||
single { DatabaseLogger(get()) } | ||
single { SoundFx(androidContext()) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
app/src/main/java/de/westnordost/streetcomplete/data/Preloader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
app/src/main/java/de/westnordost/streetcomplete/data/logs/LogLevel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package de.westnordost.streetcomplete.data.logs | ||
|
||
import de.westnordost.streetcomplete.R | ||
import de.westnordost.streetcomplete.data.logs.LogLevel.* | ||
|
||
enum class LogLevel { | ||
VERBOSE, | ||
DEBUG, | ||
INFO, | ||
WARNING, | ||
ERROR | ||
} | ||
|
||
val LogLevel.styleResId: Int get() = when (this) { | ||
VERBOSE -> R.style.TextAppearance_LogMessage_Verbose | ||
DEBUG -> R.style.TextAppearance_LogMessage_Debug | ||
INFO -> R.style.TextAppearance_LogMessage_Info | ||
WARNING -> R.style.TextAppearance_LogMessage_Warning | ||
ERROR -> R.style.TextAppearance_LogMessage_Error | ||
} | ||
|
||
val LogLevel.colorId: Int get() = when (this) { | ||
VERBOSE -> R.color.log_verbose | ||
DEBUG -> R.color.log_debug | ||
INFO -> R.color.log_info | ||
WARNING -> R.color.log_warning | ||
ERROR -> R.color.log_error | ||
} |
33 changes: 33 additions & 0 deletions
33
app/src/main/java/de/westnordost/streetcomplete/data/logs/LogMessage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package de.westnordost.streetcomplete.data.logs | ||
|
||
import kotlinx.datetime.Instant | ||
import kotlinx.datetime.TimeZone | ||
import kotlinx.datetime.toLocalDateTime | ||
|
||
data class LogMessage( | ||
val level: LogLevel, | ||
val tag: String, | ||
val message: String, | ||
val error: String?, | ||
val timestamp: Long | ||
) { | ||
override fun toString(): String { | ||
var string = "[$tag] $message" | ||
|
||
if (error != null) { | ||
string += " $error" | ||
} | ||
|
||
return string | ||
} | ||
} | ||
|
||
fun Iterable<LogMessage>.format(tz: TimeZone = TimeZone.currentSystemDefault()): String { | ||
return joinToString("\n") { | ||
val timestamp = Instant.fromEpochMilliseconds(it.timestamp) | ||
.toLocalDateTime(tz) | ||
.toString() | ||
|
||
"$timestamp: $it" | ||
} | ||
} |
Oops, something went wrong.