KE2: We now create a Logger

This commit is contained in:
Ian Lynagh
2024-08-23 15:51:53 +01:00
parent 8b11b65292
commit f3afedd510
3 changed files with 118 additions and 103 deletions

View File

@@ -75,6 +75,7 @@ fun runExtractor(args : Array<String>) {
->
/*
OLD: KE1
val usesK2 = usesK2(pluginContext)
val invocationExtractionProblems = ExtractionProblems()
*/
val invocationLabelManager = TrapLabelManager()
@@ -86,17 +87,106 @@ OLD: KE1
dtw.writeCompilation_started(compilation)
/*
OLD: KE1
tw.writeCompilation_info(
dtw.writeCompilation_info(
compilation,
"Kotlin Compiler Version",
KotlinCompilerVersion.getVersion() ?: "<unknown>"
)
val extractor_name =
this::class.java.getResource("extractor.name")?.readText() ?: "<unknown>"
dtw.writeCompilation_info(compilation, "Kotlin Extractor Name", extractor_name)
dtw.writeCompilation_info(compilation, "Uses Kotlin 2", usesK2.toString())
if (compilationStartTime != null) {
dtw.writeCompilation_compiler_times(
compilation,
-1.0,
(System.currentTimeMillis() - compilationStartTime) / 1000.0
)
}
*/
dtw.flush()
val logger = Logger(loggerBase, dtw)
logger.info("Extraction started")
/*
OLD: KE1
logger.flush()
logger.info("Extraction for invocation TRAP file $invocationTrapFile")
logger.flush()
logger.info("Kotlin version ${KotlinCompilerVersion.getVersion()}")
logger.flush()
logPeakMemoryUsage(logger, "before extractor")
if (System.getenv("CODEQL_EXTRACTOR_JAVA_KOTLIN_DUMP") == "true") {
logger.info("moduleFragment:\n" + moduleFragment.dump())
}
val compression = getCompression(logger)
val primitiveTypeMapping = PrimitiveTypeMapping(logger, pluginContext)
// FIXME: FileUtil expects a static global logger
// which should be provided by SLF4J's factory facility. For now we set it here.
FileUtil.logger = logger
val srcDir =
File(
System.getenv("CODEQL_EXTRACTOR_JAVA_SOURCE_ARCHIVE_DIR").takeUnless {
it.isNullOrEmpty()
} ?: "kotlin-extractor/src"
)
srcDir.mkdirs()
val globalExtensionState = KotlinExtractorGlobalState()
*/
doAnalysis(kotlinArgs)
/*
OLD: KE1
loggerBase.printLimitedDiagnosticCounts(tw)
logPeakMemoryUsage(logger, "after extractor")
logger.info("Extraction completed")
logger.flush()
val compilationTimeMs = System.currentTimeMillis() - startTimeMs
tw.writeCompilation_finished(
compilation,
-1.0,
compilationTimeMs.toDouble() / 1000,
invocationExtractionProblems.extractionResult()
)
tw.flush()
loggerBase.close()
*/
}
}
fun doAnalysis(args : List<String>) {
/*
OLD: KE1
moduleFragment.files.mapIndexed { index: Int, file: IrFile ->
val fileExtractionProblems = FileExtractionProblems(invocationExtractionProblems)
val fileTrapWriter = tw.makeSourceFileTrapWriter(file, true)
loggerBase.setFileNumber(index)
fileTrapWriter.writeCompilation_compiling_files(
compilation,
index,
fileTrapWriter.fileId
)
doFile(
compression,
fileExtractionProblems,
invocationTrapFile,
fileTrapWriter,
checkTrapIdentical,
loggerBase,
trapDir,
srcDir,
file,
primitiveTypeMapping,
pluginContext,
globalExtensionState
)
fileTrapWriter.writeCompilation_compiling_files_completed(
compilation,
index,
fileExtractionProblems.extractionResult()
)
}
*/
lateinit var sourceModule: KaSourceModule
val k2args : K2JVMCompilerArguments = parseCommandLineArguments(args.toList())
@@ -235,91 +325,6 @@ class KotlinExtractorExtension(
private val exitAfterExtraction: Boolean
) : IrGenerationExtension {
private fun runExtractor(moduleFragment: IrModuleFragment, pluginContext: IrPluginContext) {
val usesK2 = usesK2(pluginContext)
[...]
val extractor_name =
this::class.java.getResource("extractor.name")?.readText() ?: "<unknown>"
tw.writeCompilation_info(compilation, "Kotlin Extractor Name", extractor_name)
tw.writeCompilation_info(compilation, "Uses Kotlin 2", usesK2.toString())
if (compilationStartTime != null) {
tw.writeCompilation_compiler_times(
compilation,
-1.0,
(System.currentTimeMillis() - compilationStartTime) / 1000.0
)
}
tw.flush()
val logger = Logger(loggerBase, tw)
logger.info("Extraction started")
logger.flush()
logger.info("Extraction for invocation TRAP file $invocationTrapFile")
logger.flush()
logger.info("Kotlin version ${KotlinCompilerVersion.getVersion()}")
logger.flush()
logPeakMemoryUsage(logger, "before extractor")
if (System.getenv("CODEQL_EXTRACTOR_JAVA_KOTLIN_DUMP") == "true") {
logger.info("moduleFragment:\n" + moduleFragment.dump())
}
val compression = getCompression(logger)
val primitiveTypeMapping = PrimitiveTypeMapping(logger, pluginContext)
// FIXME: FileUtil expects a static global logger
// which should be provided by SLF4J's factory facility. For now we set it here.
FileUtil.logger = logger
val srcDir =
File(
System.getenv("CODEQL_EXTRACTOR_JAVA_SOURCE_ARCHIVE_DIR").takeUnless {
it.isNullOrEmpty()
} ?: "kotlin-extractor/src"
)
srcDir.mkdirs()
val globalExtensionState = KotlinExtractorGlobalState()
moduleFragment.files.mapIndexed { index: Int, file: IrFile ->
val fileExtractionProblems = FileExtractionProblems(invocationExtractionProblems)
val fileTrapWriter = tw.makeSourceFileTrapWriter(file, true)
loggerBase.setFileNumber(index)
fileTrapWriter.writeCompilation_compiling_files(
compilation,
index,
fileTrapWriter.fileId
)
doFile(
compression,
fileExtractionProblems,
invocationTrapFile,
fileTrapWriter,
checkTrapIdentical,
loggerBase,
trapDir,
srcDir,
file,
primitiveTypeMapping,
pluginContext,
globalExtensionState
)
fileTrapWriter.writeCompilation_compiling_files_completed(
compilation,
index,
fileExtractionProblems.extractionResult()
)
}
loggerBase.printLimitedDiagnosticCounts(tw)
logPeakMemoryUsage(logger, "after extractor")
logger.info("Extraction completed")
logger.flush()
val compilationTimeMs = System.currentTimeMillis() - startTimeMs
tw.writeCompilation_finished(
compilation,
-1.0,
compilationTimeMs.toDouble() / 1000,
invocationExtractionProblems.extractionResult()
)
tw.flush()
loggerBase.close()
}
}
private fun getCompression(logger: Logger): Compression {
val compression_env_var = "CODEQL_EXTRACTOR_JAVA_OPTION_TRAP_COMPRESSION"
val compression_option = System.getenv(compression_env_var)

View File

@@ -209,8 +209,6 @@ OLD: KE1
bw.write(trap)
}
/*
OLD: KE1
/** Write a comment into the TRAP file. */
fun writeComment(comment: String) {
writeTrap("// ${comment.replace("\n", "\n// ")}\n")
@@ -220,7 +218,6 @@ OLD: KE1
fun flush() {
bw.flush()
}
*/
/**
* Escape a string so that it can be used in a TRAP string literal, i.e. with `"` escaped as

View File

@@ -1,13 +1,13 @@
package com.github.codeql
/*
OLD: KE1
import java.io.File
import java.io.FileWriter
import java.io.OutputStreamWriter
import java.io.Writer
import java.text.SimpleDateFormat
import java.util.Date
/*
OLD: KE1
import java.util.Stack
import org.jetbrains.kotlin.ir.IrElement
*/
@@ -46,8 +46,10 @@ enum class Severity(val sev: Int) {
ErrorGlobal(8)
}
/*
OLD: KE1
/**
* Given a log message, this wrapper class adds info like a timestamp,
* and formats it for the different output targets.
*/
class LogMessage(private val kind: String, private val message: String) {
val timestamp: String
@@ -59,7 +61,7 @@ class LogMessage(private val kind: String, private val message: String) {
return "[$timestamp K] [$kind] $message"
}
private fun escape(str: String): String {
private fun escapeForJson(str: String): String {
return str.replace("\\", "\\\\")
.replace("\"", "\\\"")
.replace("\u0000", "\\u0000")
@@ -89,11 +91,13 @@ class LogMessage(private val kind: String, private val message: String) {
Pair("message", message)
)
return "{ " +
kvs.map { p -> "\"${p.first}\": \"${escape(p.second)}\"" }.joinToString(", ") +
kvs.map { p -> "\"${p.first}\": \"${escapeForJson(p.second)}\"" }.joinToString(", ") +
" }\n"
}
}
/*
OLD: KE1
data class ExtractorContext(
val kind: String,
val element: IrElement,
@@ -120,8 +124,6 @@ OLD: KE1
verbosity = System.getenv("CODEQL_EXTRACTOR_KOTLIN_VERBOSITY")?.toIntOrNull() ?: 3
}
/*
OLD: KE1
private val logStream: Writer
init {
@@ -134,6 +136,8 @@ OLD: KE1
}
}
/*
OLD: KE1
private fun getDiagnosticLocation(): String? {
val st = Exception().stackTrace
for (x in st) {
@@ -245,7 +249,7 @@ OLD: KE1
logStream.write(logMessage.toJsonLine())
}
fun trace(tw: TrapWriter, msg: String) {
fun trace(dtw: DiagnosticTrapWriter, msg: String) {
if (verbosity >= 4) {
val logMessage = LogMessage("TRACE", msg)
tw.writeComment(logMessage.toText())
@@ -253,22 +257,22 @@ OLD: KE1
}
}
fun debug(tw: TrapWriter, msg: String) {
fun debug(dtw: DiagnosticTrapWriter, msg: String) {
if (verbosity >= 4) {
val logMessage = LogMessage("DEBUG", msg)
tw.writeComment(logMessage.toText())
logStream.write(logMessage.toJsonLine())
}
}
*/
fun info(tw: TrapWriter, msg: String) {
fun info(dtw: DiagnosticTrapWriter, msg: String) {
if (verbosity >= 3) {
val logMessage = LogMessage("INFO", msg)
tw.writeComment(logMessage.toText())
dtw.writeComment(logMessage.toText())
logStream.write(logMessage.toJsonLine())
}
}
*/
fun warn(dtw: DiagnosticTrapWriter, msg: String, extraInfo: String?) {
if (verbosity >= 2) {
@@ -311,9 +315,12 @@ OLD: KE1
*/
}
/**
* Logger is the high-level interface for writint log messages.
*/
open class Logger(val loggerBase: LoggerBase, val dtw: DiagnosticTrapWriter) {
/*
OLD: KE1
open class Logger(val loggerBase: LoggerBase, open val dtw: DiagnosticTrapWriter) {
fun flush() {
dtw.flush()
loggerBase.flush()
@@ -330,11 +337,14 @@ open class Logger(val loggerBase: LoggerBase, open val dtw: DiagnosticTrapWriter
fun debug(msg: String) {
loggerBase.debug(dtw, msg)
}
*/
fun info(msg: String) {
loggerBase.info(dtw, msg)
}
/*
OLD: KE1
private fun warn(msg: String, extraInfo: String?) {
loggerBase.warn(dtw, msg, extraInfo)
}
@@ -358,8 +368,11 @@ open class Logger(val loggerBase: LoggerBase, open val dtw: DiagnosticTrapWriter
fun error(msg: String, exn: Throwable) {
error(msg, exn.stackTraceToString())
}
*/
}
/*
OLD: KE1
class FileLogger(loggerBase: LoggerBase, val ftw: FileTrapWriter) :
Logger(loggerBase, ftw.getDiagnosticTrapWriter()) {
fun warnElement(msg: String, element: IrElement, exn: Throwable? = null) {