[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
@@ -21,4 +21,4 @@ object DummyLogger : Logger {
println("e: $message") println("e: $message")
exitProcess(1) exitProcess(1)
} }
} }
@@ -224,6 +224,10 @@ internal class PropertiesProvider private constructor(private val project: Proje
val enableCInteropCommonization: Boolean val enableCInteropCommonization: Boolean
get() = booleanProperty("kotlin.mpp.enableCInteropCommonization") ?: false get() = booleanProperty("kotlin.mpp.enableCInteropCommonization") ?: false
val commonizerLogLevel: String?
get() = property("kotlin.mpp.commonizerLogLevel")
/** /**
* Enables experimental commonization of 'higher level' shared native source sets * Enables experimental commonization of 'higher level' shared native source sets
*/ */
@@ -100,7 +100,8 @@ internal open class CInteropCommonizerTask : AbstractCInteropCommonizerTask() {
inputLibraries = cinteropsForTarget.map { it.libraryFile.get() }.filter { it.exists() }.toSet(), inputLibraries = cinteropsForTarget.map { it.libraryFile.get() }.filter { it.exists() }.toSet(),
dependencyLibraries = cinteropsForTarget.flatMap { it.dependencies.files }.map(::NonTargetedCommonizerDependency).toSet() dependencyLibraries = cinteropsForTarget.flatMap { it.dependencies.files }.map(::NonTargetedCommonizerDependency).toSet()
+ nativeDistributionDependencies(parameters), + nativeDistributionDependencies(parameters),
outputDirectory = outputDirectory(parameters) outputDirectory = outputDirectory(parameters),
logLevel = project.commonizerLogLevel
) )
} }
@@ -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
}
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.gradle.targets.native.internal
import org.gradle.api.DefaultTask import org.gradle.api.DefaultTask
import org.gradle.api.Project import org.gradle.api.Project
import org.gradle.api.tasks.* import org.gradle.api.tasks.*
import org.jetbrains.kotlin.commonizer.CommonizerLogLevel
import org.jetbrains.kotlin.commonizer.SharedCommonizerTarget import org.jetbrains.kotlin.commonizer.SharedCommonizerTarget
import org.jetbrains.kotlin.commonizer.identityString import org.jetbrains.kotlin.commonizer.identityString
import org.jetbrains.kotlin.commonizer.isAncestorOf import org.jetbrains.kotlin.commonizer.isAncestorOf
@@ -106,6 +107,8 @@ internal open class HierarchicalNativeDistributionCommonizerTask : DefaultTask()
this += getRootOutputDirectory(target).absolutePath this += getRootOutputDirectory(target).absolutePath
this += "-output-commonizer-target" this += "-output-commonizer-target"
this += target.identityString this += target.identityString
this += "-log-level"
this += project.commonizerLogLevel.name
} }
} }
} }
@@ -27,7 +27,8 @@ public class CliCommonizer(private val executor: Executor) : Commonizer {
inputLibraries: Set<File>, inputLibraries: Set<File>,
dependencyLibraries: Set<CommonizerDependency>, dependencyLibraries: Set<CommonizerDependency>,
outputCommonizerTarget: SharedCommonizerTarget, outputCommonizerTarget: SharedCommonizerTarget,
outputDirectory: File outputDirectory: File,
logLevel: CommonizerLogLevel
) { ) {
if (inputLibraries.isEmpty()) return if (inputLibraries.isEmpty()) return
val arguments = mutableListOf<String>().apply { val arguments = mutableListOf<String>().apply {
@@ -39,6 +40,7 @@ public class CliCommonizer(private val executor: Executor) : Commonizer {
if (dependencyLibraries.isNotEmpty()) { if (dependencyLibraries.isNotEmpty()) {
add("-dependency-libraries"); add(dependencyLibraries.joinToString(";")) add("-dependency-libraries"); add(dependencyLibraries.joinToString(";"))
} }
add("-log-level"); add(logLevel.name.lowercase())
} }
executor(arguments) executor(arguments)
} }
@@ -15,6 +15,7 @@ public interface Commonizer : Serializable {
inputLibraries: Set<File>, inputLibraries: Set<File>,
dependencyLibraries: Set<CommonizerDependency>, dependencyLibraries: Set<CommonizerDependency>,
outputCommonizerTarget: SharedCommonizerTarget, outputCommonizerTarget: SharedCommonizerTarget,
outputDirectory: File outputDirectory: File,
logLevel: CommonizerLogLevel = CommonizerLogLevel.Quiet
) )
} }
@@ -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
}
@@ -5,24 +5,34 @@
package org.jetbrains.kotlin.commonizer.cli package org.jetbrains.kotlin.commonizer.cli
import org.jetbrains.kotlin.commonizer.CommonizerLogLevel
import org.jetbrains.kotlin.util.Logger import org.jetbrains.kotlin.util.Logger
import kotlin.system.exitProcess 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) 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 error(message: String) = fatal(message)
override fun fatal(message: String): Nothing { override fun fatal(message: String): Nothing {
printlnIndented("Error: $message\n") printlnIndented("Error: $message\n", *CommonizerLogLevel.values())
exitProcess(1) exitProcess(1)
} }
private fun printlnIndented(text: String) = private fun printlnIndented(text: String, vararg levels: CommonizerLogLevel) {
if (indent.isEmpty()) println(text) if (level in levels) {
else text.split('\n').forEach { println(indent + it) } if (indent.isEmpty()) println(text)
else text.split('\n').forEach {
println(indent + it)
}
}
}
} }
@@ -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)
}
}
}
@@ -17,7 +17,7 @@ internal abstract class Task(private val options: Collection<Option<*>>) : Compa
INFORMATIONAL, INFORMATIONAL,
COMMONIZATION( COMMONIZATION(
prologue = null, prologue = null,
epilogue = "\n", epilogue = null,
logEachStep = true logEachStep = true
) )
} }
@@ -29,7 +29,8 @@ internal enum class TaskType(
"Boolean (default false);\nwhether to copy Kotlin/Native endorsed libraries to the destination", "Boolean (default false);\nwhether to copy Kotlin/Native endorsed libraries to the destination",
mandatory = false mandatory = false
), ),
StatsTypeOptionType StatsTypeOptionType,
LogLevelOptionType,
), ),
::NativeDistributionCommonize ::NativeDistributionCommonize
), ),
@@ -52,6 +53,7 @@ internal enum class TaskType(
InputLibrariesOptionType, InputLibrariesOptionType,
DependencyLibrariesOptionType, DependencyLibrariesOptionType,
OutputCommonizerTargetOptionType, OutputCommonizerTargetOptionType,
LogLevelOptionType
), ),
::NativeKlibCommonize ::NativeKlibCommonize
) )
@@ -50,12 +50,14 @@ internal class NativeKlibCommonize(options: Collection<Option<*>>) : Task(option
val dependencyLibraries = getOptional<List<CommonizerDependency>, DependencyLibrariesOptionType>().orEmpty() val dependencyLibraries = getOptional<List<CommonizerDependency>, DependencyLibrariesOptionType>().orEmpty()
val outputCommonizerTarget = compatGetOutputTarget() val outputCommonizerTarget = compatGetOutputTarget()
val statsType = getOptional<StatsType, StatsTypeOptionType> { it == "log-stats" } ?: StatsType.NONE val statsType = getOptional<StatsType, StatsTypeOptionType> { it == "log-stats" } ?: StatsType.NONE
val logLevel = getOptional<CommonizerLogLevel, LogLevelOptionType>() ?: CommonizerLogLevel.Quiet
val konanTargets = outputCommonizerTarget.konanTargets val konanTargets = outputCommonizerTarget.konanTargets
val commonizerTargets = konanTargets.map(::CommonizerTarget) val commonizerTargets = konanTargets.map(::CommonizerTarget)
val progressLogger = ProgressLogger(CliLoggerAdapter(2)) val logger = ProgressLogger(CliLoggerAdapter(logLevel, 2))
val libraryLoader = DefaultNativeLibraryLoader(progressLogger) val libraryLoader = DefaultNativeLibraryLoader(logger)
val statsCollector = StatsCollector(statsType, commonizerTargets) val statsCollector = StatsCollector(statsType, commonizerTargets)
val repository = FilesRepository(targetLibraries.toSet(), libraryLoader) val repository = FilesRepository(targetLibraries.toSet(), libraryLoader)
@@ -74,7 +76,7 @@ internal class NativeKlibCommonize(options: Collection<Option<*>>) : Task(option
CommonizerDependencyRepository(dependencyLibraries.toSet(), libraryLoader), CommonizerDependencyRepository(dependencyLibraries.toSet(), libraryLoader),
resultsConsumer = resultsConsumer, resultsConsumer = resultsConsumer,
statsCollector = statsCollector, statsCollector = statsCollector,
progressLogger = progressLogger progressLogger = logger
).run() ).run()
statsCollector?.writeTo(FileStatsOutput(destination, statsType.name.lowercase())) 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 copyStdlib = getOptional<Boolean, BooleanOptionType> { it == "copy-stdlib" } ?: false
val copyEndorsedLibs = getOptional<Boolean, BooleanOptionType> { it == "copy-endorsed-libs" } ?: false val copyEndorsedLibs = getOptional<Boolean, BooleanOptionType> { it == "copy-endorsed-libs" } ?: false
val statsType = getOptional<StatsType, StatsTypeOptionType> { it == "log-stats" } ?: StatsType.NONE val statsType = getOptional<StatsType, StatsTypeOptionType> { it == "log-stats" } ?: StatsType.NONE
val logLevel = getOptional<CommonizerLogLevel, LogLevelOptionType>() ?: CommonizerLogLevel.Quiet
val progressLogger = ProgressLogger(CliLoggerAdapter(2)) val logger = ProgressLogger(CliLoggerAdapter(logLevel, 2))
val libraryLoader = DefaultNativeLibraryLoader(progressLogger) val libraryLoader = DefaultNativeLibraryLoader(logger)
val repository = KonanDistributionRepository(distribution, outputTarget.konanTargets, libraryLoader) val repository = KonanDistributionRepository(distribution, outputTarget.konanTargets, libraryLoader)
val statsCollector = StatsCollector(statsType, outputTarget.withAllAncestors().toList()) 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 descriptionSuffix = estimateLibrariesCount(repository, outputTarget.allLeaves()).let { " ($it items)" }
val description = "${logPrefix}Preparing commonized Kotlin/Native libraries for $outputTarget$descriptionSuffix" logger.log("${logPrefix}Preparing commonized Kotlin/Native libraries for $outputTarget$descriptionSuffix")
println(description)
LibraryCommonizer( LibraryCommonizer(
outputTarget = outputTarget, outputTarget = outputTarget,
@@ -121,7 +123,7 @@ internal class NativeDistributionCommonize(options: Collection<Option<*>>) : Tas
dependencies = StdlibRepository(distribution, libraryLoader), dependencies = StdlibRepository(distribution, libraryLoader),
resultsConsumer = resultsConsumer, resultsConsumer = resultsConsumer,
statsCollector = statsCollector, statsCollector = statsCollector,
progressLogger = progressLogger progressLogger = logger
).run() ).run()
statsCollector?.writeTo(FileStatsOutput(destination, statsType.name.lowercase())) statsCollector?.writeTo(FileStatsOutput(destination, statsType.name.lowercase()))
@@ -6,10 +6,11 @@
package org.jetbrains.kotlin.commonizer.utils package org.jetbrains.kotlin.commonizer.utils
import org.jetbrains.kotlin.commonizer.cli.CliLoggerAdapter import org.jetbrains.kotlin.commonizer.cli.CliLoggerAdapter
import org.jetbrains.kotlin.commonizer.CommonizerLogLevel
import org.jetbrains.kotlin.util.Logger import org.jetbrains.kotlin.util.Logger
class ProgressLogger( class ProgressLogger(
private val wrapped: Logger = CliLoggerAdapter(0), private val wrapped: Logger = CliLoggerAdapter(CommonizerLogLevel.Info, 0),
private val indent: Int = 0, private val indent: Int = 0,
) : Logger by wrapped { ) : Logger by wrapped {
private val clockMark = ResettableClockMark() private val clockMark = ResettableClockMark()