aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/utilities/DokkaLogging.kt
blob: 0302b8a221e918586ff88fc0113b330ad49b8e3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package org.jetbrains.dokka.utilities

interface DokkaLogger {
    var warningsCount: Int
    var errorsCount: Int
    fun debug(message: String)
    fun info(message: String)
    fun progress(message: String)
    fun warn(message: String)
    fun error(message: String)
}

fun DokkaLogger.report() {
    if (warningsCount > 0 || errorsCount > 0) {
        info(
            "Generation completed with $warningsCount warning" +
                    (if (warningsCount == 1) "" else "s") +
                    " and $errorsCount error" +
                    if (errorsCount == 1) "" else "s"
        )
    } else {
        info("Generation completed successfully")
    }
}

enum class LoggingLevel(val index: Int) {
    DEBUG(0), PROGRESS(1), INFO(2), WARN(3), ERROR(4);
}

/**
 * Used to decouple the transport layer from logger and make it convenient for testing
 */
fun interface MessageEmitter : (String) -> Unit {
    companion object {
        val consoleEmitter: MessageEmitter = MessageEmitter { message -> println(message) }
    }
}

class DokkaConsoleLogger(
    val minLevel: LoggingLevel = LoggingLevel.DEBUG,
    private val emitter: MessageEmitter = MessageEmitter.consoleEmitter
) : DokkaLogger {
    override var warningsCount: Int = 0
    override var errorsCount: Int = 0

    override fun debug(message: String) {
        if (shouldBeDisplayed(LoggingLevel.DEBUG)) emitter(message)
    }

    override fun progress(message: String) {
        if (shouldBeDisplayed(LoggingLevel.PROGRESS)) emitter("PROGRESS: $message")
    }

    override fun info(message: String) {
        if (shouldBeDisplayed(LoggingLevel.INFO)) emitter(message)
    }

    override fun warn(message: String) {
        if (shouldBeDisplayed(LoggingLevel.WARN)) {
            emitter("WARN: $message")
        }
        warningsCount++
    }

    override fun error(message: String) {
        if (shouldBeDisplayed(LoggingLevel.ERROR)) {
            emitter("ERROR: $message")
        }
        errorsCount++
    }

    private fun shouldBeDisplayed(messageLevel: LoggingLevel): Boolean = messageLevel.index >= minLevel.index
}