Skip to content

Commit

Permalink
Fixed Android tag bug
Browse files Browse the repository at this point in the history
- Fixed bug with long android messages leveraging the wrong tag instance
- Improved test coverage
- Updated dependencies
  • Loading branch information
ToxicBakery committed Nov 22, 2019
1 parent ee225a7 commit dd3282a
Show file tree
Hide file tree
Showing 17 changed files with 256 additions and 195 deletions.
10 changes: 5 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
buildscript {
ext.kotlin_version = '1.3.31'
ext.kotlin_version = '1.3.60'
ext.dokka_version = '0.9.17'
ext.jacoco_version = '0.8.3'
ext.jacoco_version = '0.8.5'
repositories {
mavenLocal()
google()
Expand All @@ -12,16 +12,16 @@ buildscript {
maven { url "https://dl.bintray.com/salomonbrys/gradle-plugins" }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
classpath 'com.android.tools.build:gradle:3.5.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-frontend-plugin:0.0.37"
classpath "org.jetbrains.kotlin:kotlin-frontend-plugin:0.0.45"
classpath "com.github.salomonbrys.gradle.kotlin.js:kotlin-js-gradle-utils:1.0.0"
classpath "org.jetbrains.dokka:dokka-gradle-plugin:$dokka_version"
}
}

plugins {
id "io.gitlab.arturbosch.detekt" version "1.0.0-RC12"
id "io.gitlab.arturbosch.detekt" version "1.1.1"
id 'nl.fabianm.kotlin.plugin.generated' version '1.3.2'
}

Expand Down
17 changes: 10 additions & 7 deletions common/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ plugins {
id 'io.gitlab.arturbosch.detekt'
id 'org.jetbrains.dokka'
id 'jacoco'
id 'nl.fabianm.kotlin.plugin.generated'
}

jacoco {
Expand All @@ -16,7 +15,7 @@ tasks.withType(Test) {
}

android {
compileSdkVersion 28
compileSdkVersion 29
defaultConfig {
minSdkVersion 15
}
Expand Down Expand Up @@ -53,15 +52,19 @@ kotlin {
dependencies {
implementation project(':common')
implementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
implementation "org.mockito:mockito-core:3.1.0"
}
}
}
}

task jacocoTestReportAndroid(type: JacocoReport) {
classDirectories = files("$buildDir/intermediates/packaged-classes/debug")
sourceDirectories = files("src/androidMain/kotlin")
executionData = fileTree(dir: "$buildDir/jacoco", include: "*.exec")
def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*']
def debugTree = fileTree(dir: "$buildDir/tmp/kotlin-classes/debug", excludes: fileFilter)

classDirectories.from(files(debugTree))
sourceDirectories.from(files("src/androidMain/kotlin"))
executionData.from(fileTree(dir: "$buildDir/jacoco", include: "*.exec"))

reports {
xml.enabled = true
Expand Down Expand Up @@ -93,12 +96,12 @@ task dokkaAndroid(type: org.jetbrains.dokka.gradle.DokkaTask) {
}

task dokkaJavadocAndroidJar(type: Jar, dependsOn: dokkaAndroid) {
classifier = 'javadoc'
classifier('javadoc')
from "$buildDir/javadoc/android"
}

task androidSourcesJar(type: Jar) {
classifier = 'sources'
classifier('sources')
from kotlin.sourceSets.androidMain.kotlin.srcDirs
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.toxicbakery.logging

import android.util.Log

/**
* Default Android delegate for logging.
*/
object LogCatDelegate : LogDelegate {

override fun writeLog(level: Int, tag: String, msg: String) {
when (level) {
Arbor.DEBUG -> Log.d(tag, msg)
Arbor.ERROR -> Log.e(tag, msg)
Arbor.INFO -> Log.i(tag, msg)
Arbor.VERBOSE -> Log.v(tag, msg)
Arbor.WARNING -> Log.w(tag, msg)
Arbor.WTF -> Log.wtf(tag, msg)
}
}

override fun writeLog(level: Int, tag: String, msg: String, throwable: Throwable) {
when (level) {
Arbor.DEBUG -> Log.d(tag, msg, throwable)
Arbor.ERROR -> Log.e(tag, msg, throwable)
Arbor.INFO -> Log.i(tag, msg, throwable)
Arbor.VERBOSE -> Log.v(tag, msg, throwable)
Arbor.WARNING -> Log.w(tag, msg, throwable)
Arbor.WTF -> Log.wtf(tag, msg, throwable)
}
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.toxicbakery.logging

import android.os.Build
import android.util.Log
import kotlin.math.min

/**
Expand All @@ -11,7 +10,8 @@ import kotlin.math.min
* the calling code.
*/
class LogCatSeedling(
private val treatAsAndroidN: Boolean = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
private val treatAsAndroidN: Boolean = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N,
private val logDelegate: LogDelegate = LogCatDelegate
) : ISeedling {

private val String.asAndroidTag: String
Expand Down Expand Up @@ -49,7 +49,6 @@ class LogCatSeedling(
throwable: Throwable?,
args: Array<out Any?>?
) {
require(level >= Arbor.DEBUG && level <= Arbor.WTF)
log(
level = level,
tag = tag.asAndroidTag,
Expand All @@ -64,17 +63,18 @@ class LogCatSeedling(
msg: String,
throwable: Throwable?
) = if (throwable == null) {
if (msg.length <= MAX_ANDROID_MSG) writeLog(level, tag, msg)
else msg.splitAndLog(level)
} else msg.splitAndLog(level, throwable)
if (msg.length <= MAX_ANDROID_MSG) logDelegate.writeLog(level, tag, msg)
else msg.splitAndLog(tag, level)
} else msg.splitAndLog(tag, level, throwable)

private fun String.splitAndLog(
tag: String,
level: Int,
throwable: Throwable? = null
) = logCatSplit()
.forEachIndexed { idx, messageChunk ->
if (idx != 0 || throwable == null) writeLog(level, tag, messageChunk)
else writeLog(level, tag, messageChunk, throwable)
if (idx != 0 || throwable == null) logDelegate.writeLog(level, tag, messageChunk)
else logDelegate.writeLog(level, tag, messageChunk, throwable)
}

companion object {
Expand All @@ -90,35 +90,10 @@ class LogCatSeedling(
*/
internal const val MAX_ANDROID_MSG = 4023

@JvmStatic
internal fun writeLog(level: Int, tag: String, msg: String) {
require(level >= Arbor.DEBUG && level <= Arbor.WTF)
when (level) {
Arbor.DEBUG -> Log.d(tag, msg)
Arbor.ERROR -> Log.e(tag, msg)
Arbor.INFO -> Log.i(tag, msg)
Arbor.VERBOSE -> Log.v(tag, msg)
Arbor.WARNING -> Log.w(tag, msg)
Arbor.WTF -> Log.wtf(tag, msg)
}
}

@JvmStatic
internal fun writeLog(level: Int, tag: String, msg: String, throwable: Throwable) {
require(level >= Arbor.DEBUG && level <= Arbor.WTF)
when (level) {
Arbor.DEBUG -> Log.d(tag, msg, throwable)
Arbor.ERROR -> Log.e(tag, msg, throwable)
Arbor.INFO -> Log.i(tag, msg, throwable)
Arbor.VERBOSE -> Log.v(tag, msg, throwable)
Arbor.WARNING -> Log.w(tag, msg, throwable)
Arbor.WTF -> Log.wtf(tag, msg, throwable)
}
}

/**
* Split a log message at the [MAX_ANDROID_MSG] length limit.
*/
@Suppress("LongMethod")
@JvmStatic
internal fun String.logCatSplit(): List<String> = mutableListOf<String>().also { output ->
var leftIndex = 0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.toxicbakery.logging

/**
* Delegate for printing Android logs. This is useful for injecting a new output such as to a file.
*/
interface LogDelegate {

/**
* Write a log with a given level, tag, and message.
*/
fun writeLog(level: Int, tag: String, msg: String)

/**
* Write a log with a given level, tag, message, and a printed throwable exception.
*/
fun writeLog(level: Int, tag: String, msg: String, throwable: Throwable)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.toxicbakery.logging

import org.junit.Test

class LogCatDelegateTest {

@Test
fun writeLog() {
LogCatDelegate.writeLog(Arbor.DEBUG, "tag", "msg")
LogCatDelegate.writeLog(Arbor.ERROR, "tag", "msg")
LogCatDelegate.writeLog(Arbor.INFO, "tag", "msg")
LogCatDelegate.writeLog(Arbor.VERBOSE, "tag", "msg")
LogCatDelegate.writeLog(Arbor.WARNING, "tag", "msg")
LogCatDelegate.writeLog(Arbor.WTF, "tag", "msg")

LogCatDelegate.writeLog(Arbor.DEBUG, "tag", "msg", Exception())
LogCatDelegate.writeLog(Arbor.ERROR, "tag", "msg", Exception())
LogCatDelegate.writeLog(Arbor.INFO, "tag", "msg", Exception())
LogCatDelegate.writeLog(Arbor.VERBOSE, "tag", "msg", Exception())
LogCatDelegate.writeLog(Arbor.WARNING, "tag", "msg", Exception())
LogCatDelegate.writeLog(Arbor.WTF, "tag", "msg", Exception())
}

@Test
fun invalidLevel_withoutException() {
LogCatDelegate.writeLog(0, "tag", "msg")
}

@Test
fun invalidLevel_withException() {
LogCatDelegate.writeLog(0, "tag", "msg", Exception())
}

}
Loading

0 comments on commit dd3282a

Please sign in to comment.