-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Allow defaults for DI observers
Feat: Scoped DI This is pretty much multi-context support but every context is tied to the root DI so clearing is still easy! Feat: Integrate logging with DI system, tries to create reasonable defaults when a logger isn't registered, deprecate old logging functions. Deps: Kotlin 1.9.23, compose 1.6.1
- Loading branch information
Showing
19 changed files
with
342 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
group=com.mineinabyss | ||
version=0.22 | ||
version=0.23 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
idofront-di/src/commonMain/kotlin/com/mineinabyss/idofront/di/DefaultingModuleObserver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.mineinabyss.idofront.di | ||
|
||
import kotlin.properties.ReadOnlyProperty | ||
import kotlin.reflect.KProperty | ||
|
||
class DefaultingModuleObserver<T>( | ||
val observer: ModuleObserver<T>, | ||
val defaultProvider: () -> T, | ||
) : ReadOnlyProperty<Any?, T> { | ||
val default by lazy { defaultProvider() } | ||
|
||
fun get(): T = observer.getOrNull() ?: default | ||
|
||
override fun getValue(thisRef: Any?, property: KProperty<*>): T { | ||
return get() | ||
} | ||
} | ||
|
||
fun <T> ModuleObserver<T>.default(provider: () -> T): DefaultingModuleObserver<T> { | ||
return DefaultingModuleObserver(this, provider) | ||
} |
8 changes: 8 additions & 0 deletions
8
idofront-di/src/commonMain/kotlin/com/mineinabyss/idofront/di/ScopedDIContext.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.mineinabyss.idofront.di | ||
|
||
import kotlin.reflect.KClass | ||
|
||
class ScopedDIContext( | ||
val simpleName: String, | ||
val byClass: KClass<*>? = null, | ||
) : DIContext() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
idofront-logging/src/main/kotlin/com/mineinabyss/idofront/messaging/ComponentLogger.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.mineinabyss.idofront.messaging | ||
|
||
import co.touchlab.kermit.Logger | ||
import co.touchlab.kermit.Severity | ||
import co.touchlab.kermit.StaticConfig | ||
import com.mineinabyss.idofront.messaging.IdoLogging.errorComp | ||
import com.mineinabyss.idofront.messaging.IdoLogging.successComp | ||
import com.mineinabyss.idofront.textcomponents.miniMsg | ||
import com.mineinabyss.idofront.textcomponents.toPlainText | ||
import net.kyori.adventure.text.ComponentLike | ||
import org.bukkit.plugin.Plugin | ||
|
||
open class ComponentLogger( | ||
staticConfig: StaticConfig, | ||
tag: String, | ||
) : Logger(staticConfig, tag) { | ||
|
||
fun i(message: ComponentLike) { | ||
if (config.minSeverity <= Severity.Info) | ||
logComponent(Severity.Info, message) | ||
} | ||
|
||
fun iMM(message: String) { | ||
i(message.miniMsg()) | ||
} | ||
|
||
fun iSuccess(message: String) { | ||
i(successComp.append(message.miniMsg())) | ||
} | ||
|
||
fun iSuccess(message: ComponentLike) { | ||
i(successComp.append(message)) | ||
} | ||
|
||
fun iFail(message: String) { | ||
i(errorComp.append(message.miniMsg())) | ||
} | ||
|
||
fun iFail(message: ComponentLike) { | ||
i(errorComp.append(message)) | ||
} | ||
|
||
fun v(message: ComponentLike) { | ||
if (config.minSeverity <= Severity.Verbose) | ||
logComponent(Severity.Verbose, message) | ||
} | ||
|
||
fun d(message: ComponentLike) { | ||
if (config.minSeverity <= Severity.Debug) | ||
logComponent(Severity.Debug, message) | ||
} | ||
|
||
fun w(message: ComponentLike) { | ||
if (config.minSeverity <= Severity.Warn) | ||
logComponent(Severity.Warn, message) | ||
} | ||
|
||
fun e(message: ComponentLike) { | ||
if (config.minSeverity <= Severity.Error) | ||
logComponent(Severity.Error, message) | ||
} | ||
|
||
fun a(message: ComponentLike) { | ||
if (config.minSeverity <= Severity.Assert) | ||
logComponent(Severity.Assert, message) | ||
} | ||
|
||
fun logComponent(severity: Severity, message: ComponentLike) { | ||
config.logWriterList.forEach { | ||
if (!it.isLoggable(severity)) return@forEach | ||
if (it is KermitPaperWriter) it.log(severity, message) | ||
else it.log(severity, message.asComponent().toPlainText(), tag, null) | ||
} | ||
} | ||
|
||
companion object { | ||
fun forPlugin(plugin: Plugin, minSeverity: Severity = Severity.Info): ComponentLogger { | ||
return ComponentLogger( | ||
StaticConfig(minSeverity = minSeverity, logWriterList = listOf(KermitPaperWriter(plugin))), | ||
plugin.name | ||
) | ||
} | ||
|
||
fun fallback( | ||
minSeverity: Severity = Severity.Info, | ||
tag: String = "Idofront" | ||
): ComponentLogger { | ||
return ComponentLogger(StaticConfig(minSeverity = minSeverity), tag) | ||
} | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
idofront-logging/src/main/kotlin/com/mineinabyss/idofront/messaging/IdoLogging.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package com.mineinabyss.idofront.messaging | ||
|
||
import com.mineinabyss.idofront.messaging.IdoLogging.ERROR_PREFIX | ||
import com.mineinabyss.idofront.messaging.IdoLogging.SUCCESS_PREFIX | ||
import com.mineinabyss.idofront.messaging.IdoLogging.WARN_PREFIX | ||
import com.mineinabyss.idofront.messaging.IdoLogging.logWithFallback | ||
import com.mineinabyss.idofront.textcomponents.miniMsg | ||
import com.mineinabyss.idofront.textcomponents.toPlainText | ||
import net.kyori.adventure.text.Component | ||
import org.bukkit.Bukkit | ||
import org.bukkit.command.CommandSender | ||
import org.bukkit.command.ConsoleCommandSender | ||
|
||
object IdoLogging { | ||
const val ERROR_PREFIX = "<dark_red><b>\u274C</b><red>" | ||
const val SUCCESS_PREFIX = "<green><b>\u2714</b>" | ||
const val WARN_PREFIX = "<yellow>\u26A0<gray>" | ||
|
||
val successComp = SUCCESS_PREFIX.miniMsg() | ||
val errorComp = ERROR_PREFIX.miniMsg() | ||
val warnComp = WARN_PREFIX.miniMsg() | ||
|
||
@PublishedApi | ||
internal val BUKKIT_LOADED = runCatching { | ||
Bukkit.getConsoleSender() | ||
}.isSuccess | ||
|
||
@PublishedApi | ||
internal val ADVENTURE_LOADED = runCatching { | ||
"<green>Test".miniMsg() | ||
}.isSuccess | ||
|
||
inline fun logWithFallback(message: Any?, printBukkit: (Component) -> Unit) { | ||
if (ADVENTURE_LOADED) { | ||
val messageComponent = message as? Component ?: message.toString().miniMsg() | ||
if (BUKKIT_LOADED) printBukkit(messageComponent) | ||
else println(messageComponent.toPlainText()) | ||
} else { | ||
println(message) | ||
} | ||
} | ||
} | ||
|
||
@Deprecated("Use Plugin.logger().i(...)") | ||
fun logInfo(message: Any?) = | ||
logWithFallback(message) { Bukkit.getConsoleSender().sendMessage(it) } | ||
|
||
@Deprecated("Use Plugin.logger().i(...)") | ||
fun logSuccess(message: Any?) = | ||
logWithFallback("<green>$message") { Bukkit.getConsoleSender().sendMessage(it) } | ||
|
||
@Deprecated("Use Plugin.logger().e(...)") | ||
fun logError(message: Any?) = | ||
logWithFallback(message) { Bukkit.getLogger().severe(it.toPlainText()) } | ||
|
||
@Deprecated("Use Plugin.logger().w(...)") | ||
fun logWarn(message: Any?) = | ||
logWithFallback(message) { Bukkit.getLogger().warning(it.toPlainText()) } | ||
|
||
/** Broadcasts a message to the entire server. */ | ||
fun broadcast(message: Any?) = logWithFallback(message) { Bukkit.getServer().broadcast(it) } | ||
|
||
fun CommandSender.info(message: Any?) = logWithFallback(message, printBukkit = ::sendMessage) | ||
|
||
fun CommandSender.error(message: Any?) { | ||
if (this is ConsoleCommandSender) | ||
logWithFallback(message) { Bukkit.getLogger().severe(it.toPlainText()) } | ||
else info("$ERROR_PREFIX $message") | ||
} | ||
|
||
fun CommandSender.success(message: Any?) { | ||
if (this is ConsoleCommandSender) | ||
logWithFallback("<green>$message") { Bukkit.getConsoleSender().sendMessage(it) } | ||
else info("$SUCCESS_PREFIX $message") | ||
} | ||
|
||
fun CommandSender.warn(message: Any?) { | ||
if (this is ConsoleCommandSender) | ||
logWithFallback(message) { Bukkit.getLogger().warning(it.toPlainText()) } | ||
else info("$WARN_PREFIX $message") | ||
} | ||
|
||
/** | ||
* (Kotlin) Logs a value with an optional string in front of it e.x. | ||
* | ||
* ``` | ||
* val length: Int = "A String".logVal("Name").length.logVal("Its length") | ||
* ``` | ||
* Will print: | ||
* ``` | ||
* Name: A String | ||
* Its length: 8 | ||
* ``` | ||
* | ||
* @param message A string to be placed in front of this value. | ||
* @return Itself. | ||
*/ | ||
fun <T> T.logVal(message: String = ""): T = logWithFallback( | ||
"${if (message == "") "" else "$message: "}$this", | ||
printBukkit = Bukkit.getConsoleSender()::sendMessage | ||
).let { this } | ||
|
||
/** | ||
* Same as [logVal] but uses [broadcast] instead | ||
*/ | ||
fun <T> T.broadcastVal(message: String = ""): T = broadcast("$message$this").let { this } |
5 changes: 5 additions & 0 deletions
5
idofront-logging/src/main/kotlin/com/mineinabyss/idofront/messaging/IdofrontLogger.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.mineinabyss.idofront.messaging | ||
|
||
import com.mineinabyss.idofront.di.DI | ||
|
||
val idofrontLogger by DI.scoped("Idofront").observeLogger() |
36 changes: 36 additions & 0 deletions
36
idofront-logging/src/main/kotlin/com/mineinabyss/idofront/messaging/KermitPaperWriter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.mineinabyss.idofront.messaging | ||
|
||
import co.touchlab.kermit.LogWriter | ||
import co.touchlab.kermit.Severity | ||
import com.mineinabyss.idofront.textcomponents.toPlainText | ||
import net.kyori.adventure.text.ComponentLike | ||
import org.bukkit.Bukkit | ||
import org.bukkit.plugin.Plugin | ||
import java.util.logging.Level | ||
|
||
class KermitPaperWriter(private val plugin: Plugin) : LogWriter() { | ||
override fun log(severity: Severity, message: String, tag: String, throwable: Throwable?) { | ||
plugin.logger.log(severityToLogLevel(severity), message) | ||
throwable?.printStackTrace() | ||
} | ||
|
||
fun log(severity: Severity, message: ComponentLike) { | ||
if (severity >= Severity.Warn) | ||
log(severity, message.asComponent().toPlainText(), "", null) | ||
else | ||
Bukkit.getConsoleSender().sendMessage(message) | ||
} | ||
|
||
companion object { | ||
// Spigot passes the java log level into log4j that's harder to configure, we'll just stick to info level | ||
// and filter on our end | ||
fun severityToLogLevel(severity: Severity): Level = when (severity) { | ||
Severity.Verbose -> Level.INFO | ||
Severity.Debug -> Level.INFO | ||
Severity.Info -> Level.INFO | ||
Severity.Warn -> Level.WARNING | ||
Severity.Error -> Level.SEVERE | ||
Severity.Assert -> Level.SEVERE | ||
} | ||
} | ||
} |
Oops, something went wrong.