Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add opportunity to setup debug antilog levels #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import java.io.StringWriter
import java.util.regex.Pattern
import kotlin.math.min

actual class DebugAntilog actual constructor(private val defaultTag: String) : Antilog() {
actual class DebugAntilog actual constructor(
private val defaultTag: String,
private val logLevelChecker: (LogLevel, String?) -> Boolean
) : Antilog() {

companion object {
private const val MAX_LOG_LENGTH = 4000
Expand All @@ -17,6 +20,8 @@ actual class DebugAntilog actual constructor(private val defaultTag: String) : A

private val anonymousClass = Pattern.compile("(\\$\\d+)+$")

override fun isEnable(priority: LogLevel, tag: String?): Boolean = logLevelChecker(priority, tag)

override fun performLog(
priority: LogLevel,
tag: String?,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
package io.github.aakira.napier

expect class DebugAntilog(defaultTag: String = "app") : Antilog
import kotlin.js.JsName
import kotlin.jvm.JvmName

expect class DebugAntilog(
defaultTag: String = "app",
logLevelChecker: (LogLevel, String?) -> Boolean
) : Antilog


@JvmName("DebugAntilogFromLogLevels")
@JsName("DebugAntilogFromLogLevels")
fun DebugAntilog(
defaultTag: String = "app",
enabledLevels: Array<LogLevel> = LogLevel.values()
) = DebugAntilog(defaultTag) { level, _ -> level in enabledLevels }

fun DebugAntilog(
defaultTag: String = "app",
enabledLevels: Iterable<LogLevel>
) = DebugAntilog(defaultTag, enabledLevels.toList().toTypedArray())

fun DebugAntilog(
defaultTag: String = "app",
vararg enabledLevels: LogLevel
): DebugAntilog {
@Suppress("UNCHECKED_CAST")
return DebugAntilog(defaultTag, enabledLevels as Array<LogLevel>)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@ private const val CALL_STACK_INDEX = 8
actual class DebugAntilog(
private val defaultTag: String = "app",
private val coroutinesSuffix: Boolean = true,
private val logLevelChecker: (LogLevel, String?) -> Boolean = { _, _ -> true },
) : Antilog() {
actual constructor(defaultTag: String) : this(defaultTag, coroutinesSuffix = true)
actual constructor(
defaultTag: String,
logLevelChecker: (LogLevel, String?) -> Boolean
) : this(
defaultTag,
coroutinesSuffix = true,
logLevelChecker = logLevelChecker
)

var crashAssert = false

Expand All @@ -27,6 +35,8 @@ actual class DebugAntilog(
LogLevel.ASSERT to "💞 ASSERT"
)

override fun isEnable(priority: LogLevel, tag: String?): Boolean = logLevelChecker(priority, tag)

override fun performLog(
priority: LogLevel,
tag: String?,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package io.github.aakira.napier

actual class DebugAntilog actual constructor(private val defaultTag: String) : Antilog() {
actual class DebugAntilog actual constructor(
private val defaultTag: String,
private val logLevelChecker: (LogLevel, String?) -> Boolean
) : Antilog() {

override fun isEnable(priority: LogLevel, tag: String?): Boolean = logLevelChecker(priority, tag)

override fun performLog(
priority: LogLevel,
Expand Down
14 changes: 12 additions & 2 deletions napier/src/jvmMain/kotlin/io/github/aakira/napier/DebugAntilog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@ import java.util.regex.Pattern

actual class DebugAntilog(
private val defaultTag: String = "app",
private val handler: List<Handler> = listOf()
private val handler: List<Handler> = listOf(),
private val logLevelChecker: (LogLevel, String?) -> Boolean = { _, _ -> true }
) : Antilog() {
actual constructor(defaultTag: String) : this(defaultTag, handler = listOf())
actual constructor(
defaultTag: String,
logLevelChecker: (LogLevel, String?) -> Boolean
) : this(
defaultTag,
handler = listOf(),
logLevelChecker = logLevelChecker
)

companion object {
private const val CALL_STACK_INDEX = 8
Expand Down Expand Up @@ -43,6 +51,8 @@ actual class DebugAntilog(
LogLevel.ASSERT to "[ASSERT]"
)

override fun isEnable(priority: LogLevel, tag: String?): Boolean = logLevelChecker(priority, tag)

override fun performLog(
priority: LogLevel,
tag: String?,
Expand Down