Skip to content

Commit

Permalink
v1.2 support for using KmLogging in libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
ellsworthrw committed Apr 19, 2022
1 parent b7127f9 commit ba30934
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 11 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [yyyy] [name of copyright owner]
Copyright 2020-2022 Reed Ellsworth

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Kotlin Multiplatform Logging <img src="https://upload.wikimedia.org/wikipedia/commons/0/06/Kotlin_Icon.svg" width="30"> <img src="https://upload.wikimedia.org/wikipedia/commons/d/d7/Android_robot.svg" width="30"> <img src="https://upload.wikimedia.org/wikipedia/commons/6/66/Apple_iOS_logo.svg" width="30"> <img src="https://upload.wikimedia.org/wikipedia/commons/6/6a/JavaScript-logo.png" width="30"> <img src="https://upload.wikimedia.org/wikipedia/commons/1/18/OpenJDK_logo.svg" width="80">

[![ver](https://img.shields.io/maven-central/v/org.lighthousegames/logging)](todo)
[![ver](https://img.shields.io/maven-central/v/org.lighthousegames/logging)](https://repo1.maven.org/maven2/org/lighthousegames/logging/)
[![Kotlin](https://img.shields.io/badge/Kotlin-1.5.32-blue.svg?logo=kotlin)](http://kotlinlang.org)
![kmm](https://img.shields.io/badge/Multiplatform-Android%20iOS%20JS%20JVM-blue)
[![License](https://img.shields.io/badge/License-Apache--2.0-blue)](http://www.apache.org/licenses/LICENSE-2.0)
Expand Down Expand Up @@ -76,7 +76,7 @@ class MyClass {
}

companion object {
val log = logging() // or can instantiate KmLog()
val log = logging()
}
}
```
Expand Down
4 changes: 2 additions & 2 deletions logging/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ kotlin {
iosArm64()
iosX64()
iosSimulatorArm64()
js(BOTH) {
js {
browser {
}
}
Expand Down Expand Up @@ -68,7 +68,7 @@ tasks {
}

extra["artifactId"] = "kmlogging"
extra["artifactVersion"] = "1.1.2"
extra["artifactVersion"] = "1.2.0"
extra["libraryName"] = "KmLogging: Kotlin Multiplatform Logging"
extra["libraryDescription"] = "KmLogging is a high performance, extensible and easy to use logging library for Kotlin Multiplatform development"
extra["gitUrl"] = "https://github.com/LighthouseGames/KmLogging"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,14 @@ open class KmLog(tag: String) {
}
}

/**
* Create a logging object. This is the primary entry point for logging and should be called once for each file, class or object.
* For classes a val can be created either as a private member of the class or as a member of the companion object.
* @param tag string to be used instead of the calculated tag based on the class name or file name.
*/
fun logging(tag: String? = null): KmLog {
if (tag != null)
return logFactory.get()?.createKmLog(tag, tag) ?: KmLog(tag)
val (tagCalculated, className) = KmLogging.createTag("KmLog")
val t = tag ?: tagCalculated
return logFactory.get()?.createKmLog(t, className) ?: KmLog(t)
return logFactory.get()?.createKmLog(tagCalculated, className) ?: KmLog(tagCalculated)
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.lighthousegames.logging

/**
* Wrapper for KmLog that allows library modules to also use KmLogging and be able to enable or disable its logging independently
* of the application that is also using KmLogging.
*
* Example usage with code implemented in ChartsLogging.kt
* ```
* object ChartsLogging {
* var enabled = true
* }
*
* fun moduleLogging(tag: String? = null): KmModuleLog {
* // string passed into createTag should be the name of the class that this function is implemented in
* // if it is a top level function then the class name is the file name with Kt appended
* val t = tag ?: KmLogging.createTag("ChartsLoggingKt").first
* return KmModuleLog(logging(t), ChartsLogging::enabled)
* }
* ```
*/
class KmModuleLog(val log: KmLog, val isModuleLogging: () -> Boolean) {

inline fun v(msg: () -> Any?) {
if (isModuleLogging())
log.verbose(msg)
}

inline fun v(tag: String, msg: () -> Any?) {
if (isModuleLogging())
log.verbose(tag, msg)
}

inline fun d(msg: () -> Any?) {
if (isModuleLogging())
log.debug(msg)
}

inline fun d(tag: String, msg: () -> Any?) {
if (isModuleLogging())
log.debug(tag, msg)
}

inline fun i(msg: () -> Any?) {
if (isModuleLogging())
log.info(msg)
}

inline fun i(tag: String, msg: () -> Any?) {
if (isModuleLogging())
log.info(tag, msg)
}

inline fun w(msg: () -> Any?) {
if (isModuleLogging())
log.warn(msg)
}

inline fun w(err: Throwable?, tag: String? = null, msg: () -> Any?) {
if (isModuleLogging())
log.warn(err, tag, msg)
}

inline fun e(msg: () -> Any?) {
if (isModuleLogging())
log.error(msg)
}

inline fun e(err: Throwable?, tag: String? = null, msg: () -> Any?) {
if (isModuleLogging())
log.error(err, tag, msg)
}
}
2 changes: 1 addition & 1 deletion sample/shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ kotlin {
}
}
}
js(LEGACY) {
js {
browser {
}
}
Expand Down
2 changes: 1 addition & 1 deletion sample/webApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dependencies {
}

kotlin {
js(LEGACY) {
js {
browser {
binaries.executable()
commonWebpackConfig {
Expand Down

0 comments on commit ba30934

Please sign in to comment.