[Native][tests] Special logger to pass messages directly to Gradle console

This commit is contained in:
Dmitriy Dolovov
2022-05-24 09:56:11 +03:00
committed by Space
parent 1b9768a385
commit 009e7f5c3a
2 changed files with 43 additions and 0 deletions
@@ -0,0 +1,6 @@
handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=INFO
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=%5$s%n
@@ -0,0 +1,37 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.konan.blackboxtest.support.util
import java.util.logging.LogManager
import java.util.logging.Logger
/**
* Helps to log textual messages directly from JUnit test process to Gradle console.
*
* Note: Need to call [initialize] before the first [log] call.
*/
internal object TestLogger {
private const val CONFIG_FILE = "/native-tests-logging.properties"
private val mutex = Any()
private var wrappedLogger: Logger? = null
fun initialize() {
synchronized(mutex) {
check(wrappedLogger == null) { "An attempt to re-initialize ${TestLogger::class.java}" }
val logManager: LogManager = LogManager.getLogManager()
TestLogger::class.java.getResourceAsStream(CONFIG_FILE)?.let(logManager::readConfiguration)
?: error("Test configuration file $CONFIG_FILE not found at the classpath")
wrappedLogger = Logger.getLogger(TestLogger::class.java.name)
}
}
fun log(message: String) {
wrappedLogger?.info(message) ?: error("${TestLogger::class.java} has not been initialized")
}
}