Skip to content

Commit

Permalink
Added docs, tests, fix existing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adamfilipow92 authored and mergify-bot committed Dec 7, 2020
1 parent f298a86 commit 0f5faf9
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 17 deletions.
7 changes: 7 additions & 0 deletions docs/logging.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Logs in Flank

1. Log level depends on the output style.
1. ```Simple, multi``` and ```verbose``` output style prints logs from ```SIMPLE``` and ```DETAILED``` levels.
1. ```Compact``` style prints log only from ```SIMPLE``` level.
1. If you want a print message for all output styles uses ```log``` or ```logLn``` with only ```message``` parameter.
1. If you want print message more detailed message use ```log``` or ```logLn``` and set ```level``` to ```OutputLogLevel.DETAILED```
2 changes: 1 addition & 1 deletion test_runner/src/main/kotlin/ftl/args/IArgs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ interface IArgs {
}

val IArgs.logLevel
get() = if (outputStyle == OutputStyle.Compact) OutputLogLevel.LOW else OutputLogLevel.All
get() = if (outputStyle == OutputStyle.Compact) OutputLogLevel.SIMPLE else OutputLogLevel.DETAILED

fun IArgs.setLogLevel() = also {
ftl.log.setLogLevel(logLevel)
Expand Down
10 changes: 5 additions & 5 deletions test_runner/src/main/kotlin/ftl/log/OutputLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ fun setLogLevel(logLevel: OutputLogLevel) {
minimumLogLevel = logLevel
}

fun log(message: Any, level: OutputLogLevel = OutputLogLevel.LOW) =
fun log(message: Any, level: OutputLogLevel = OutputLogLevel.SIMPLE) =
if (minimumLogLevel >= level) print(message)
else Unit

fun logLn(message: Any = "", level: OutputLogLevel = OutputLogLevel.LOW) =
fun logLn(message: Any = "", level: OutputLogLevel = OutputLogLevel.SIMPLE) =
if (minimumLogLevel >= level) println(message)
else Unit

private var minimumLogLevel: OutputLogLevel = OutputLogLevel.LOW
private var minimumLogLevel: OutputLogLevel = OutputLogLevel.DETAILED

enum class OutputLogLevel {
LOW,
All
SIMPLE,
DETAILED
}
2 changes: 1 addition & 1 deletion test_runner/src/main/kotlin/ftl/run/DumpShards.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fun saveShardChunks(
Paths.get(shardFilePath),
getGson(obfuscatedOutput).toJson(shards).toByteArray()
)
logLn("Saved $size shards to $shardFilePath", OutputLogLevel.All)
logLn("Saved $size shards to $shardFilePath", OutputLogLevel.DETAILED)
}

private fun getGson(obfuscatedOutput: Boolean) = if (obfuscatedOutput) obfuscatePrettyPrinter else prettyPrint
Expand Down
10 changes: 5 additions & 5 deletions test_runner/src/main/kotlin/ftl/run/common/FetchArtifacts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import java.nio.file.Paths

// TODO needs refactor
internal suspend fun fetchArtifacts(matrixMap: MatrixMap, args: IArgs) = coroutineScope {
logLn("FetchArtifacts", OutputLogLevel.All)
logLn("FetchArtifacts", OutputLogLevel.DETAILED)
val fields = Storage.BlobListOption.fields(Storage.BlobField.NAME)

var dirty = false
Expand All @@ -43,7 +43,7 @@ internal suspend fun fetchArtifacts(matrixMap: MatrixMap, args: IArgs) = corouti
if (matched) {
val downloadFile = getDownloadPath(args, blobPath)

log(".", OutputLogLevel.All)
log(".", OutputLogLevel.DETAILED)
if (!downloadFile.toFile().exists()) {
val parentFile = downloadFile.parent.toFile()
parentFile.mkdirs()
Expand All @@ -56,12 +56,12 @@ internal suspend fun fetchArtifacts(matrixMap: MatrixMap, args: IArgs) = corouti
filtered[index] = matrix.copy(downloaded = true)
jobs
}.joinAll()
logLn(level = OutputLogLevel.All)
logLn(level = OutputLogLevel.DETAILED)

if (dirty) {
logLn(FtlConstants.indent + "Updating matrix file", level = OutputLogLevel.All)
logLn(FtlConstants.indent + "Updating matrix file", level = OutputLogLevel.DETAILED)
args.updateMatrixFile(matrixMap)
logLn(level = OutputLogLevel.All)
logLn(level = OutputLogLevel.DETAILED)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ internal class MultiLinePrinter(
@VisibleForTesting
internal object VerbosePrinter : (List<ExecutionStatus.Change>) -> Unit {
override fun invoke(changes: List<ExecutionStatus.Change>) {
changes.map(ExecutionStatus.Change::views).flatten().forEach { logLn(it, OutputLogLevel.All) }
changes.map(ExecutionStatus.Change::views).flatten().forEach { logLn(it, OutputLogLevel.DETAILED) }
}
}

Expand Down
6 changes: 3 additions & 3 deletions test_runner/src/main/kotlin/ftl/util/ProgressBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ class ProgressBar {
private val timer = Timer(true)

fun start(msg: String) {
log(" $msg ", OutputLogLevel.All)
log(" $msg ", OutputLogLevel.DETAILED)
timer.scheduleAtFixedRate(task, 0, 10_000)
}

fun stop() {
logLn(level = OutputLogLevel.All)
logLn(level = OutputLogLevel.DETAILED)
timer.cancel()
task.cancel()
}
}

private class ProgressBarTask : TimerTask() {
override fun run() {
log(".", OutputLogLevel.All)
log(".", OutputLogLevel.DETAILED)
}
}

Expand Down
45 changes: 45 additions & 0 deletions test_runner/src/test/kotlin/ftl/log/OutputLoggerTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ftl.log

import com.google.common.truth.Truth
import ftl.test.util.TestHelper.normalizeLineEnding
import org.junit.After
import org.junit.Rule
import org.junit.Test
import org.junit.contrib.java.lang.system.SystemOutRule

class OutputLoggerTest {

@Rule
@JvmField
val systemOutRule: SystemOutRule = SystemOutRule().enableLog().muteForSuccessfulTests()

private val simpleMessage = "print for simple"
private val detailedMessage = "print for detailed"

@After
fun afterTest() {
setLogLevel(OutputLogLevel.DETAILED)
}

@Test
fun `should print messages from all output levels if log level set to detailed`() {
setLogLevel(OutputLogLevel.DETAILED)

logLn(simpleMessage)
logLn(detailedMessage, OutputLogLevel.DETAILED)

Truth.assertThat(systemOutRule.log.normalizeLineEnding()).contains(simpleMessage)
Truth.assertThat(systemOutRule.log.normalizeLineEnding()).contains(detailedMessage)
}

@Test
fun `should print messages only simple output level if log level not set`() {
setLogLevel(OutputLogLevel.SIMPLE)

logLn(simpleMessage)
logLn(detailedMessage, OutputLogLevel.DETAILED)

Truth.assertThat(systemOutRule.log.normalizeLineEnding()).contains(simpleMessage)
Truth.assertThat(systemOutRule.log.normalizeLineEnding()).doesNotContain(detailedMessage)
}
}
2 changes: 1 addition & 1 deletion test_runner/src/test/kotlin/ftl/util/ProgressBarTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ProgressBarTest {

@Before
fun beforeTest() {
setLogLevel(OutputLogLevel.All)
setLogLevel(OutputLogLevel.DETAILED)
}

@Test
Expand Down

0 comments on commit 0f5faf9

Please sign in to comment.