Skip to content

brudaswen/android-logcat

Repository files navigation

Android Logcat Parser

Maven Central Snapshot CI Status Codecov License

Library to easily parse Android Logcat, store it in a database and export to txt or csv.

Gradle Dependencies

[versions]
logcat = "3.1.0"

[libraries]
logcat-core = { group = "de.brudaswen.android.logcat", name = "logcat-core", version.ref = "logcat" }
logcat-database = { group = "de.brudaswen.android.logcat", name = "logcat-database", version.ref = "logcat" }
logcat-export = { group = "de.brudaswen.android.logcat", name = "logcat-export", version.ref = "logcat" }
logcat-export-csv = { group = "de.brudaswen.android.logcat", name = "logcat-export-csv", version.ref = "logcat" }
logcat-ui = { group = "de.brudaswen.android.logcat", name = "logcat-ui", version.ref = "logcat" }
// Plain Logcat Core (parser)
implementation(libs.logcat.core)

// Android Logcat Database (Room database and import service)
implementation(libs.logcat.database)

// Android Logcat Export (export database to txt file)
implementation(libs.logcat.export)

// Android Logcat CSV Export (csv export extension)
implementation(libs.logcat.export.csv)

// Android Material3 Compose Logcat UI
implementation(libs.logcat.ui)

Usage

Core (Parser)

The LogcatBinaryParser can be used on an arbitrary input stream to parse Logcat items.

fun main() = runBlocking {
    val process = ProcessBuilder("adb", "logcat", "-B").start()

    LogcatBinaryParser(
        input = BufferedInputStream(process.inputStream)
    ).use { parser ->
        while (true) {
            val item = parser.parseItem()
            println(item)
        }
    }
}

Database and Import

If you want to provide a way to capture and store all Logcat messages of your app and represent them inside your app, you can use the LogcatDatabase and the LogcatImportService.

Start the import service with:

val logcat = Logcat(context)
launch {
    logcat.service.start()
}

Search items via the LogcatSearchDao:

val logcat = Logcat(context)
val pagingSource = logcat.searchDao.getAllPaged()

Export

The logcat items can get exported to a simple txt file.

val logcat = Logcat(context)
launch {
    LogcatExporter(logcat.exportDao).export(output)
}

CSV Export

The logcat items can also get exported as a CSV file.

val logcat = Logcat(context)
launch {
    LogcatExporter(
        dao = logcat.exportDao,
        serializer = LogcatCsvSerializer,
    ).export(output)
}

Material3 Compose UI

A simple Material3 Compose UI can be added that lists all Logcat items and allows to export them.

The LogcatListScreen shows a list of all Logcat items.

LogcatListScreen(
    onItemClick = { navigator.navigate(LogcatDetailsScreen(it.uuid)) },
)

The LogcatDetailsScreen shows the details of a single Logcat item including the full log message.

LogcatDetailsScreen(
    uuid = uuid,
    onUpClick = navigator::navigateUp,
)

Make sure that the the androidx.compose.material3.MaterialTheme is used, as well as the LogcatTheme:

MaterialTheme {
    LogcatTheme {
        Logcat*Screen()
    }
}

License

Copyright 2020 Sven Obser

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

Android Logcat Parser

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages