[Commonizer] Logging: Implement CommonizerLogLevel and hide verbose output by default

^KT-36679 Fixed
This commit is contained in:
sebastian.sellmair
2021-04-29 14:09:46 +02:00
committed by TeamCityServer
parent 7349ec8f2f
commit b48850c993
14 changed files with 97 additions and 22 deletions

View File

@@ -21,4 +21,4 @@ object DummyLogger : Logger {
println("e: $message")
exitProcess(1)
}
}
}

View File

@@ -224,6 +224,10 @@ internal class PropertiesProvider private constructor(private val project: Proje
val enableCInteropCommonization: Boolean
get() = booleanProperty("kotlin.mpp.enableCInteropCommonization") ?: false
val commonizerLogLevel: String?
get() = property("kotlin.mpp.commonizerLogLevel")
/**
* Enables experimental commonization of 'higher level' shared native source sets
*/

View File

@@ -100,7 +100,8 @@ internal open class CInteropCommonizerTask : AbstractCInteropCommonizerTask() {
inputLibraries = cinteropsForTarget.map { it.libraryFile.get() }.filter { it.exists() }.toSet(),
dependencyLibraries = cinteropsForTarget.flatMap { it.dependencies.files }.map(::NonTargetedCommonizerDependency).toSet()
+ nativeDistributionDependencies(parameters),
outputDirectory = outputDirectory(parameters)
outputDirectory = outputDirectory(parameters),
logLevel = project.commonizerLogLevel
)
}

View File

