From 9ce31cc2b9f50265f3df50a5a6e973374e39e780 Mon Sep 17 00:00:00 2001 From: Ian Lynagh Date: Mon, 9 Sep 2024 15:19:39 +0100 Subject: [PATCH] KE2: Add a BasicLogger interface --- .../src/main/kotlin/utils/Logger.kt | 57 ++++++++++++++----- 1 file changed, 43 insertions(+), 14 deletions(-) diff --git a/java/kotlin-extractor2/src/main/kotlin/utils/Logger.kt b/java/kotlin-extractor2/src/main/kotlin/utils/Logger.kt index 7b76dd0e7f2..9fc7f607145 100644 --- a/java/kotlin-extractor2/src/main/kotlin/utils/Logger.kt +++ b/java/kotlin-extractor2/src/main/kotlin/utils/Logger.kt @@ -107,13 +107,22 @@ data class ExtractorContext( ) */ +interface BasicLogger { + abstract fun trace(dtw: DiagnosticTrapWriter, msg: String) + abstract fun debug(dtw: DiagnosticTrapWriter, msg: String) + abstract fun info(dtw: DiagnosticTrapWriter, msg: String) + abstract fun warn(dtw: DiagnosticTrapWriter, msg: String, extraInfo: String?) + abstract fun error(dtw: DiagnosticTrapWriter, msg: String, extraInfo: String?) + abstract fun flush() +} + /** * LoggerBase actually writes log messages to a log file, and to * the DiagnosticTrapWriter that it is passed. * It is only usde directly from the DiagnosticTrapWriter. Everything * else will use a Logger that wraps it (and the DiagnosticTrapWriter). */ -open class LoggerBase(val diagnosticCounter: DiagnosticCounter) { +class LoggerBase(val diagnosticCounter: DiagnosticCounter): BasicLogger { private val verbosity: Int init { @@ -241,7 +250,7 @@ OLD: KE1 logStream.write(logMessage.toJsonLine()) } - fun trace(dtw: DiagnosticTrapWriter, msg: String) { + override fun trace(dtw: DiagnosticTrapWriter, msg: String) { if (verbosity >= 4) { val logMessage = LogMessage("TRACE", msg) dtw.writeComment(logMessage.toText()) @@ -249,7 +258,7 @@ OLD: KE1 } } - fun debug(dtw: DiagnosticTrapWriter, msg: String) { + override fun debug(dtw: DiagnosticTrapWriter, msg: String) { if (verbosity >= 4) { val logMessage = LogMessage("DEBUG", msg) dtw.writeComment(logMessage.toText()) @@ -257,7 +266,7 @@ OLD: KE1 } } - fun info(dtw: DiagnosticTrapWriter, msg: String) { + override fun info(dtw: DiagnosticTrapWriter, msg: String) { if (verbosity >= 3) { val logMessage = LogMessage("INFO", msg) dtw.writeComment(logMessage.toText()) @@ -265,13 +274,13 @@ OLD: KE1 } } - fun warn(dtw: DiagnosticTrapWriter, msg: String, extraInfo: String?) { + override fun warn(dtw: DiagnosticTrapWriter, msg: String, extraInfo: String?) { if (verbosity >= 2) { diagnostic(dtw, Severity.Warn, msg, extraInfo) } } - fun error(dtw: DiagnosticTrapWriter, msg: String, extraInfo: String?) { + override fun error(dtw: DiagnosticTrapWriter, msg: String, extraInfo: String?) { if (verbosity >= 1) { diagnostic(dtw, Severity.Error, msg, extraInfo) } @@ -294,7 +303,7 @@ OLD: KE1 } */ - fun flush() { + override fun flush() { logStream.flush() } @@ -306,37 +315,53 @@ OLD: KE1 /** * Logger is the high-level interface for writint log messages. */ -open class Logger(val loggerBase: LoggerBase, val dtw: DiagnosticTrapWriter) { +open class Logger(val loggerBase: LoggerBase, val dtw: DiagnosticTrapWriter): BasicLogger { /* OLD: KE1 val extractorContextStack = Stack() */ - fun flush() { + override fun flush() { dtw.flush() loggerBase.flush() } - fun trace(msg: String) { + override fun trace(dtw: DiagnosticTrapWriter, msg: String) { loggerBase.trace(dtw, msg) } + fun trace(msg: String) { + trace(dtw, msg) + } + fun trace(msg: String, exn: Throwable) { trace(msg + "\n" + exn.stackTraceToString()) } - fun debug(msg: String) { + override fun debug(dtw: DiagnosticTrapWriter, msg: String) { loggerBase.debug(dtw, msg) } - fun info(msg: String) { + fun debug(msg: String) { + debug(dtw, msg) + } + + override fun info(dtw: DiagnosticTrapWriter, msg: String) { loggerBase.info(dtw, msg) } - private fun warn(msg: String, extraInfo: String?) { + fun info(msg: String) { + info(dtw, msg) + } + + override fun warn(dtw: DiagnosticTrapWriter, msg: String, extraInfo: String?) { loggerBase.warn(dtw, msg, extraInfo) } + private fun warn(msg: String, extraInfo: String?) { + warn(dtw, msg, extraInfo) + } + fun warn(msg: String, exn: Throwable) { warn(msg, exn.stackTraceToString()) } @@ -345,10 +370,14 @@ OLD: KE1 warn(msg, null) } - private fun error(msg: String, extraInfo: String?) { + override fun error(dtw: DiagnosticTrapWriter, msg: String, extraInfo: String?) { loggerBase.error(dtw, msg, extraInfo) } + private fun error(msg: String, extraInfo: String?) { + error(dtw, msg, extraInfo) + } + fun error(msg: String) { error(msg, null) }