mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-10 16:01:50 -04:00
cleanup some function overloading in logging
This commit is contained in:
parent
e6fe8135c0
commit
74e52823f5
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Minosoft
|
* Minosoft
|
||||||
* Copyright (C) 2020-2023 Moritz Zwerger
|
* Copyright (C) 2020-2024 Moritz Zwerger
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
||||||
*
|
*
|
||||||
@ -167,7 +167,7 @@ class PlayConnection(
|
|||||||
ChatTextPositions.HOTBAR -> "[HOTBAR] "
|
ChatTextPositions.HOTBAR -> "[HOTBAR] "
|
||||||
else -> ""
|
else -> ""
|
||||||
}
|
}
|
||||||
Log.log(LogMessageType.CHAT_IN, level = if (it.message.type.position == ChatTextPositions.HOTBAR) LogLevels.VERBOSE else LogLevels.INFO, additionalPrefix = ChatComponent.of(additionalPrefix)) { it.message.text }
|
Log.log(LogMessageType.CHAT_IN, level = if (it.message.type.position == ChatTextPositions.HOTBAR) LogLevels.VERBOSE else LogLevels.INFO, prefix = ChatComponent.of(additionalPrefix)) { it.message.text }
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,12 +29,8 @@ import java.io.PrintWriter
|
|||||||
import java.io.StringWriter
|
import java.io.StringWriter
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
import java.util.concurrent.LinkedBlockingQueue
|
import java.util.concurrent.LinkedBlockingQueue
|
||||||
import kotlin.contracts.ExperimentalContracts
|
|
||||||
import kotlin.contracts.InvocationKind
|
|
||||||
import kotlin.contracts.contract
|
|
||||||
|
|
||||||
|
|
||||||
@OptIn(ExperimentalContracts::class)
|
|
||||||
object Log {
|
object Log {
|
||||||
var ASYNC_LOGGING = true
|
var ASYNC_LOGGING = true
|
||||||
private val MINOSOFT_START_TIME = millis()
|
private val MINOSOFT_START_TIME = millis()
|
||||||
@ -119,43 +115,72 @@ object Log {
|
|||||||
return setLevel.ordinal < level.ordinal
|
return setLevel.ordinal < level.ordinal
|
||||||
}
|
}
|
||||||
|
|
||||||
fun log(type: LogMessageType, level: LogLevels = LogLevels.INFO, additionalPrefix: ChatComponent? = null, message: Any?, vararg formatting: Any) {
|
private fun formatMessage(message: Any?, vararg formatting: Any) = when (message) {
|
||||||
if (skipLogging(type, level)) {
|
is ChatComponent -> message
|
||||||
return
|
is TextFormattable -> ChatComponent.of(message.toText())
|
||||||
|
is Throwable -> {
|
||||||
|
val stringWriter = StringWriter()
|
||||||
|
message.printStackTrace(PrintWriter(stringWriter))
|
||||||
|
ChatComponent.of(stringWriter.toString(), ignoreJson = true)
|
||||||
}
|
}
|
||||||
val formattedMessage = when (message) {
|
|
||||||
is ChatComponent -> message
|
|
||||||
is TextFormattable -> ChatComponent.of(message.toText())
|
|
||||||
is Throwable -> {
|
|
||||||
val stringWriter = StringWriter()
|
|
||||||
message.printStackTrace(PrintWriter(stringWriter))
|
|
||||||
ChatComponent.of(stringWriter.toString(), ignoreJson = true)
|
|
||||||
}
|
|
||||||
|
|
||||||
is String -> {
|
|
||||||
if (message.isBlank()) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if (formatting.isNotEmpty()) {
|
|
||||||
ChatComponent.of(message.format(*formatting), ignoreJson = true)
|
|
||||||
} else {
|
|
||||||
ChatComponent.of(message, ignoreJson = true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
is String -> when {
|
||||||
|
message.isBlank() -> ChatComponent.EMPTY
|
||||||
|
formatting.isNotEmpty() -> ChatComponent.of(message.format(*formatting), ignoreJson = true)
|
||||||
else -> ChatComponent.of(message, ignoreJson = true)
|
else -> ChatComponent.of(message, ignoreJson = true)
|
||||||
}
|
}
|
||||||
|
|
||||||
QueuedMessage(
|
else -> ChatComponent.of(message, ignoreJson = true)
|
||||||
message = formattedMessage,
|
|
||||||
time = millis(),
|
|
||||||
type = type,
|
|
||||||
level = level,
|
|
||||||
thread = Thread.currentThread(),
|
|
||||||
prefix = additionalPrefix,
|
|
||||||
).queue()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun logInternal(type: LogMessageType, level: LogLevels, prefix: ChatComponent?, message: Any?, vararg formatting: Any) {
|
||||||
|
val formatted = formatMessage(message, *formatting)
|
||||||
|
if (formatted.length <= 0) return
|
||||||
|
|
||||||
|
QueuedMessage(message = formatted, time = millis(), type = type, level = level, thread = Thread.currentThread(), prefix = prefix).queue()
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun log(type: LogMessageType, level: LogLevels, prefix: ChatComponent?, message: Any?, vararg formatting: Any) {
|
||||||
|
if (skipLogging(type, level)) return
|
||||||
|
logInternal(type, level, prefix, message, formatting)
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun log(type: LogMessageType, level: LogLevels, prefix: ChatComponent?, message: Any?) {
|
||||||
|
if (skipLogging(type, level)) return
|
||||||
|
logInternal(type, level, prefix, message)
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun log(type: LogMessageType, level: LogLevels, message: Any?) {
|
||||||
|
if (skipLogging(type, level)) return
|
||||||
|
logInternal(type, level, null, message)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline fun log(type: LogMessageType, level: LogLevels, prefix: ChatComponent?, builder: () -> Any?, vararg formatting: Any) {
|
||||||
|
if (skipLogging(type, level)) return
|
||||||
|
logInternal(type, level, prefix, builder.invoke(), *formatting)
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun log(type: LogMessageType, level: LogLevels, prefix: ChatComponent?, builder: () -> Any?) {
|
||||||
|
if (skipLogging(type, level)) return
|
||||||
|
logInternal(type, level, prefix, builder.invoke())
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun log(type: LogMessageType, level: LogLevels, builder: () -> Any?) {
|
||||||
|
if (skipLogging(type, level)) return
|
||||||
|
logInternal(type, level, null, builder.invoke())
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun log(type: LogMessageType, builder: () -> Any?) {
|
||||||
|
if (skipLogging(type, LogLevels.INFO)) return
|
||||||
|
logInternal(type, LogLevels.INFO, null, builder.invoke())
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private fun QueuedMessage.queue() {
|
private fun QueuedMessage.queue() {
|
||||||
if (!ASYNC_LOGGING) {
|
if (!ASYNC_LOGGING) {
|
||||||
this.print()
|
this.print()
|
||||||
@ -164,38 +189,6 @@ object Log {
|
|||||||
QUEUE += this
|
QUEUE += this
|
||||||
}
|
}
|
||||||
|
|
||||||
@JvmStatic
|
|
||||||
inline fun log(type: LogMessageType, level: LogLevels = LogLevels.INFO, additionalPrefix: ChatComponent? = null, messageBuilder: () -> Any?) {
|
|
||||||
contract {
|
|
||||||
callsInPlace(messageBuilder, InvocationKind.AT_MOST_ONCE)
|
|
||||||
}
|
|
||||||
if (skipLogging(type, level)) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
log(type, level, additionalPrefix, messageBuilder.invoke())
|
|
||||||
}
|
|
||||||
|
|
||||||
@JvmStatic
|
|
||||||
inline fun log(type: LogMessageType, level: LogLevels, messageBuilder: () -> Any?) {
|
|
||||||
contract {
|
|
||||||
callsInPlace(messageBuilder, InvocationKind.AT_MOST_ONCE)
|
|
||||||
}
|
|
||||||
log(type, level = level, additionalPrefix = null, messageBuilder = messageBuilder)
|
|
||||||
}
|
|
||||||
|
|
||||||
inline fun log(type: LogMessageType, level: LogLevels, message: Any?) {
|
|
||||||
log(type, level, null, message)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@JvmStatic
|
|
||||||
fun log(type: LogMessageType, messageBuilder: () -> Any?) {
|
|
||||||
contract {
|
|
||||||
callsInPlace(messageBuilder, InvocationKind.AT_MOST_ONCE)
|
|
||||||
}
|
|
||||||
log(type, additionalPrefix = null, messageBuilder = messageBuilder)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun await() {
|
fun await() {
|
||||||
while (this.QUEUE.isNotEmpty()) {
|
while (this.QUEUE.isNotEmpty()) {
|
||||||
Thread.sleep(1)
|
Thread.sleep(1)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user