Skip to content

Commit

Permalink
add improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
buenaflor committed Sep 13, 2023
1 parent d606322 commit 479a9c3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.giancarlobuenaflor.kflogger

import kotlin.test.Test
val logger = KFluentLogger.forEnclosingClass()

class KFluentLoggerTest {
@Test
fun testNotCrashing() {
val logger = KFluentLogger.forEnclosingClass()
logger.atInfo().log("Hello, world! %s", "test")
logger.atWarning().log("Hello, world!")
logger.atSevere().log("Hello, world!")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ public actual class KSimpleLoggerBackend actual constructor(public val logger: K

public actual override fun log(data: KLogData) {
val templateContext = data.getTemplateContext()
data.getTemplateContext()?.getParser()
if (templateContext != null) {
if (logger.isLoggable(data.getLevel())) {
log(data.getLevel().toOsLogType(), templateContext.message.formatArgs(data.getArguments()))
}
} else {
log(data.getLevel().toOsLogType(), data.getLiteralArgument().toString())
if (logger.isLoggable(data.getLevel())) {
log(data.getLevel().toOsLogType(), data.getLiteralArgument().toString())
}
}
}

private fun log(osLogSeverity: UByte, message: String) {
// TODO: KFlogger implement MessageFormatting so we can support arguments as well
_os_log_internal(__dso_handle.ptr, logger.osLogger, osLogSeverity, message)
}

Expand Down Expand Up @@ -57,7 +57,7 @@ public class OSLogSubsystemAppender(private val subsystem: String) {
}
}

public fun String.formatArgs(args: Array<out Any?>?): String {
private fun String.formatArgs(args: Array<out Any?>?): String {
if (args == null || args.isEmpty()) {
return this
}
Expand Down Expand Up @@ -104,19 +104,6 @@ private fun String.parseFormatSpecifier(): String {
return "%$specifierChar"
}

private fun String.parseFormatSpecifier(pos: Int): String {
if (pos == -1 || pos == length - 1) {
throw IllegalArgumentException("Invalid format string: $this")
}

val specifierChar = this[pos + 1]
if (specifierChar != 's' && specifierChar != 'd' && specifierChar != 'f') {
throw IllegalArgumentException("Invalid format string: $this")
}

return "%$specifierChar"
}

private fun formatArgument(arg: Any?, formatSpecifier: String): Any {
if (arg == null) {
return "null"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public class NSThreadBasedCallerFinder private constructor() : KPlatformLogCalle
private fun findCallerNameOf(target: Klass<*>): String {
val name = target.kClass.qualifiedName
checkNotNull(name) { "Logger class has no name" }
val callstackSymbols = NSThread.callStackSymbols
val callstackSymbols = NSThread.callStackSymbols as List<String>
check(callstackSymbols.size > 1) { "Callstack is too short" }
val firstIndex =
findIndexOfStringContainingSubstring(
callstackSymbols as List<String>, target.kClass.qualifiedName + ".")
val firstIndex = callstackSymbols.indexOfFirst {
it.contains(target.kClass.qualifiedName + ".")
}
val stackframeString = callstackSymbols[firstIndex + 1]
val stackFrameSignature = stackframeString.substringAfter("kfun:").substringBefore("#")
val lastIndexOfDot = stackFrameSignature.lastIndexOf(".")
Expand All @@ -30,12 +30,9 @@ public class NSThreadBasedCallerFinder private constructor() : KPlatformLogCalle
stackTrace: List<String>,
substring: String
): Int {
for ((index, line) in stackTrace.withIndex()) {
if (line.contains(substring)) {
return index
}
return stackTrace.indexOfFirst {
it.contains(substring)
}
return -1
}

override fun findLoggingClass(loggerClass: Klass<out KAbstractLogger<*>>): String {
Expand Down

0 comments on commit 479a9c3

Please sign in to comment.