mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-17 19:35:00 -04:00
it: disable async logging
This commit is contained in:
parent
7a0f54ed30
commit
1271b7f762
@ -30,6 +30,7 @@ internal object MinosoftSIT {
|
|||||||
|
|
||||||
@BeforeSuite
|
@BeforeSuite
|
||||||
fun setup() {
|
fun setup() {
|
||||||
|
Log.ASYNC_LOGGING = false
|
||||||
disableGC()
|
disableGC()
|
||||||
Log.log(LogMessageType.OTHER, LogLevels.INFO) { "Setting up integration tests...." }
|
Log.log(LogMessageType.OTHER, LogLevels.INFO) { "Setting up integration tests...." }
|
||||||
initAssetsManager()
|
initAssetsManager()
|
||||||
|
@ -12,8 +12,7 @@
|
|||||||
*/
|
*/
|
||||||
package de.bixilon.minosoft.util.logging
|
package de.bixilon.minosoft.util.logging
|
||||||
|
|
||||||
import com.google.errorprone.annotations.DoNotCall
|
import de.bixilon.kutil.time.TimeUtil.millis
|
||||||
import de.bixilon.kutil.time.TimeUtil
|
|
||||||
import de.bixilon.minosoft.config.StaticConfiguration
|
import de.bixilon.minosoft.config.StaticConfiguration
|
||||||
import de.bixilon.minosoft.config.profile.profiles.other.OtherProfileSelectEvent
|
import de.bixilon.minosoft.config.profile.profiles.other.OtherProfileSelectEvent
|
||||||
import de.bixilon.minosoft.data.text.BaseComponent
|
import de.bixilon.minosoft.data.text.BaseComponent
|
||||||
@ -34,9 +33,10 @@ import kotlin.contracts.contract
|
|||||||
|
|
||||||
@OptIn(ExperimentalContracts::class)
|
@OptIn(ExperimentalContracts::class)
|
||||||
object Log {
|
object Log {
|
||||||
private val MINOSOFT_START_TIME = TimeUtil.millis
|
var ASYNC_LOGGING = true
|
||||||
|
private val MINOSOFT_START_TIME = millis()
|
||||||
private val TIME_FORMAT = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
|
private val TIME_FORMAT = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
|
||||||
private val LOG_QUEUE = LinkedBlockingQueue<MessageToSend>()
|
private val QUEUE = LinkedBlockingQueue<QueuedMessage>()
|
||||||
private val SYSTEM_ERR_STREAM = System.err
|
private val SYSTEM_ERR_STREAM = System.err
|
||||||
private val SYSTEM_OUT_STREAM = System.out
|
private val SYSTEM_OUT_STREAM = System.out
|
||||||
private var levels: Map<LogMessageType, LogLevels>? = null
|
private var levels: Map<LogMessageType, LogLevels>? = null
|
||||||
@ -53,57 +53,60 @@ object Log {
|
|||||||
}
|
}
|
||||||
Thread({
|
Thread({
|
||||||
while (true) {
|
while (true) {
|
||||||
val messageToSend = LOG_QUEUE.take()
|
QUEUE.take().print()
|
||||||
try {
|
|
||||||
val message = BaseComponent()
|
|
||||||
val messageColor = messageToSend.logMessageType.colorMap[messageToSend.level] ?: messageToSend.logMessageType.defaultColor
|
|
||||||
message += if (RunConfiguration.LOG_RELATIVE_TIME) {
|
|
||||||
TextComponent("[${TimeUtil.millis - MINOSOFT_START_TIME}] ")
|
|
||||||
} else {
|
|
||||||
TextComponent("[${TIME_FORMAT.format(messageToSend.time)}] ")
|
|
||||||
}
|
|
||||||
message += TextComponent("[${messageToSend.thread.name}] ")
|
|
||||||
message += TextComponent("[${messageToSend.logMessageType}] ").let {
|
|
||||||
if (RunConfiguration.LOG_COLOR_TYPE) {
|
|
||||||
it.color(messageColor)
|
|
||||||
} else {
|
|
||||||
it
|
|
||||||
}
|
|
||||||
}
|
|
||||||
message += TextComponent("[${messageToSend.level}] ").let {
|
|
||||||
if (RunConfiguration.LOG_COLOR_LEVEL) {
|
|
||||||
it.color(messageToSend.level.levelColors)
|
|
||||||
} else {
|
|
||||||
it
|
|
||||||
}
|
|
||||||
}
|
|
||||||
messageToSend.additionalPrefix?.let {
|
|
||||||
message += it
|
|
||||||
}
|
|
||||||
if (RunConfiguration.LOG_COLOR_MESSAGE) {
|
|
||||||
messageToSend.message.setFallbackColor(messageColor)
|
|
||||||
}
|
|
||||||
|
|
||||||
val stream = if (messageToSend.level.error) {
|
|
||||||
SYSTEM_ERR_STREAM
|
|
||||||
} else {
|
|
||||||
SYSTEM_OUT_STREAM
|
|
||||||
}
|
|
||||||
|
|
||||||
val prefix = message.ansiColoredMessage.removeSuffix("\u001b[0m") // reset suffix
|
|
||||||
for (line in messageToSend.message.ansiColoredMessage.lineSequence()) {
|
|
||||||
stream.println(prefix + line)
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (exception: Throwable) {
|
|
||||||
SYSTEM_ERR_STREAM.println("Can not send log message $messageToSend!")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}, "Log").start()
|
}, "Log").start()
|
||||||
|
|
||||||
GlobalEventMaster.register(CallbackEventListener.of<OtherProfileSelectEvent> { this.levels = it.profile.log.levels })
|
GlobalEventMaster.register(CallbackEventListener.of<OtherProfileSelectEvent> { this.levels = it.profile.log.levels })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun QueuedMessage.print() {
|
||||||
|
try {
|
||||||
|
val message = BaseComponent()
|
||||||
|
val color = this.type.colorMap[this.level] ?: this.type.defaultColor
|
||||||
|
message += if (RunConfiguration.LOG_RELATIVE_TIME) {
|
||||||
|
TextComponent("[${millis() - MINOSOFT_START_TIME}] ")
|
||||||
|
} else {
|
||||||
|
TextComponent("[${TIME_FORMAT.format(this.time)}] ")
|
||||||
|
}
|
||||||
|
message += TextComponent("[${this.thread.name}] ")
|
||||||
|
message += TextComponent("[${this.type}] ").let {
|
||||||
|
if (RunConfiguration.LOG_COLOR_TYPE) {
|
||||||
|
it.color(color)
|
||||||
|
} else {
|
||||||
|
it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
message += TextComponent("[${this.level}] ").let {
|
||||||
|
if (RunConfiguration.LOG_COLOR_LEVEL) {
|
||||||
|
it.color(this.level.levelColors)
|
||||||
|
} else {
|
||||||
|
it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.prefix?.let {
|
||||||
|
message += it
|
||||||
|
}
|
||||||
|
if (RunConfiguration.LOG_COLOR_MESSAGE) {
|
||||||
|
this.message.setFallbackColor(color)
|
||||||
|
}
|
||||||
|
|
||||||
|
val stream = if (this.level.error) {
|
||||||
|
SYSTEM_ERR_STREAM
|
||||||
|
} else {
|
||||||
|
SYSTEM_OUT_STREAM
|
||||||
|
}
|
||||||
|
|
||||||
|
val prefix = message.ansiColoredMessage.removeSuffix("\u001b[0m") // reset suffix
|
||||||
|
for (line in this.message.ansiColoredMessage.lineSequence()) {
|
||||||
|
stream.println(prefix + line)
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (exception: Throwable) {
|
||||||
|
SYSTEM_ERR_STREAM.println("Can not send log message $this!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private fun skipLogging(type: LogMessageType, level: LogLevels): Boolean {
|
private fun skipLogging(type: LogMessageType, level: LogLevels): Boolean {
|
||||||
if (RunConfiguration.VERBOSE_LOGGING) {
|
if (RunConfiguration.VERBOSE_LOGGING) {
|
||||||
@ -116,9 +119,6 @@ object Log {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@DoNotCall
|
|
||||||
@JvmOverloads
|
|
||||||
@JvmStatic
|
|
||||||
fun log(type: LogMessageType, level: LogLevels = LogLevels.INFO, additionalPrefix: ChatComponent? = null, message: Any?, vararg formatting: Any) {
|
fun log(type: LogMessageType, level: LogLevels = LogLevels.INFO, additionalPrefix: ChatComponent? = null, message: Any?, vararg formatting: Any) {
|
||||||
if (skipLogging(type, level)) {
|
if (skipLogging(type, level)) {
|
||||||
return
|
return
|
||||||
@ -145,14 +145,22 @@ object Log {
|
|||||||
else -> ChatComponent.of(message, ignoreJson = true)
|
else -> ChatComponent.of(message, ignoreJson = true)
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_QUEUE += MessageToSend(
|
QueuedMessage(
|
||||||
message = formattedMessage,
|
message = formattedMessage,
|
||||||
time = TimeUtil.millis,
|
time = millis(),
|
||||||
logMessageType = type,
|
type = type,
|
||||||
level = level,
|
level = level,
|
||||||
thread = Thread.currentThread(),
|
thread = Thread.currentThread(),
|
||||||
additionalPrefix = additionalPrefix,
|
prefix = additionalPrefix,
|
||||||
)
|
).queue()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun QueuedMessage.queue() {
|
||||||
|
if (!ASYNC_LOGGING) {
|
||||||
|
this.print()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
QUEUE += this
|
||||||
}
|
}
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Minosoft
|
* Minosoft
|
||||||
* Copyright (C) 2021 Moritz Zwerger
|
* Copyright (C) 2020-2022 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.
|
||||||
*
|
*
|
||||||
@ -15,11 +15,11 @@ package de.bixilon.minosoft.util.logging
|
|||||||
|
|
||||||
import de.bixilon.minosoft.data.text.ChatComponent
|
import de.bixilon.minosoft.data.text.ChatComponent
|
||||||
|
|
||||||
data class MessageToSend(
|
data class QueuedMessage(
|
||||||
val message: ChatComponent,
|
val message: ChatComponent,
|
||||||
val time: Long,
|
val time: Long,
|
||||||
val logMessageType: LogMessageType,
|
val type: LogMessageType,
|
||||||
val level: LogLevels,
|
val level: LogLevels,
|
||||||
val thread: Thread,
|
val thread: Thread,
|
||||||
val additionalPrefix: ChatComponent? = null,
|
val prefix: ChatComponent? = null,
|
||||||
)
|
)
|
Loading…
x
Reference in New Issue
Block a user