Skip to content

Commit

Permalink
Introduced nop mode
Browse files Browse the repository at this point in the history
  • Loading branch information
devindi committed Aug 13, 2020
1 parent ae8fcaa commit 50feeb4
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 23 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ POM_LICENCE_DIST=repo
POM_DEVELOPER_ID=bolteu
POM_DEVELOPER_NAME=Bolt Technologies OÜ

VERSION_NAME=1.0.1
VERSION_NAME=1.0.2
3 changes: 1 addition & 2 deletions kalev-android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
POM_ARTIFACT_ID=kalev-android
POM_NAME=kalev-android
POM_PACKAGING=aar
POM_DESCRIPTION=New age logging for Android
VERSION_NAME=1.0.1
POM_DESCRIPTION=New age logging for Android
3 changes: 1 addition & 2 deletions kalev-lib/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
POM_ARTIFACT_ID=kalev
POM_NAME=kalev
POM_PACKAGING=jar
POM_DESCRIPTION=New age logging for JVM
VERSION_NAME=1.0.1
POM_DESCRIPTION=New age logging for JVM
32 changes: 24 additions & 8 deletions kalev-lib/src/main/java/eu/bolt/kalev/Kalev.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.bolt.kalev

@Suppress("MemberVisibilityCanBePrivate", "unused")
object Kalev {

const val V = "v"
Expand All @@ -8,11 +9,18 @@ object Kalev {
const val W = "w"
const val E = "e"

/**
* No Operation mode.
* In case of enabled NOP mode Kalev will not create log entries and dispatch them to consumers
*/
var nop = false
private val nopEntry = NopLogEntry

private val consumers = mutableListOf<Kalevipoeg>()

@JvmStatic
fun d(message: String) {
LogEntry().log(message, D)
getEntry().log(message, D)
}

@JvmStatic
Expand All @@ -22,7 +30,7 @@ object Kalev {

@JvmStatic
fun e(message: String) {
LogEntry().log(message, E)
getEntry().log(message, E)
}

@JvmStatic
Expand All @@ -32,7 +40,7 @@ object Kalev {

@JvmStatic
fun i(message: String) {
LogEntry().log(message, I)
getEntry().log(message, I)
}

@JvmStatic
Expand All @@ -42,7 +50,7 @@ object Kalev {

@JvmStatic
fun v(message: String) {
LogEntry().log(message, V)
getEntry().log(message, V)
}

@JvmStatic
Expand All @@ -52,7 +60,7 @@ object Kalev {

@JvmStatic
fun w(message: String) {
LogEntry().log(message, W)
getEntry().log(message, W)
}

@JvmStatic
Expand All @@ -62,24 +70,32 @@ object Kalev {

@JvmStatic
fun with(key: String, value: Any?): LogEntry {
return LogEntry().with(key, value)
return getEntry().with(key, value)
}

@JvmStatic
fun tag(tag: String): LogEntry {
return LogEntry().tag(tag)
return getEntry().tag(tag)
}

@JvmStatic
fun with(error: Throwable): LogEntry {
return LogEntry().with(error)
return getEntry().with(error)
}

@JvmStatic
fun addPoeg(consumer: Kalevipoeg) {
consumers.add(consumer)
}

fun getEntry(): LogEntry {
return if (nop) {
nopEntry
} else {
LogEntry()
}
}

internal fun log(entry: LogEntry) {
consumers.forEach {
it.log(entry)
Expand Down
21 changes: 11 additions & 10 deletions kalev-lib/src/main/java/eu/bolt/kalev/LogEntry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package eu.bolt.kalev

import java.util.Date

class LogEntry {
@Suppress("unused")
open class LogEntry internal constructor() {

var tag: String? = null
private set
Expand All @@ -14,42 +15,42 @@ class LogEntry {
lateinit var message: String
val data = mutableMapOf<String, Any?>()

fun with(key: String, value: Any?): LogEntry {
open fun with(key: String, value: Any?): LogEntry {
data[key] = value
return this
}

fun with(throwable: Throwable): LogEntry {
open fun with(throwable: Throwable): LogEntry {
this.throwable = throwable
return this
}

fun tag(tag: String): LogEntry {
open fun tag(tag: String): LogEntry {
this.tag = tag
return this
}

fun v(message: String) {
open fun v(message: String) {
log(message, Kalev.V)
}

fun d(message: String) {
open fun d(message: String) {
log(message, Kalev.D)
}

fun i(message: String) {
open fun i(message: String) {
log(message, Kalev.I)
}

fun w(message: String) {
open fun w(message: String) {
log(message, Kalev.W)
}

fun e(message: String) {
open fun e(message: String) {
log(message, Kalev.E)
}

internal fun log(message: String, severity: String) {
internal open fun log(message: String, severity: String) {
this.severity = severity
timestamp = Date().time
this.message = message
Expand Down
20 changes: 20 additions & 0 deletions kalev-lib/src/main/java/eu/bolt/kalev/NopLogEntry.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package eu.bolt.kalev

object NopLogEntry : LogEntry() {

override fun with(key: String, value: Any?): LogEntry {
return this
}

override fun with(throwable: Throwable): LogEntry {
return this
}

override fun tag(tag: String): LogEntry {
return this
}

override fun log(message: String, severity: String) {
// do nothing
}
}

0 comments on commit 50feeb4

Please sign in to comment.