From c073be8eb0013445220a988afea0984152cfb62a Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Tue, 9 Aug 2016 00:06:20 -0800 Subject: [PATCH] More parallel logging work. --- .../main/kotlin/com/beust/kobalt/AsciiArt.kt | 15 +++++------ .../beust/kobalt/internal/BuildListeners.kt | 2 +- .../com/beust/kobalt/internal/DynamicGraph.kt | 4 +-- .../beust/kobalt/internal/ParallelLogger.kt | 26 ++++++++++--------- .../kobalt/internal/ParallelProjectRunner.kt | 9 +++---- .../com/beust/kobalt/internal/TaskManager.kt | 2 +- 6 files changed, 29 insertions(+), 29 deletions(-) diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/AsciiArt.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/AsciiArt.kt index bcd40d4c..df506b9f 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/AsciiArt.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/AsciiArt.kt @@ -1,6 +1,5 @@ package com.beust.kobalt -import com.beust.kobalt.misc.log import java.util.* /** @@ -75,13 +74,12 @@ class AsciiArt { return result } - val defaultLog : (s: String) -> Unit = { log(1, " $it") } - - fun logBox(strings: List, bl: String = bottomLeft, br: String = bottomRight, - indent: Int = 0): String { + fun logBox(strings: List, bl: String = bottomLeft, br: String = bottomRight, indent: Int = 0): String { return buildString { - box(strings, bl, br).forEach { - append(fill(indent)).append(it).append("\n") + val boxLines = box(strings, bl, br) + boxLines.withIndex().forEach { iv -> + append(fill(indent)).append(iv.value) + if (iv.index < boxLines.size - 1) append("\n") } } } @@ -125,7 +123,7 @@ class AsciiTable { fun headers(vararg names: String) = headers.addAll(names) private val widths = arrayListOf() - fun width(w: Int) : Builder { + fun columnWidth(w: Int) : Builder { widths.add(w) return this } @@ -149,6 +147,7 @@ class AsciiTable { }.joinToString(vb) val result = StringBuffer().apply { append(AsciiArt.logBox(formattedHeaders, AsciiArt.bottomLeft2, AsciiArt.bottomRight2)) + append("\n") } var lineLength = 0 rows.forEachIndexed { index, row -> diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/BuildListeners.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/BuildListeners.kt index 1d666dee..7337b40d 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/BuildListeners.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/BuildListeners.kt @@ -81,7 +81,7 @@ class BuildListeners : IBuildListener, IBuildReportContributor { val line = listOf(col1("Project"), col2("Build status"), col3("Time")) .joinToString(AsciiArt.verticalBar) val table = StringBuffer() - table.append(AsciiArt.logBox(listOf(line), AsciiArt.bottomLeft2, AsciiArt.bottomRight2, indent = 10)) + table.append(AsciiArt.logBox(listOf(line), AsciiArt.bottomLeft2, AsciiArt.bottomRight2, indent = 10) + "\n") projectStatuses.forEach { pair -> val projectName = pair.first.name val cl = listOf(col1(projectName), col2(pair.second.toString()), diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/DynamicGraph.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/DynamicGraph.kt index c8b2c944..1271f6cf 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/DynamicGraph.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/DynamicGraph.kt @@ -271,9 +271,9 @@ class DynamicGraphExecutor(val graph : DynamicGraph, val factory: IThreadW log(1, "Thread report") val table = AsciiTable.Builder() - .width(11) + .columnWidth(11) threadIds.keys.forEach { - table.width(20) + table.columnWidth(24) } table.header("Time (sec)") threadIds.keys.forEach { diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/ParallelLogger.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/ParallelLogger.kt index 05038dd1..29ae7b62 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/ParallelLogger.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/ParallelLogger.kt @@ -12,7 +12,7 @@ import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentLinkedQueue interface ILogger { - fun log(tag: String, level: Int, message: String) + fun log(tag: CharSequence, level: Int, message: CharSequence) } /** @@ -28,8 +28,8 @@ interface ILogger { class ParallelLogger @Inject constructor(val args: Args) : ILogger { enum class Type { LOG, WARN, ERROR } - class LogLine(val name: String? = null, val level: Int, val message: String, val type: Type) - private val logLines = ConcurrentHashMap>() + class LogLine(val name: CharSequence? = null, val level: Int, val message: CharSequence, val type: Type) + private val logLines = ConcurrentHashMap>() private val runningProjects = ConcurrentLinkedQueue() var startTime: Long? = null @@ -65,9 +65,11 @@ class ParallelLogger @Inject constructor(val args: Args) : ILogger { } } - private fun debug(s: String) { - val time = System.currentTimeMillis() - startTime!! - println(" @@@ [$time] $s") + private fun debug(s: CharSequence) { + if (args.log >= 2) { + val time = System.currentTimeMillis() - startTime!! + println(" ### [$time] $s") + } } val LOCK = Any() @@ -78,7 +80,7 @@ class ParallelLogger @Inject constructor(val args: Args) : ILogger { private fun displayLine(ll: LogLine) { val time = System.currentTimeMillis() - startTime!! - val m = "### [$time] " + ll.message + val m = (if (args.dev) "### [$time] " else "") + ll.message when(ll.type) { Type.LOG -> kobaltLog(ll.level, m) Type.WARN -> kobaltWarn(m) @@ -86,22 +88,22 @@ class ParallelLogger @Inject constructor(val args: Args) : ILogger { } } - private fun emptyProjectLog(name: String?) { + private fun emptyProjectLog(name: CharSequence?) { val lines = logLines[name] if (lines != null && lines.any()) { - debug("EMPTY PROJECT LOG FOR $name") + debug("emptyProjectLog($name)") lines.forEach { displayLine(it) } lines.clear() - debug("DONE EMPTY PROJECT LOG FOR $name") + debug("Done emptyProjectLog($name)") // logLines.remove(name) } else if (lines == null) { throw KobaltException("Didn't call onStartProject() for $name") } } - private fun addLogLine(name: String, ll: LogLine) { + private fun addLogLine(name: CharSequence, ll: LogLine) { if (name != currentName) { val list = logLines[name] ?: arrayListOf() logLines[name] = list @@ -112,7 +114,7 @@ class ParallelLogger @Inject constructor(val args: Args) : ILogger { } } - override fun log(tag: String, level: Int, message: String) { + override fun log(tag: CharSequence, level: Int, message: CharSequence) { if (args.parallel) { addLogLine(tag, LogLine(tag, level, message, Type.LOG)) } else { diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/ParallelProjectRunner.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/ParallelProjectRunner.kt index 93dcc0f0..8fb665e2 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/ParallelProjectRunner.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/ParallelProjectRunner.kt @@ -1,6 +1,7 @@ package com.beust.kobalt.internal import com.beust.kobalt.Args +import com.beust.kobalt.AsciiArt import com.beust.kobalt.TaskResult import com.beust.kobalt.api.ITask import com.beust.kobalt.api.Kobalt @@ -42,7 +43,7 @@ class ParallelProjectRunner(val tasksByNames: (Project) -> ListMultimap task.plugin.accept(project) }) var lastResult = TaskResult() kobaltLog.onProjectStarted(project.name) - kobaltLog.log(project.name, 1, "Parallel build of ${project.name} starting") + context.logger.log(project.name, 1, AsciiArt.logBox("Building ${project.name}", indent = 5)) while (graph.freeNodes.any()) { val toProcess = graph.freeNodes toProcess.forEach { node -> @@ -50,8 +51,8 @@ class ParallelProjectRunner(val tasksByNames: (Project) -> ListMultimap runBuildListenersForTask(project, context, task.name, start = true) - kobaltLog.log(project.name, 1, "===== " + project.name + ":" + task.name) -// log(1, "===== " + project.name + ":" + task.name) + kobaltLog.log(project.name, 1, + AsciiArt.taskColor(AsciiArt.horizontalSingleLine + " ${project.name}:${task.name}")) val thisResult = if (dryRun) TaskResult2(true, null, task) else task.call() if (lastResult.success) { lastResult = thisResult @@ -67,8 +68,6 @@ class ParallelProjectRunner(val tasksByNames: (Project) -> ListMultimap, val dryRun: Boolean, val pluginInfo: Pl // override val timeOut : Long = 10000 override val priority: Int = 0 - override val name: String get() = "[Taskworker " + tasks.map { it.toString() }.joinToString(",") + "]" + override val name: String get() = "[Taskworker " + tasks.map(ITask::toString).joinToString(",") + "]" }