Use task's logger where appropriate

KT-28836

Using project's logger prevents messages
from showing in task's output (in command-line and build scans)
This commit is contained in:
Alexey Tsvetkov
2018-12-16 00:48:20 +03:00
parent 256317da15
commit 78d9cd1277
9 changed files with 58 additions and 18 deletions
@@ -16,7 +16,7 @@ internal class GradleCompilerRunnerWithWorkers(
private val workersExecutor: WorkerExecutor
) : GradleCompilerRunner(task) {
override fun runCompilerAsync(workArgs: GradleKotlinCompilerWorkArguments) {
project.logger.kotlinDebug { "Starting Kotlin compiler work from task '${task.path}'" }
task.logger.kotlinDebug { "Starting Kotlin compiler work from task '${task.path}'" }
// todo: write tests with Workers enabled;
workersExecutor.submit(GradleKotlinCompilerWork::class.java) { config ->
config.isolationMode = IsolationMode.NONE
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.cli.common.arguments.K2MetadataCompilerArguments
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.daemon.client.CompileServiceSession
import org.jetbrains.kotlin.daemon.common.*
import org.jetbrains.kotlin.gradle.plugin.TaskLoggers
import org.jetbrains.kotlin.gradle.plugin.kotlinDebug
import org.jetbrains.kotlin.gradle.tasks.*
import org.jetbrains.kotlin.gradle.utils.newTmpFile
@@ -121,7 +122,7 @@ internal open class GradleCompilerRunner(protected val task: Task) {
buildFile: File? = null
) {
if (compilerArgs.version) {
project.logger.lifecycle(
task.logger.lifecycle(
"Kotlin version " + loadCompilerVersion(environment.compilerClasspath) +
" (JRE " + System.getProperty("java.runtime.version") + ")"
)
@@ -139,8 +140,10 @@ internal open class GradleCompilerRunner(protected val task: Task) {
incrementalCompilationEnvironment = incrementalCompilationEnvironment,
incrementalModuleInfo = modulesInfo,
buildFile = buildFile,
localStateDirectories = environment.localStateDirectories
localStateDirectories = environment.localStateDirectories,
taskPath = task.path
)
TaskLoggers.put(task.path, task.logger)
runCompilerAsync(workArgs)
}
@@ -56,7 +56,8 @@ internal class GradleKotlinCompilerWorkArguments(
val incrementalCompilationEnvironment: IncrementalCompilationEnvironment?,
val incrementalModuleInfo: IncrementalModuleInfo?,
val buildFile: File?,
val localStateDirectories: List<File>
val localStateDirectories: List<File>,
val taskPath: String
) : Serializable {
companion object {
const val serialVersionUID: Long = 0
@@ -84,9 +85,14 @@ internal class GradleKotlinCompilerWork @Inject constructor(
private val incrementalModuleInfo = config.incrementalModuleInfo
private val buildFile = config.buildFile
private val localStateDirectories = config.localStateDirectories
private val taskPath = config.taskPath
private val log: KotlinLogger =
SL4JKotlinLogger(LoggerFactory.getLogger("GradleKotlinCompilerWork"))
TaskLoggers.get(taskPath)?.let { GradleKotlinLogger(it) }
?: run {
System.err.println("Could not get logger for '$taskPath'. Falling back to sl4j logger")
SL4JKotlinLogger(LoggerFactory.getLogger("GradleKotlinCompilerWork"))
}
private val messageCollector = GradleMessageCollector(log)
private val isIncremental: Boolean
@@ -66,6 +66,7 @@ internal class KotlinGradleBuildServices private constructor(gradle: Gradle) : B
// but it is called before any plugin can attach build listener
fun buildStarted() {
startMemory = getUsedMemoryKb()
TaskLoggers.clear()
}
override fun buildFinished(result: BuildResult) {
@@ -97,6 +98,7 @@ internal class KotlinGradleBuildServices private constructor(gradle: Gradle) : B
log.lifecycle("[KOTLIN][PERF] Used memory after build: $endMem kb (difference since build start: ${"%+d".format(endMem - startMem)} kb)")
}
TaskLoggers.clear()
gradle.removeListener(this)
instance = null
log.kotlinDebug(DISPOSE_MESSAGE)
@@ -84,11 +84,11 @@ open class KotlinPlatformImplementationPluginBase(platformName: String) : Kotlin
val incrementalMultiplatform = PropertiesProvider(project).incrementalMultiplatform ?: true
project.afterEvaluate {
project.tasks.withType(AbstractKotlinCompile::class.java).all {
if (it.incremental && !incrementalMultiplatform) {
project.logger.debug("IC is turned off for task '${it.path}' because multiplatform IC is not enabled")
project.tasks.withType(AbstractKotlinCompile::class.java).all { task ->
if (task.incremental && !incrementalMultiplatform) {
task.logger.debug("IC is turned off for task '${task.path}' because multiplatform IC is not enabled")
}
it.incremental = it.incremental && incrementalMultiplatform
task.incremental = task.incremental && incrementalMultiplatform
}
}
}
@@ -0,0 +1,29 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. 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.plugin
import org.gradle.api.logging.Logger
import java.lang.ref.WeakReference
import java.util.HashMap
// todo: remove when https://github.com/gradle/gradle/issues/2678 is resolved
object TaskLoggers {
private val taskLoggers = HashMap<String, WeakReference<Logger>>()
@Synchronized
fun put(path: String, logger: Logger) {
taskLoggers[path] = WeakReference(logger)
}
@Synchronized
fun get(path: String): Logger? =
taskLoggers[path]?.get()
@Synchronized
fun clear() {
taskLoggers.clear()
}
}
@@ -91,14 +91,14 @@ open class DiscoverScriptExtensionsTask : DefaultTask() {
val definitions =
ScriptDefinitionsFromClasspathDiscoverySource(
scriptingClasspath.toList(), emptyMap(),
GradleMessageCollector(project.logger)
GradleMessageCollector(logger)
).definitions
val extensions = definitions.mapTo(arrayListOf(), KotlinScriptDefinition::fileExtension)
val kotlinSourceSet = sourceSet.getConvention(KOTLIN_DSL_NAME) as? KotlinSourceSet
if (kotlinSourceSet == null) {
project.logger.warn("kotlin scripting plugin: kotlin source set not found: $project.$sourceSet")
logger.warn("kotlin scripting plugin: kotlin source set not found: $project.$sourceSet")
} else if (extensions.isNotEmpty()) {
project.logger.info("kotlin scripting plugin: Add new extensions to the sourceset $project.$sourceSet: $extensions")
logger.info("kotlin scripting plugin: Add new extensions to the sourceset $project.$sourceSet: $extensions")
kotlinSourceSet.kotlin.filter.include(extensions.map { "**/*.$it" })
}
}
@@ -73,7 +73,7 @@ open class KotlinJsDce : AbstractKotlinCompileTool<K2JSDceArguments>(), KotlinJs
val argsArray = serializedCompilerArguments.toTypedArray()
val log = GradleKotlinLogger(project.logger)
val log = GradleKotlinLogger(logger)
val allArgs = argsArray + outputDirArgs + inputFiles
val exitCode = runToolInSeparateProcess(
allArgs, K2JSDce::class.java.name, computedCompilerClasspath,
@@ -50,7 +50,7 @@ const val USING_JS_INCREMENTAL_COMPILATION_MESSAGE = "Using Kotlin/JS incrementa
abstract class AbstractKotlinCompileTool<T : CommonToolArguments>() : AbstractCompile(), CompilerArgumentAwareWithInput<T> {
private fun useCompilerClasspathConfigurationMessage(propertyName: String) {
project.logger.kotlinWarn(
logger.kotlinWarn(
"'$path.$propertyName' is deprecated and will be removed soon. " +
"Use '$COMPILER_CLASSPATH_CONFIGURATION_NAME' " +
"configuration for customizing compiler classpath."
@@ -94,7 +94,7 @@ abstract class AbstractKotlinCompileTool<T : CommonToolArguments>() : AbstractCo
try {
project.configurations.getByName(COMPILER_CLASSPATH_CONFIGURATION_NAME).resolve().toList()
} catch (e: Exception) {
project.logger.error(
logger.error(
"Could not resolve compiler classpath. " +
"Check if Kotlin Gradle plugin repository is configured in $project."
)
@@ -296,7 +296,7 @@ abstract class AbstractKotlinCompile<T : CommonCompilerArguments>() : AbstractKo
logger.kotlinDebug { "args.coroutinesState=${args.coroutinesState}" }
if (project.logger.isDebugEnabled) {
if (logger.isDebugEnabled) {
args.verbose = true
}
@@ -428,7 +428,7 @@ open class KotlinCompile : AbstractKotlinCompile<K2JVMCompilerArguments>(), Kotl
}.firstOrNull() as? AbstractCompile
if (illegalTask != null) {
project.logger.info(
logger.info(
"Kotlin inter-project IC is disabled: " +
"unknown task '$illegalTask' destination dir ${illegalTask.destinationDir} " +
"intersects with java destination dir $javaOutputDir"
@@ -577,7 +577,7 @@ private fun Task.getGradleVersion(): ParsedGradleVersion? {
val gradleVersion = project.gradle.gradleVersion
val result = ParsedGradleVersion.parse(gradleVersion)
if (result == null) {
project.logger.kotlinDebug("Could not parse gradle version: $gradleVersion")
logger.kotlinDebug("Could not parse gradle version: $gradleVersion")
}
return result
}