Skip to content

Commit

Permalink
🐛 Fix cannot read property isPrefixForAll from YAML #21
Browse files Browse the repository at this point in the history
🔥 Remove output not by mirai logger
⚡ Use launch for logger

close #21
  • Loading branch information
PigeonYuze committed Apr 15, 2023
1 parent 1ee392a commit 8a2ad46
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 30 deletions.
65 changes: 41 additions & 24 deletions src/main/kotlin/com/pigeonyuze/LoggerManager.kt
Original file line number Diff line number Diff line change
@@ -1,72 +1,89 @@
package com.pigeonyuze

import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.*
import net.mamoe.mirai.message.data.ForwardMessageBuilder
import net.mamoe.mirai.message.data.PlainText
import java.text.SimpleDateFormat
import java.util.*
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import kotlin.coroutines.CoroutineContext

@Suppress("UNUSED")
object LoggerManager {
object LoggerManager : CoroutineScope {

private inline val notRun get() = !LoggerConfig.open
override val coroutineContext: CoroutineContext = CoroutineExceptionHandler { _, error ->
loggingError(error)
} + YamlBot.coroutineContext + SupervisorJob(YamlBot.coroutineContext[Job])

private val miraiLogger = YamlBot.logger
private val miraiLogger by lazy { YamlBot.logger }

private const val defaultfrom = "YamlBot"

private val dateFormat: DateTimeFormatter =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")

@JvmOverloads
@JvmStatic
fun loggingError(from: Any = defaultfrom, message: String?) {
if (notRun) return
if (!LoggerConfig.open) return
miraiLogger.error("[$from] $message")
trySendLogMessage(message ?: "")
trySendLogMessage("ERROR", from.toString(), message ?: "")
}

fun loggingError(error: Throwable) {
if (!LoggerConfig.open) return
miraiLogger.error(error)
trySendLogMessage("ERROR", error.javaClass.simpleName, error.message ?: "")
}

@JvmOverloads
@JvmStatic
fun loggingWarn(from: Any = defaultfrom, message: String?) {
if (notRun) return
if (!LoggerConfig.open) return
miraiLogger.warning("[$from] $message")
}

@JvmOverloads
@JvmStatic
fun loggingInfo(from: Any = defaultfrom, message: String?) {
if (notRun) return
if (!LoggerConfig.open) return
miraiLogger.info("[$from] $message")
}

@JvmOverloads
@JvmStatic
fun loggingDebug(from: Any = defaultfrom, message: String?) {
if (notRun) return
if (!LoggerConfig.open) return
miraiLogger.debug("[$from] $message")
}

@JvmOverloads
@JvmStatic
fun loggingTrace(from: Any = defaultfrom, message: String?) {
if (notRun) return
if (!LoggerConfig.open) return
miraiLogger.verbose("[$from] $message")
}

private fun trySendLogMessage(message: String) {
val bot = BotsTool.firstBot
val group = BotsTool.getGroupOrNullJava(LoggerConfig.debugGroup) ?: return
val forwardMessageBuilder = ForwardMessageBuilder(group)
forwardMessageBuilder.add(
bot,
PlainText("${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())} log message\nlevel type = ERROR")
)
forwardMessageBuilder.add(bot, PlainText(message))
runBlocking {
@Suppress("SameParameterValue")
private fun trySendLogMessage(level: String, from: String, message: String) {
if (LoggerConfig.debugGroup == 0L) return
launch {
val group = BotsTool.getGroupOrNull(LoggerConfig.debugGroup) ?: return@launch

val forwardMessageBuilder = ForwardMessageBuilder(group)
.add(
group.bot,
PlainText("${LocalDateTime.now().format(dateFormat)} log message")
)
.add(
group.bot,
PlainText("level type = $level\nfrom = $from\nmessage = $message")
)

group.sendMessage(
forwardMessageBuilder.build()
.copy(title = "Log信息", summary = "level = ERROR", preview = listOf("(●'◡'●)"))
.copy(title = "Log信息", summary = "level = $level", preview = listOf("(●'◡'●)"))
)
}
}

}
}
2 changes: 2 additions & 0 deletions src/main/kotlin/com/pigeonyuze/command/Command.kt
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ sealed interface Command {
return template.execute(args)
}

@Suppress("FunctionName")
private fun Command._initImpl() {
val run by lazy { this.run }
val name by lazy { this.name }
Expand Down Expand Up @@ -518,6 +519,7 @@ sealed interface Command {
* 反之则返回`null`
* */
private inline fun <K> checkCommandName(checkNativeObj: String, shouldBe: String, run: () -> K): K? {
println("$checkNativeObj == $shouldBe , by $isPrefixForAll.")
if (isPrefixForAll) {
if (checkNativeObj.startsWith(shouldBe)) {
return run.invoke()
Expand Down
10 changes: 5 additions & 5 deletions src/main/kotlin/com/pigeonyuze/command/YamlCommandDecoder.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.pigeonyuze.command

import com.pigeonyuze.CommandConfigs
import com.pigeonyuze.*
import com.pigeonyuze.CommandPolymorphism
import com.pigeonyuze.YamlBot
import com.pigeonyuze.YamlBot.reload
import com.pigeonyuze.command.Command.*
import com.pigeonyuze.command.Command.ArgCommand.Type.*
Expand Down Expand Up @@ -93,7 +92,7 @@ object YamlCommandDecoder : PluginDataStorage {
val argsSplit = (commandElement["argsSplit"] as? YamlLiteral)?.content ?: " "
val requestYaml = commandElement["request"] as? YamlMap
val describeYaml = commandElement["describe"] as? YamlMap
val isPrefixForAll = (commandElement["isPrefix"] as? YamlLiteral)?.content?.toBooleanStrictOrNull() ?: true
val isPrefixForAll = (commandElement["isPrefixForAll"] as? YamlLiteral)?.content?.toBooleanStrictOrNull() ?: true
val name = mutableListOf<String>()
for (oneNameElement in nameYaml) {
if (oneNameElement !is YamlLiteral) name.add(oneNameElement.toString())
Expand Down Expand Up @@ -266,14 +265,15 @@ object YamlCommandDecoder : PluginDataStorage {
file.copyTo(file.resolveSibling("${file.name}.${System.currentTimeMillis()}.bak"))
throw e
}

LoggerManager.loggingDebug("load-command","Start read CommandReg.yml ->")
val commands = (yaml as YamlMap)["COMMAND"] as YamlList
val commandList = mutableListOf<CommandPolymorphism>()
for (commandYaml in commands) { //循环尝试反序列化为一个Command类
for ((index,commandYaml) in commands.withIndex()) { //循环尝试反序列化为一个Command类
if (commandYaml !is YamlMap) continue
val command: Command =
readToArgsCommand(commandYaml) ?: readToCommand(commandYaml) ?: readToOnlyCommand(commandYaml)
?: illegalArgument("Cannot initialize object to a command!")
LoggerManager.loggingTrace("load-command","$command #$index")
commandList.add(command.toPolymorphismObject())
}
CommandConfigs.COMMAND = commandList
Expand Down
1 change: 0 additions & 1 deletion src/main/kotlin/com/pigeonyuze/template/Parameter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,6 @@ fun YamlList.asParameter(): Parameter {
}

fun List<String>.asParameter(): Parameter {
println("is this")
return Parameter(this)
}

0 comments on commit 8a2ad46

Please sign in to comment.