diff options
author | Ignat Beresnev <ignat.beresnev@jetbrains.com> | 2023-01-31 18:38:09 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-31 18:38:09 +0100 |
commit | fe360f7fadc7173be3ae170778a93ebd01962149 (patch) | |
tree | 25ea1be7e75ffd3c3235cd8ab4109bb26c5b2ad0 | |
parent | eb54d69a5cd8e27b1235a23b9098e285c408cb23 (diff) | |
download | dokka-fe360f7fadc7173be3ae170778a93ebd01962149.tar.gz dokka-fe360f7fadc7173be3ae170778a93ebd01962149.tar.bz2 dokka-fe360f7fadc7173be3ae170778a93ebd01962149.zip |
Make implementations of DokkaLogger thread-safe (#2827)
-rw-r--r-- | core/src/main/kotlin/DokkaBootstrapImpl.kt | 17 | ||||
-rw-r--r-- | core/src/main/kotlin/utilities/DokkaLogging.kt | 18 | ||||
-rw-r--r-- | core/test-api/src/main/kotlin/testApi/logger/TestLogger.kt | 16 | ||||
-rw-r--r-- | runners/maven-plugin/src/main/kotlin/MavenDokkaLogger.kt | 17 |
4 files changed, 51 insertions, 17 deletions
diff --git a/core/src/main/kotlin/DokkaBootstrapImpl.kt b/core/src/main/kotlin/DokkaBootstrapImpl.kt index 114bade7..d2e138f0 100644 --- a/core/src/main/kotlin/DokkaBootstrapImpl.kt +++ b/core/src/main/kotlin/DokkaBootstrapImpl.kt @@ -1,6 +1,7 @@ package org.jetbrains.dokka import org.jetbrains.dokka.utilities.DokkaLogger +import java.util.concurrent.atomic.AtomicInteger import java.util.function.BiConsumer @@ -11,8 +12,16 @@ import java.util.function.BiConsumer class DokkaBootstrapImpl : DokkaBootstrap { class DokkaProxyLogger(val consumer: BiConsumer<String, String>) : DokkaLogger { - override var warningsCount: Int = 0 - override var errorsCount: Int = 0 + private val warningsCounter = AtomicInteger() + private val errorsCounter = AtomicInteger() + + override var warningsCount: Int + get() = warningsCounter.get() + set(value) = warningsCounter.set(value) + + override var errorsCount: Int + get() = errorsCounter.get() + set(value) = errorsCounter.set(value) override fun debug(message: String) { consumer.accept("debug", message) @@ -27,11 +36,11 @@ class DokkaBootstrapImpl : DokkaBootstrap { } override fun warn(message: String) { - consumer.accept("warn", message).also { warningsCount++ } + consumer.accept("warn", message).also { warningsCounter.incrementAndGet() } } override fun error(message: String) { - consumer.accept("error", message).also { errorsCount++ } + consumer.accept("error", message).also { errorsCounter.incrementAndGet() } } } diff --git a/core/src/main/kotlin/utilities/DokkaLogging.kt b/core/src/main/kotlin/utilities/DokkaLogging.kt index 0302b8a2..80c762a3 100644 --- a/core/src/main/kotlin/utilities/DokkaLogging.kt +++ b/core/src/main/kotlin/utilities/DokkaLogging.kt @@ -1,5 +1,7 @@ package org.jetbrains.dokka.utilities +import java.util.concurrent.atomic.AtomicInteger + interface DokkaLogger { var warningsCount: Int var errorsCount: Int @@ -40,8 +42,16 @@ class DokkaConsoleLogger( val minLevel: LoggingLevel = LoggingLevel.DEBUG, private val emitter: MessageEmitter = MessageEmitter.consoleEmitter ) : DokkaLogger { - override var warningsCount: Int = 0 - override var errorsCount: Int = 0 + private val warningsCounter = AtomicInteger() + private val errorsCounter = AtomicInteger() + + override var warningsCount: Int + get() = warningsCounter.get() + set(value) = warningsCounter.set(value) + + override var errorsCount: Int + get() = errorsCounter.get() + set(value) = errorsCounter.set(value) override fun debug(message: String) { if (shouldBeDisplayed(LoggingLevel.DEBUG)) emitter(message) @@ -59,14 +69,14 @@ class DokkaConsoleLogger( if (shouldBeDisplayed(LoggingLevel.WARN)) { emitter("WARN: $message") } - warningsCount++ + warningsCounter.incrementAndGet() } override fun error(message: String) { if (shouldBeDisplayed(LoggingLevel.ERROR)) { emitter("ERROR: $message") } - errorsCount++ + errorsCounter.incrementAndGet() } private fun shouldBeDisplayed(messageLevel: LoggingLevel): Boolean = messageLevel.index >= minLevel.index diff --git a/core/test-api/src/main/kotlin/testApi/logger/TestLogger.kt b/core/test-api/src/main/kotlin/testApi/logger/TestLogger.kt index 0bc66a2b..e70200a7 100644 --- a/core/test-api/src/main/kotlin/testApi/logger/TestLogger.kt +++ b/core/test-api/src/main/kotlin/testApi/logger/TestLogger.kt @@ -1,24 +1,28 @@ package org.jetbrains.dokka.testApi.logger import org.jetbrains.dokka.utilities.DokkaLogger +import java.util.Collections +/* + * Even in tests it be used in a concurrent environment, so needs to be thread safe + */ class TestLogger(private val logger: DokkaLogger) : DokkaLogger { override var warningsCount: Int by logger::warningsCount override var errorsCount: Int by logger::errorsCount - private var _debugMessages = mutableListOf<String>() + private var _debugMessages = synchronizedMutableListOf<String>() val debugMessages: List<String> get() = _debugMessages.toList() - private var _infoMessages = mutableListOf<String>() + private var _infoMessages = synchronizedMutableListOf<String>() val infoMessages: List<String> get() = _infoMessages.toList() - private var _progressMessages = mutableListOf<String>() + private var _progressMessages = synchronizedMutableListOf<String>() val progressMessages: List<String> get() = _progressMessages.toList() - private var _warnMessages = mutableListOf<String>() + private var _warnMessages = synchronizedMutableListOf<String>() val warnMessages: List<String> get() = _warnMessages.toList() - private var _errorMessages = mutableListOf<String>() + private var _errorMessages = synchronizedMutableListOf<String>() val errorMessages: List<String> get() = _errorMessages.toList() override fun debug(message: String) { @@ -45,4 +49,6 @@ class TestLogger(private val logger: DokkaLogger) : DokkaLogger { _errorMessages.add(message) logger.error(message) } + + private fun <T> synchronizedMutableListOf(): MutableList<T> = Collections.synchronizedList(mutableListOf()) } diff --git a/runners/maven-plugin/src/main/kotlin/MavenDokkaLogger.kt b/runners/maven-plugin/src/main/kotlin/MavenDokkaLogger.kt index 4b5f4fa9..ac8ed376 100644 --- a/runners/maven-plugin/src/main/kotlin/MavenDokkaLogger.kt +++ b/runners/maven-plugin/src/main/kotlin/MavenDokkaLogger.kt @@ -2,14 +2,23 @@ package org.jetbrains.dokka.maven import org.apache.maven.plugin.logging.Log import org.jetbrains.dokka.utilities.DokkaLogger +import java.util.concurrent.atomic.AtomicInteger class MavenDokkaLogger(val log: Log) : DokkaLogger { - override var warningsCount: Int = 0 - override var errorsCount: Int = 0 + private val warningsCounter = AtomicInteger() + private val errorsCounter = AtomicInteger() + + override var warningsCount: Int + get() = warningsCounter.get() + set(value) = warningsCounter.set(value) + + override var errorsCount: Int + get() = errorsCounter.get() + set(value) = errorsCounter.set(value) override fun debug(message: String) = log.debug(message) override fun info(message: String) = log.info(message) override fun progress(message: String) = log.info(message) - override fun warn(message: String) = log.warn(message).also { warningsCount++ } - override fun error(message: String) = log.error(message).also { errorsCount++ } + override fun warn(message: String) = log.warn(message).also { warningsCounter.incrementAndGet() } + override fun error(message: String) = log.error(message).also { errorsCounter.incrementAndGet() } } |