From 879587ada76b78344f805b77b3f13087e76a225f Mon Sep 17 00:00:00 2001 From: akkkihi Date: Tue, 7 Jun 2022 17:26:11 +0400 Subject: [PATCH] Logger created --- gradle.properties | 2 +- .../kotlin/project_group/Library.kt | 7 ---- .../kotlin/project_group/LibraryTest.kt | 14 ------- lib/src/main/AndroidManifest.xml | 1 - {lib => logger}/build.gradle | 0 logger/src/commonMain/kotlin/Logger.kt | 40 +++++++++++++++++++ logger/src/jsMain/kotlin/ActualLogger.kt | 28 +++++++++++++ logger/src/jvmMain/kotlin/ActualLogger.kt | 26 ++++++++++++ logger/src/main/AndroidManifest.xml | 1 + logger/src/main/kotlin/ActualLogger.kt | 21 ++++++++++ settings.gradle | 4 +- 11 files changed, 119 insertions(+), 25 deletions(-) delete mode 100644 lib/src/commonMain/kotlin/project_group/Library.kt delete mode 100644 lib/src/commonTest/kotlin/project_group/LibraryTest.kt delete mode 100644 lib/src/main/AndroidManifest.xml rename {lib => logger}/build.gradle (100%) create mode 100644 logger/src/commonMain/kotlin/Logger.kt create mode 100644 logger/src/jsMain/kotlin/ActualLogger.kt create mode 100644 logger/src/jvmMain/kotlin/ActualLogger.kt create mode 100644 logger/src/main/AndroidManifest.xml create mode 100644 logger/src/main/kotlin/ActualLogger.kt diff --git a/gradle.properties b/gradle.properties index 1759e8f..cd06544 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,6 +8,6 @@ android.enableJetifier=true # Project data -group=project_group +group=dev.akkihi version=0.0.1 android_code_version=1 diff --git a/lib/src/commonMain/kotlin/project_group/Library.kt b/lib/src/commonMain/kotlin/project_group/Library.kt deleted file mode 100644 index 424d112..0000000 --- a/lib/src/commonMain/kotlin/project_group/Library.kt +++ /dev/null @@ -1,7 +0,0 @@ -package project_group - -class Library { - fun someLibraryMethod(): Boolean { - return true - } -} diff --git a/lib/src/commonTest/kotlin/project_group/LibraryTest.kt b/lib/src/commonTest/kotlin/project_group/LibraryTest.kt deleted file mode 100644 index 84b573a..0000000 --- a/lib/src/commonTest/kotlin/project_group/LibraryTest.kt +++ /dev/null @@ -1,14 +0,0 @@ -/* - * This Kotlin source file was generated by the Gradle 'init' task. - */ -package project_group - -import kotlin.test.Test -import kotlin.test.assertTrue - -class LibraryTest { - @Test fun testSomeLibraryMethod() { - val classUnderTest = Library() - assertTrue(classUnderTest.someLibraryMethod(), "someLibraryMethod should return 'true'") - } -} diff --git a/lib/src/main/AndroidManifest.xml b/lib/src/main/AndroidManifest.xml deleted file mode 100644 index 03c9554..0000000 --- a/lib/src/main/AndroidManifest.xml +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/lib/build.gradle b/logger/build.gradle similarity index 100% rename from lib/build.gradle rename to logger/build.gradle diff --git a/logger/src/commonMain/kotlin/Logger.kt b/logger/src/commonMain/kotlin/Logger.kt new file mode 100644 index 0000000..2359fed --- /dev/null +++ b/logger/src/commonMain/kotlin/Logger.kt @@ -0,0 +1,40 @@ +package truth.simple.kmp.logger.common + + +enum class LogLevel { + VERBOSE, + INFO, + WARNING, + ERROR, + ASSERT, + DEBUG +} + +interface Logger { + fun log (level: LogLevel, message: String, tag: String? = null, throwable: Throwable? = null) + companion object : Logger { + var DEFAULT: Logger = Logger("app") + override fun log(level: LogLevel, message: String, tag: String?, throwable: Throwable?) = DEFAULT.log(level, message, tag, throwable) + + operator fun invoke (log: (level: LogLevel, message: String, tag: String?, throwable: Throwable?) -> Unit) = CallbackLogger(log) + } +} + +class CallbackLogger( + private val logger: (level: LogLevel, message: String, tag: String?, throwable: Throwable?) -> Unit +) : Logger { + override fun log(level: LogLevel, message: String, tag: String?, throwable: Throwable?) = logger(level, message, tag, throwable) +} + + +expect fun Logger( + defaultTag: String, + filter: (l: LogLevel, m: String, t: String, Throwable?) -> Boolean +) : Logger + +fun Logger( + defaultTag: String, + levels: Set = LogLevel.values().toSet() +): Logger = Logger (defaultTag) { l, _, _, _ -> + l in levels +} diff --git a/logger/src/jsMain/kotlin/ActualLogger.kt b/logger/src/jsMain/kotlin/ActualLogger.kt new file mode 100644 index 0000000..5e4497f --- /dev/null +++ b/logger/src/jsMain/kotlin/ActualLogger.kt @@ -0,0 +1,28 @@ +package truth.simple.kmp.logger.common + +fun Logger( + messageFormatter: (l: LogLevel, m: String, t: String?, Throwable?) -> String, + filter: (l: LogLevel, m: String, t: String?, Throwable?) -> Boolean +) = Logger { l, m, t, e -> + if (!filter(l, m, t, e)) return@Logger + val text = messageFormatter(l,m,t,e) + when (l) { + LogLevel.VERBOSE, + LogLevel.INFO -> console.info(text, e) + LogLevel.WARNING -> console.warn(text, e) + LogLevel.ERROR, + LogLevel.ASSERT -> console.error(text, e) + LogLevel.DEBUG -> console.log(text, e) + } + +} + +actual fun Logger( + defaultTag: String, + filter: (l: LogLevel, m: String, t: String, Throwable?) -> Boolean +): Logger { + return Logger( + { l, m, t, e -> "[$l] ${t ?: defaultTag} - $m" }, + { l, m, t, e -> filter(l, m, t ?: defaultTag, e) } + ) +} \ No newline at end of file diff --git a/logger/src/jvmMain/kotlin/ActualLogger.kt b/logger/src/jvmMain/kotlin/ActualLogger.kt new file mode 100644 index 0000000..f471eab --- /dev/null +++ b/logger/src/jvmMain/kotlin/ActualLogger.kt @@ -0,0 +1,26 @@ +package truth.simple.kmp.logger.common + +import java.util.logging.Level + +actual fun Logger( + defaultTag: String, + filter: (l: LogLevel, m: String, t: String, Throwable?) -> Boolean +): Logger { + val logger = java.util.logging.Logger.getLogger(defaultTag) + return Logger { l, m, t, e -> + if (!filter(l, m, t ?: defaultTag, e)) return@Logger + val currentLogger = t ?.let { java.util.logging.Logger.getLogger(t) } ?: logger + currentLogger.log( + when(l) { + LogLevel.VERBOSE -> Level.FINEST + LogLevel.INFO -> Level.INFO + LogLevel.WARNING -> Level.WARNING + LogLevel.ERROR -> Level.SEVERE + LogLevel.ASSERT -> Level.OFF + LogLevel.DEBUG -> Level.ALL + }, + m, + e + ) + } +} \ No newline at end of file diff --git a/logger/src/main/AndroidManifest.xml b/logger/src/main/AndroidManifest.xml new file mode 100644 index 0000000..8c316c2 --- /dev/null +++ b/logger/src/main/AndroidManifest.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/logger/src/main/kotlin/ActualLogger.kt b/logger/src/main/kotlin/ActualLogger.kt new file mode 100644 index 0000000..ec2bed2 --- /dev/null +++ b/logger/src/main/kotlin/ActualLogger.kt @@ -0,0 +1,21 @@ +package truth.simple.kmp.logger.common + +import android.util.Log + +actual fun Logger( + defaultTag: String, + filter: (l: LogLevel, m: String, t: String, Throwable?) -> Boolean +): Logger { + return Logger { l, m, t, e -> + val tag = t ?: defaultTag + if (!filter(l, m, t ?: defaultTag, e)) return@Logger + when(l) { + LogLevel.VERBOSE -> Log.v(tag, m, e) + LogLevel.INFO -> Log.i(tag, m, e) + LogLevel.WARNING -> Log.w(tag, m, e) + LogLevel.ERROR -> Log.e(tag, m, e) + LogLevel.ASSERT -> Log.wtf(tag, m, e) + LogLevel.DEBUG -> Log.d(tag, m, e) + } + } +} \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 4e2023f..baf3abb 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,7 +1,7 @@ -rootProject.name = 'project_name' +rootProject.name = 'truth_simple_kmp_logger' String[] includes = [ - ":lib" + ":logger" ]