From 4d3a9e7cc52217e2db60bc5aa43bb0ca819f03c6 Mon Sep 17 00:00:00 2001 From: Him188 Date: Wed, 30 Jun 2021 23:25:50 +0800 Subject: [PATCH] Add `SynchronizedStdoutLogger` for tests --- .../kotlin/test/initPlatform.android.kt | 5 ---- .../framework/SynchronizedStdoutLogger.kt | 24 +++++++++++++++++++ .../kotlin/test/initPlatform.common.kt | 6 +++++ 3 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 mirai-core/src/commonTest/kotlin/network/framework/SynchronizedStdoutLogger.kt diff --git a/mirai-core/src/androidTest/kotlin/test/initPlatform.android.kt b/mirai-core/src/androidTest/kotlin/test/initPlatform.android.kt index 8c292c56b..1b76e62cd 100644 --- a/mirai-core/src/androidTest/kotlin/test/initPlatform.android.kt +++ b/mirai-core/src/androidTest/kotlin/test/initPlatform.android.kt @@ -20,11 +20,6 @@ internal actual fun initPlatform() { } private val init: Unit by lazy { - MiraiLogger.setDefaultLoggerCreator { - @Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") - net.mamoe.mirai.internal.utils.StdoutLogger(it) - } - if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) != null) { Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME) } diff --git a/mirai-core/src/commonTest/kotlin/network/framework/SynchronizedStdoutLogger.kt b/mirai-core/src/commonTest/kotlin/network/framework/SynchronizedStdoutLogger.kt new file mode 100644 index 000000000..e47618ea9 --- /dev/null +++ b/mirai-core/src/commonTest/kotlin/network/framework/SynchronizedStdoutLogger.kt @@ -0,0 +1,24 @@ +/* + * Copyright 2019-2021 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +package net.mamoe.mirai.internal.network.framework + +import kotlinx.atomicfu.locks.withLock +import java.util.concurrent.locks.ReentrantLock + +private val lock = ReentrantLock() + +@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "CANNOT_OVERRIDE_INVISIBLE_MEMBER") +internal class SynchronizedStdoutLogger(override val identity: String?) : net.mamoe.mirai.internal.utils.StdoutLogger( + identity) { + + override val output: (String) -> Unit = { str -> + lock.withLock { println(str) } + } +} \ No newline at end of file diff --git a/mirai-core/src/commonTest/kotlin/test/initPlatform.common.kt b/mirai-core/src/commonTest/kotlin/test/initPlatform.common.kt index 74db0e1e8..56db8f562 100644 --- a/mirai-core/src/commonTest/kotlin/test/initPlatform.common.kt +++ b/mirai-core/src/commonTest/kotlin/test/initPlatform.common.kt @@ -10,6 +10,8 @@ package net.mamoe.mirai.internal.test import net.mamoe.mirai.IMirai +import net.mamoe.mirai.internal.network.framework.SynchronizedStdoutLogger +import net.mamoe.mirai.utils.MiraiLogger import org.junit.jupiter.api.Test import org.junit.jupiter.api.Timeout import java.util.concurrent.TimeUnit @@ -24,6 +26,10 @@ abstract class AbstractTest { init { initPlatform() + MiraiLogger.setDefaultLoggerCreator { + SynchronizedStdoutLogger(it) + } + System.setProperty("mirai.debug.network.packet.logger", "true") System.setProperty("mirai.debug.network.state.observer.logging", "true") System.setProperty("mirai.debug.network.show.all.components", "true")