Skip to content

Commit

Permalink
[Gradle, JS] LogType on Enum
Browse files Browse the repository at this point in the history
#KT-38109 fixed
#KT-38286 fixed
  • Loading branch information
ilgonmic committed Apr 16, 2020
1 parent fa4003b commit 5969f1d
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.gradle.api.tasks.testing.TestResult.ResultType.*
import org.gradle.internal.operations.OperationIdentifier
import org.gradle.process.internal.ExecHandle
import org.jetbrains.kotlin.gradle.logging.kotlinDebug
import org.jetbrains.kotlin.gradle.targets.js.LogType
import org.jetbrains.kotlin.gradle.testing.KotlinTestFailure
import org.slf4j.Logger
import java.text.ParseException
Expand Down Expand Up @@ -85,7 +86,7 @@ internal open class TCServiceMessagesClient(
}
}
is TestSuiteFinished -> close(message.ts, getSuiteName(message))
is Message -> printNonTestOutput(message.text, message.attributes["type"])
is Message -> printNonTestOutput(message.text, LogType.byValueOrNull(message.attributes["type"]))
else -> Unit
}

Expand Down Expand Up @@ -115,7 +116,7 @@ internal open class TCServiceMessagesClient(
afterMessage = false
}

protected open fun printNonTestOutput(text: String, type: String? = null) {
protected open fun printNonTestOutput(text: String, type: LogType? = null) {
print(text)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class TeamCityMessageCommonClient(

override fun serviceMessage(message: ServiceMessage) {
when (message) {
is Message -> printMessage(message.text, message.attributes["type"])
is Message -> printMessage(message.text, LogType.byValueOrNull(message.attributes["type"]))
}
}

Expand All @@ -37,23 +37,22 @@ class TeamCityMessageCommonClient(
.joinToString("\n")
}

private fun printMessage(text: String, type: String?) {
private fun printMessage(text: String, type: LogType?) {
val value = text.trimEnd()
progressLogger.progress(value)

var actualType = type
stackTraceProcessor.process(text) { line ->
actualType = ERROR
if (line != null) {
printMessage(line, actualType)
}
val inStackTrace = stackTraceProcessor.process(text) { line, logType ->
log.processLogMessage(line, logType)
errors.add(line.clearAnsiColor())
}

when (actualType) {
ERROR, WARN -> errors.add(value.clearAnsiColor())
if (inStackTrace) return

when (type) {
LogType.ERROR, LogType.WARN -> errors.add(value.clearAnsiColor())
}

actualType?.let { log.processLogMessage(value, it) }
type?.let { log.processLogMessage(value, it) }
}

override fun regularText(text: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ package org.jetbrains.kotlin.gradle.targets.js
internal class TeamCityMessageStackTraceProcessor {
private var firstLine: String? = null

fun process(text: String, firstLineAction: (String?) -> Unit) {
firstLine = if (text.trim().startsWith("at ")) {
// firstLineAction can have side effects
// that's why important to call it even with null line
firstLineAction(firstLine)
null
fun process(text: String, action: (String, LogType) -> Unit): Boolean {
return if (text.trim().startsWith("at ")) {
firstLine?.let {
action(it, LogType.ERROR)
}
firstLine = null
action(text, LogType.ERROR)
true
} else {
text
firstLine = text
false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import org.slf4j.Logger as SlfLogger

internal fun SlfLogger.processLogMessage(
message: String,
type: String
type: LogType
) {
processLogMessageInternal(
message = message,
Expand All @@ -26,7 +26,7 @@ internal fun SlfLogger.processLogMessage(

internal fun GradleLogger.processLogMessage(
message: String,
type: String
type: LogType
) {
processLogMessageInternal(
message = message,
Expand All @@ -40,27 +40,36 @@ internal fun GradleLogger.processLogMessage(

private fun processLogMessageInternal(
message: String,
type: String,
type: LogType,
error: (text: String) -> Unit,
warn: (text: String) -> Unit,
info: (text: String) -> Unit,
debug: (text: String) -> Unit
) {
val nonColoredMessage = message.clearAnsiColor()
when (type.toLowerCase()) {
WARN -> {
when (type) {
LogType.WARN -> {
warn(nonColoredMessage)
}
ERROR -> {
LogType.ERROR -> {
error(nonColoredMessage)
}
INFO, LOG -> info(nonColoredMessage)
DEBUG -> debug(nonColoredMessage)
LogType.INFO, LogType.LOG -> info(nonColoredMessage)
LogType.DEBUG -> debug(nonColoredMessage)
}
}

internal const val ERROR = "error"
internal const val WARN = "warn"
internal const val INFO = "info"
internal const val DEBUG = "debug"
internal const val LOG = "log"
enum class LogType(val value: String) {
ERROR("error"),
WARN("warn"),
INFO("info"),
DEBUG("debug"),
LOG("log");

companion object {
fun byValueOrNull(value: String?): LogType? {
if (value == null) return null
return values().singleOrNull { it.value == value }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.gradle.process.ProcessForkOptions
import org.jetbrains.kotlin.gradle.internal.testing.TCServiceMessagesClient
import org.jetbrains.kotlin.gradle.internal.testing.TCServiceMessagesClientSettings
import org.jetbrains.kotlin.gradle.internal.testing.TCServiceMessagesTestExecutionSpec
import org.jetbrains.kotlin.gradle.targets.js.LogType
import org.slf4j.Logger

internal open class JSServiceMessagesTestExecutionSpec(
Expand Down Expand Up @@ -37,7 +38,7 @@ internal open class JSServiceMessagesClient(
settings: TCServiceMessagesClientSettings,
log: Logger
) : TCServiceMessagesClient(results, settings, log) {
override fun printNonTestOutput(text: String, type: String?) {
override fun printNonTestOutput(text: String, type: LogType?) {
if (log.isDebugEnabled) {
log.debug(text)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,28 +430,29 @@ class KotlinKarma(override val compilation: KotlinJsCompilation) :

private var stackTraceProcessor = TeamCityMessageStackTraceProcessor()

override fun printNonTestOutput(text: String, type: String?) {
override fun printNonTestOutput(text: String, type: LogType?) {
val value = text.trimEnd()
progressLogger.progress(value)

parseConsole(value, type)
}

private fun parseConsole(text: String, type: String?) {
private fun parseConsole(text: String, type: LogType?) {

var actualType = type
stackTraceProcessor.process(text) { line ->
actualType = ERROR
line?.let { parseConsole(it, actualType) }
val inStackTrace = stackTraceProcessor.process(text) { line, logType ->
log.processLogMessage(line, logType)
}

if (inStackTrace) return

val launcherMessage = KARMA_MESSAGE.matchEntire(text)

val actualText = if (launcherMessage != null) {
val (logLevel, message) = launcherMessage.destructured
actualType = logLevel.toLowerCase()
actualType = LogType.byValueOrNull(logLevel.toLowerCase())
when (actualType) {
WARN, ERROR -> {
LogType.WARN, LogType.ERROR -> {
processFailedBrowsers(text)
}
}
Expand Down

0 comments on commit 5969f1d

Please sign in to comment.