Kotlin: Pull Logger out into its own file

This commit is contained in:
Ian Lynagh
2021-09-09 20:25:39 +01:00
parent e8d3125b40
commit 2721f6aabf
2 changed files with 87 additions and 84 deletions

View File

@@ -8,8 +8,6 @@ import java.io.StringWriter
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Optional
import kotlin.system.exitProcess
import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
@@ -79,88 +77,6 @@ class StarLabel<T>(): Label<T> {
fun escapeTrapString(str: String) = str.replace("\"", "\"\"")
class LogCounter() {
public val warningCounts = mutableMapOf<String, Int>()
public val warningLimit: Int
init {
warningLimit = System.getenv("CODEQL_EXTRACTOR_KOTLIN_WARNING_LIMIT")?.toIntOrNull() ?: 100
}
}
enum class Severity(val sev: Int) {
WarnLow(1),
Warn(2),
WarnHigh(3),
/** Minor extractor errors, with minimal impact on analysis. */
ErrorLow(4),
/** Most extractor errors, with local impact on analysis. */
Error(5),
/** Javac errors. */
ErrorHigh(6),
/** Severe extractor errors affecting a single source file. */
ErrorSevere(7),
/** Severe extractor errors likely to affect multiple source files. */
ErrorGlobal(8)
}
open class Logger(val logCounter: LogCounter, open val tw: TrapWriter) {
private fun timestamp(): String {
return "[${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())} K]"
}
fun flush() {
tw.flush()
System.out.flush()
}
fun info(msg: String) {
val fullMsg = "${timestamp()} $msg"
tw.writeTrap("// " + fullMsg.replace("\n", "\n//") + "\n")
println(fullMsg)
}
fun warn(severity: Severity, msg: String, locationString: String? = null, locationId: Label<DbLocation> = tw.unknownLocation) {
val st = Exception().stackTrace
val suffix =
if(st.size < 2) {
" Missing caller information.\n"
} else {
val caller = st[1].toString()
val count = logCounter.warningCounts.getOrDefault(caller, 0) + 1
logCounter.warningCounts[caller] = count
when {
logCounter.warningLimit <= 0 -> ""
count == logCounter.warningLimit -> " Limit reached for warnings from $caller.\n"
count > logCounter.warningLimit -> return
else -> ""
}
}
val ts = timestamp()
tw.writeDiagnostics(StarLabel(), severity.sev, "", msg, "$ts $msg\n$suffix", locationId)
val locStr = if (locationString == null) "" else "At " + locationString + ": "
print("$ts Warning: $locStr$msg\n$suffix")
}
fun printLimitedWarningCounts() {
for((caller, count) in logCounter.warningCounts) {
if(count >= logCounter.warningLimit) {
val msg = "Total of $count warnings from $caller.\n"
tw.writeTrap("// $msg")
print(msg)
}
}
}
}
class FileLogger(logCounter: LogCounter, override val tw: FileTrapWriter): Logger(logCounter, tw) {
private fun timestamp(): String {
return "[${SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())} K]"
}
fun warnElement(severity: Severity, msg: String, element: IrElement) {
val locationString = tw.getLocationString(element)
val locationId = tw.getLocation(element)
warn(severity, msg, locationString, locationId)
}
}
class TrapLabelManager {
public var nextId: Int = 100