Skip to content

Commit

Permalink
Merge pull request #24 from ToxicBakery/feature/extensions
Browse files Browse the repository at this point in the history
Added extensions approach to better support kotlin string concatenation
  • Loading branch information
ToxicBakery authored Mar 6, 2022
2 parents 3f079a2 + be93c18 commit cf65622
Show file tree
Hide file tree
Showing 8 changed files with 347 additions and 36 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ Log tagging is automatic on Android but can be easily overridden.
Arbor.tag("Custom Tag").i("My Log with a tag.")
```

##### Kotlin Extensions
While Arbor supports writing string concatenations, leveraging Kotlin concatenations leads to strings being evaluated regardless of if they will be logged. In release that means CPU cycles may be wasted towards logging that is ultimately never printed. The `arbor` Kotlin extension leverages functions to work around this.

Debug message
```kotlin
arbor { "Debug" }
```

Changing the log level
```kotlin
arbor(LogLevel.E) { "Error" }
```

Levering custom tags via Arbor branches
```kotlin
val tag = Arbor.tag("MyTag")
arbor(branch = tag) { "Custom Tag" }
```

## Install
Arbor is a Kotlin Multiplatform project supporting JavaScript, JVM, and Android platforms.

Expand Down
14 changes: 11 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//file:noinspection GroovyAssignabilityCheck
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension

buildscript {
ext.kotlin_version = '1.6.10'
ext.dokka_version = '1.4.20'
ext.jacoco_version = '0.8.5'
repositories {
mavenLocal()
google()
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
Expand All @@ -22,12 +24,18 @@ plugins {
}

repositories {
mavenLocal()
google()
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
}

// Work around for mac based builds which are only supported in NPM 16 and newer
// https://youtrack.jetbrains.com/issue/KT-49109
rootProject.plugins.withType(Class.forName("org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin")) {
rootProject.extensions.getByType(NodeJsRootExtension.class)
.nodeVersion = "16.0.0"
}

def getGitCommitCount() {
try {
def process = "git rev-list HEAD --count".execute()
Expand All @@ -44,7 +52,7 @@ subprojects {
final buildNumber = getGitCommitCount() ?: "0"

group 'com.ToxicBakery.logging'
version "1.36.$buildNumber" + (isCI && isMaster ? "" : "-SNAPSHOT")
version "1.37.$buildNumber" + (isCI && isMaster ? "" : "-SNAPSHOT")

repositories {
google()
Expand Down
68 changes: 68 additions & 0 deletions common/src/commonMain/kotlin/com/toxicbakery/logging/Arbor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,16 @@ object Arbor {

/**
* Sow a seedling into the forest. Logs will call seedlings in the order they have been sown.
*
* @return true if added, false if previously added
*/
@JvmStatic
fun sow(seedling: ISeedling) = perennial.add(seedling)

/**
* Harvest a seedling from the forest.
*
* @return true if removed
*/
@JvmStatic
fun harvest(seedling: ISeedling) = perennial.remove(seedling)
Expand All @@ -82,6 +86,12 @@ object Arbor {
@JvmStatic
fun d(msg: String) = taglessBranch.d(msg)

/**
* Log a debug message.
*/
@JvmStatic
fun d(msg: () -> String) = taglessBranch.d(msg)

/**
* Log a debug message.
*/
Expand All @@ -107,6 +117,12 @@ object Arbor {
@JvmStatic
fun v(msg: String) = taglessBranch.v(msg)

/**
* Log a verbose message.
*/
@JvmStatic
fun v(msg: () -> String) = taglessBranch.v(msg)

/**
* Log a verbose message.
*/
Expand All @@ -132,6 +148,12 @@ object Arbor {
@JvmStatic
fun i(msg: String) = taglessBranch.i(msg)

/**
* Log an info message.
*/
@JvmStatic
fun i(msg: () -> String) = taglessBranch.i(msg)

/**
* Log an info message.
*/
Expand All @@ -157,6 +179,12 @@ object Arbor {
@JvmStatic
fun w(msg: String) = taglessBranch.w(msg)

/**
* Log a warning message.
*/
@JvmStatic
fun w(msg: () -> String) = taglessBranch.w(msg)

/**
* Log a warning message.
*/
Expand All @@ -182,6 +210,12 @@ object Arbor {
@JvmStatic
fun e(msg: String) = taglessBranch.e(msg)

/**
* Log an error message.
*/
@JvmStatic
fun e(msg: () -> String) = taglessBranch.e(msg)

/**
* Log an error message.
*/
Expand All @@ -207,6 +241,12 @@ object Arbor {
@JvmStatic
fun wtf(msg: String) = taglessBranch.wtf(msg)

/**
* Log a wtf message.
*/
@JvmStatic
fun wtf(msg: () -> String) = taglessBranch.wtf(msg)

/**
* Log a wtf message.
*/
Expand All @@ -227,3 +267,31 @@ object Arbor {
fun wtf(throwable: Throwable, msg: String, vararg args: Any?) = taglessBranch.wtf(throwable, msg, args)

}

enum class LogLevel { D, V, I, W, E, WTF }

private val defaultBranch: Branch = Arbor.tag("2143fef6-18e0-4341-a1de-b043ba5e32cd")

fun arbor(
logLevel: LogLevel = LogLevel.D,
tag: Branch = defaultBranch,
message: () -> String
) = if (tag == defaultBranch) {
when (logLevel) {
LogLevel.D -> Arbor.d(message)
LogLevel.V -> Arbor.v(message)
LogLevel.I -> Arbor.i(message)
LogLevel.W -> Arbor.w(message)
LogLevel.E -> Arbor.e(message)
LogLevel.WTF -> Arbor.wtf(message)
}
} else {
when (logLevel) {
LogLevel.D -> tag.d(message)
LogLevel.V -> tag.v(message)
LogLevel.I -> tag.i(message)
LogLevel.W -> tag.w(message)
LogLevel.E -> tag.e(message)
LogLevel.WTF -> tag.wtf(message)
}
}
Loading

0 comments on commit cf65622

Please sign in to comment.