@@ -0,0 +1,20 @@
/*
* Copyright 2010-2021 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.gradle.targets.native.internal
import org.gradle.api.Project
import org.jetbrains.kotlin.commonizer.CommonizerLogLevel
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider
internal val Project.commonizerLogLevel: CommonizerLogLevel
get() {
PropertiesProvider(this).commonizerLogLevel?.let { logLevelString ->
val matchingLevel = CommonizerLogLevel.values().firstOrNull { logLevel -> logLevel.name.equals(logLevelString, true) }
if (matchingLevel != null) return matchingLevel
}
return if (logger.isInfoEnabled) CommonizerLogLevel.Info else CommonizerLogLevel.Quiet
}

View File

@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.gradle.targets.native.internal
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.tasks.*
import org.jetbrains.kotlin.commonizer.CommonizerLogLevel
import org.jetbrains.kotlin.commonizer.SharedCommonizerTarget
import org.jetbrains.kotlin.commonizer.identityString
import org.jetbrains.kotlin.commonizer.isAncestorOf
@@ -106,6 +107,8 @@ internal open class HierarchicalNativeDistributionCommonizerTask : DefaultTask()
this += getRootOutputDirectory(target).absolutePath
this += "-output-commonizer-target"
this += target.identityString
this += "-log-level"
this += project.commonizerLogLevel.name
}
}
}

View File

@@ -27,7 +27,8 @@ public class CliCommonizer(private val executor: Executor) : Commonizer {
inputLibraries: Set<File>,
dependencyLibraries: Set<CommonizerDependency>,
outputCommonizerTarget: SharedCommonizerTarget,
outputDirectory: File
outputDirectory: File,
logLevel: CommonizerLogLevel
) {
if (inputLibraries.isEmpty()) return
val arguments = mutableListOf<String>().apply {
@@ -39,6 +40,7 @@ public class CliCommonizer(private val executor: Executor) : Commonizer {
if (dependencyLibraries.isNotEmpty()) {
add("-dependency-libraries"); add(dependencyLibraries.joinToString(";"))
}
add("-log-level"); add(logLevel.name.lowercase())
}
executor(arguments)
}

View File

@@ -15,6 +15,7 @@ public interface Commonizer : Serializable {
inputLibraries: Set<File>,
dependencyLibraries: Set<CommonizerDependency>,
outputCommonizerTarget: SharedCommonizerTarget,
outputDirectory: File
outputDirectory: File,
logLevel: CommonizerLogLevel = CommonizerLogLevel.Quiet
)
}

View File

@@ -0,0 +1,11 @@
/*
* Copyright 2010-2021 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.commonizer
public enum class CommonizerLogLevel {
Quiet,
Info
}

View File

@@ -5,24 +5,34 @@
package org.jetbrains.kotlin.commonizer.cli
import org.jetbrains.kotlin.commonizer.CommonizerLogLevel
import org.jetbrains.kotlin.util.Logger
import kotlin.system.exitProcess
internal class CliLoggerAdapter(indentSize: Int = 0) : Logger {
internal class CliLoggerAdapter(
private val level: CommonizerLogLevel,
indentSize: Int = 0
) : Logger {
private val indent = " ".repeat(indentSize)
override fun log(message: String) = printlnIndented(message)
override fun log(message: String) = printlnIndented(message, CommonizerLogLevel.Info)
override fun warning(message: String) = printlnIndented("Warning: $message")
override fun warning(message: String) = printlnIndented("Warning: $message", *CommonizerLogLevel.values())
override fun error(message: String) = fatal(message)
override fun fatal(message: String): Nothing {
printlnIndented("Error: $message\n")
printlnIndented("Error: $message\n", *CommonizerLogLevel.values())
exitProcess(1)
}
private fun printlnIndented(text: String) =
if (indent.isEmpty()) println(text)
else text.split('\n').forEach { println(indent + it) }
private fun printlnIndented(text: String, vararg levels: CommonizerLogLevel) {
if (level in levels) {
if (indent.isEmpty()) println(text)
else text.split('\n').forEach {
println(indent + it)
}
}
}
}

View File

@@ -0,0 +1,18 @@
/*
* Copyright 2010-2021 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.commonizer.cli
import org.jetbrains.kotlin.commonizer.CommonizerLogLevel
internal object LogLevelOptionType : OptionType<CommonizerLogLevel>("log-level", "{quiet, info}", false) {
override fun parse(rawValue: String, onError: (reason: String) -> Nothing): Option<CommonizerLogLevel> {
return when (rawValue.lowercase().trim()) {
"quiet" -> Option(this, CommonizerLogLevel.Quiet)
"info" -> Option(this, CommonizerLogLevel.Info)
else -> Option(this, CommonizerLogLevel.Quiet)
}
}
}

View File

@@ -17,7 +17,7 @@ internal abstract class Task(private val options: Collection<Option<*>>) : Compa
INFORMATIONAL,
COMMONIZATION(
prologue = null,
epilogue = "\n",
epilogue = null,
logEachStep = true
)
}

View File

@@ -29,7 +29,8 @@ internal enum class TaskType(
"Boolean (default false);\nwhether to copy Kotlin/Native endorsed libraries to the destination",
mandatory = false
),
StatsTypeOptionType
StatsTypeOptionType,
LogLevelOptionType,
),
::NativeDistributionCommonize
),
@@ -52,6 +53,7 @@ internal enum class TaskType(
InputLibrariesOptionType,
DependencyLibrariesOptionType,
OutputCommonizerTargetOptionType,
LogLevelOptionType
),
::NativeKlibCommonize
)

View File

@@ -50,12 +50,14 @@ internal class NativeKlibCommonize(options: Collection<Option<*>>) : Task(option
val dependencyLibraries = getOptional<List<CommonizerDependency>, DependencyLibrariesOptionType>().orEmpty()
val outputCommonizerTarget = compatGetOutputTarget()
val statsType = getOptional<StatsType, StatsTypeOptionType> { it == "log-stats" } ?: StatsType.NONE
val logLevel = getOptional<CommonizerLogLevel, LogLevelOptionType>() ?: CommonizerLogLevel.Quiet
val konanTargets = outputCommonizerTarget.konanTargets
val commonizerTargets = konanTargets.map(::CommonizerTarget)
val progressLogger = ProgressLogger(CliLoggerAdapter(2))
val libraryLoader = DefaultNativeLibraryLoader(progressLogger)
val logger = ProgressLogger(CliLoggerAdapter(logLevel, 2))
val libraryLoader = DefaultNativeLibraryLoader(logger)
val statsCollector = StatsCollector(statsType, commonizerTargets)
val repository = FilesRepository(targetLibraries.toSet(), libraryLoader)
@@ -74,7 +76,7 @@ internal class NativeKlibCommonize(options: Collection<Option<*>>) : Task(option
CommonizerDependencyRepository(dependencyLibraries.toSet(), libraryLoader),
resultsConsumer = resultsConsumer,
statsCollector = statsCollector,
progressLogger = progressLogger
progressLogger = logger
).run()
statsCollector?.writeTo(FileStatsOutput(destination, statsType.name.lowercase()))
@@ -97,9 +99,10 @@ internal class NativeDistributionCommonize(options: Collection<Option<*>>) : Tas
val copyStdlib = getOptional<Boolean, BooleanOptionType> { it == "copy-stdlib" } ?: false
val copyEndorsedLibs = getOptional<Boolean, BooleanOptionType> { it == "copy-endorsed-libs" } ?: false
val statsType = getOptional<StatsType, StatsTypeOptionType> { it == "log-stats" } ?: StatsType.NONE
val logLevel = getOptional<CommonizerLogLevel, LogLevelOptionType>() ?: CommonizerLogLevel.Quiet
val progressLogger = ProgressLogger(CliLoggerAdapter(2))
val libraryLoader = DefaultNativeLibraryLoader(progressLogger)
val logger = ProgressLogger(CliLoggerAdapter(logLevel, 2))
val libraryLoader = DefaultNativeLibraryLoader(logger)
val repository = KonanDistributionRepository(distribution, outputTarget.konanTargets, libraryLoader)
val statsCollector = StatsCollector(statsType, outputTarget.withAllAncestors().toList())
@@ -112,8 +115,7 @@ internal class NativeDistributionCommonize(options: Collection<Option<*>>) : Tas
}
val descriptionSuffix = estimateLibrariesCount(repository, outputTarget.allLeaves()).let { " ($it items)" }
val description = "${logPrefix}Preparing commonized Kotlin/Native libraries for $outputTarget$descriptionSuffix"
println(description)
logger.log("${logPrefix}Preparing commonized Kotlin/Native libraries for $outputTarget$descriptionSuffix")
LibraryCommonizer(
outputTarget = outputTarget,
@@ -121,7 +123,7 @@ internal class NativeDistributionCommonize(options: Collection<Option<*>>) : Tas
dependencies = StdlibRepository(distribution, libraryLoader),
resultsConsumer = resultsConsumer,
statsCollector = statsCollector,
progressLogger = progressLogger
progressLogger = logger
).run()
statsCollector?.writeTo(FileStatsOutput(destination, statsType.name.lowercase()))

View File

@@ -6,10 +6,11 @@
package org.jetbrains.kotlin.commonizer.utils
import org.jetbrains.kotlin.commonizer.cli.CliLoggerAdapter
import org.jetbrains.kotlin.commonizer.CommonizerLogLevel
import org.jetbrains.kotlin.util.Logger
class ProgressLogger(
private val wrapped: Logger = CliLoggerAdapter(0),
private val wrapped: Logger = CliLoggerAdapter(CommonizerLogLevel.Info, 0),
private val indent: Int = 0,
) : Logger by wrapped {
private val clockMark = ResettableClockMark()