feat: add logs screen

This commit is contained in:
Zane Schepke
2024-03-13 02:52:57 -04:00
parent c0cff297b2
commit 4fc8ffbcbb
38 changed files with 786 additions and 249 deletions
+1
View File
@@ -0,0 +1 @@
/build
+43
View File
@@ -0,0 +1,43 @@
plugins {
alias(libs.plugins.androidLibrary)
alias(libs.plugins.kotlin.android)
}
android {
namespace = "com.zaneschepke.logcatter"
compileSdk = 34
defaultConfig {
minSdk = 26
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.material)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
}
View File
+21
View File
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
@@ -0,0 +1,22 @@
package com.zaneschepke
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.zaneschepke.test", appContext.packageName)
}
}
+4
View File
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest>
</manifest>
@@ -0,0 +1,19 @@
package com.zaneschepke.logcatter
import com.zaneschepke.logcatter.model.LogMessage
object Logcatter {
fun logs(callback: (input: LogMessage) -> Unit) {
clear()
Runtime.getRuntime().exec("logcat -v epoch")
.inputStream
.bufferedReader()
.useLines { lines ->
lines.forEach { callback(LogMessage.from(it)) }
}
}
fun clear() {
Runtime.getRuntime().exec("logcat -c")
}
}
@@ -0,0 +1,48 @@
package com.zaneschepke.logcatter.model
enum class LogLevel(val signifier: String) {
DEBUG("D") {
override fun color(): Long {
return 0xFF2196F3
}
},
INFO("I"){
override fun color(): Long {
return 0xFF4CAF50
}
},
ASSERT("A"){
override fun color(): Long {
return 0xFF9C27B0
}
},
WARNING("W"){
override fun color(): Long {
return 0xFFFFC107
}
},
ERROR("E"){
override fun color(): Long {
return 0xFFF44336
}
},
VERBOSE("V"){
override fun color(): Long {
return 0xFF000000
}
};
abstract fun color() : Long
companion object {
fun fromSignifier(signifier: String) : LogLevel {
return when(signifier) {
DEBUG.signifier -> DEBUG
INFO.signifier -> INFO
WARNING.signifier -> WARNING
VERBOSE.signifier -> VERBOSE
ASSERT.signifier -> ASSERT
ERROR.signifier -> ERROR
else -> VERBOSE
}
}
}
}
@@ -0,0 +1,28 @@
package com.zaneschepke.logcatter.model
import java.time.Instant
data class LogMessage(
val time: Instant,
val pid: String,
val tid: String,
val level : LogLevel,
val tag: String,
val message: String
) {
override fun toString(): String {
return "$time $pid $tid $level $tag message= $message"
}
companion object {
fun from(logcatLine : String) : LogMessage {
return if(logcatLine.contains("---------")) LogMessage(Instant.now(), "0","0",LogLevel.VERBOSE,"System", logcatLine)
else {
//TODO improve this
val parts = logcatLine.trim().split(" ").filter { it.isNotEmpty() }
val epochParts = parts[0].split(".").map { it.toLong() }
val message = parts.subList(5, parts.size).joinToString(" ")
LogMessage(Instant.ofEpochSecond(epochParts[0], epochParts[1]), parts[1], parts[2], LogLevel.fromSignifier(parts[3]), parts[4], message)
}
}
}
}
@@ -0,0 +1,16 @@
package com.zaneschepke
import org.junit.Assert.assertEquals
import org.junit.Test
/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class ExampleUnitTest {
@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
}