Skip to content
This repository was archived by the owner on Oct 20, 2023. It is now read-only.

Commit cb84fc1

Browse files
committed
fix log
1 parent 007a1a5 commit cb84fc1

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

app/src/main/java/io/github/mzdluo123/mirai/android/miraiconsole/MiraiAndroidLogger.kt

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,42 +32,51 @@ fun logException(err: Throwable?) {
3232
MiraiAndroidLogger.error(stringWriter.toString())
3333
}
3434

35-
internal fun processHTMLLog(log: String): String =
36-
log.replace("\n", "<br>")
37-
.replace("<", "&lt;")
38-
.replace(">", "&gt")
39-
.replace("&", "&amp;")
40-
.replace("\"", "&quot")
35+
private val lock = Object()
36+
37+
internal fun pushLog(log: String) {
38+
synchronized(lock) {
39+
logStorage.add(log)
40+
for (i in 0 until BotService.consoleUi.beginBroadcast()) {
41+
try {
42+
BotService.consoleUi.getBroadcastItem(i).newLog(log)
43+
} catch (remoteE: Exception) {
44+
Log.e("MA", remoteE.message ?: "发生错误")
45+
remoteE.printStackTrace()
46+
logException(remoteE)
47+
}
48+
}
49+
BotService.consoleUi.finishBroadcast()
50+
51+
}
52+
}
53+
4154

4255
object MiraiAndroidLogger :
4356
SimpleLogger(LOGGER_IDENTITY, { priority: LogPriority, message: String?, e: Throwable? ->
44-
val log = "[${priority.name}] ${message ?: e}"
45-
val colorLog =
46-
"<font color=\"${LogColor.valueOf(priority.name).color}\">[${priority.name}]</font> ${
47-
processHTMLLog(message ?: "")
48-
}"
49-
57+
e?.printStackTrace()
58+
logException(e)
5059
synchronized(this) {
51-
logStorage.add(colorLog)
52-
logException(e)
53-
for (i in 0 until BotService.consoleUi.beginBroadcast()) {
54-
try {
55-
BotService.consoleUi.getBroadcastItem(i).newLog(colorLog)
56-
} catch (remoteE: Exception) {
57-
Log.e("MA", remoteE.message ?: "发生错误")
58-
remoteE.printStackTrace()
59-
logException(remoteE)
60+
message?.split("\n")?.forEach {
61+
val log = "[${priority.name}] ${it}"
62+
val colorLog =
63+
"<font color=\"${LogColor.valueOf(priority.name).color}\">[${priority.name}]</font><![CDATA[${
64+
it.replace(
65+
"\n",
66+
"\r"
67+
)
68+
}]]>"
69+
pushLog(colorLog)
70+
71+
72+
if (BuildConfig.DEBUG || printToSysLog) {
73+
Log.i("MA", log)
6074
}
6175
}
62-
BotService.consoleUi.finishBroadcast()
6376
}
77+
}
6478

65-
if (BuildConfig.DEBUG || printToSysLog) {
66-
Log.i("MA", log)
67-
e?.printStackTrace()
68-
69-
}
70-
}) {
79+
) {
7180
val logs: MutableList<String>
7281
get() = logStorage.toMutableList()
7382

0 commit comments

Comments
 (0)