Skip to content

Commit

Permalink
refactor: custom logging solution
Browse files Browse the repository at this point in the history
  • Loading branch information
jenspots committed Apr 26, 2024
1 parent fae5f6b commit cdd7a39
Show file tree
Hide file tree
Showing 25 changed files with 318 additions and 171 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions src/main/java/runner/Processor.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package runner;

import technology.idlab.logging.Log;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Logger;

import static technology.idlab.logging.LoggerKt.createLogger;

public abstract class Processor {
/**
* Processors which wish to log messages should use the logger provided by
* the template class. This logger is created with the name of the class
* which extends the template.
*/
protected final Logger logger = createLogger();
protected final Log log = Log.Companion.getShared();

/**
* The arguments of a processor are stored in a map and can be accessed by
Expand All @@ -40,10 +39,10 @@ protected <T> Optional<T> getOptionalArgument(String name) {
}

public void setup() {
logger.info("Setting up processor");
log.info("Setting up processor");
}

public void exec() {
logger.info("Executing processor");
log.info("Executing processor");
}
}
12 changes: 5 additions & 7 deletions src/main/kotlin/compiler/Compiler.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package technology.idlab.compiler

import technology.idlab.logging.createLogger
import technology.idlab.logging.fatal
import technology.idlab.logging.Log
import java.io.File
import java.io.PrintWriter
import javax.tools.DiagnosticCollector
import javax.tools.JavaFileObject
import javax.tools.ToolProvider

class Compiler {
private val logger = createLogger()
private val compiler =
ToolProvider.getSystemJavaCompiler() ?: logger.fatal(
ToolProvider.getSystemJavaCompiler() ?: Log.shared.fatal(
"No Java compiler found.",
)

fun compile(file: File): ByteArray {
logger.info("Compiling file://${file.absolutePath}")
Log.shared.info("Compiling file://${file.absolutePath}")

// Prepare compilation.
val files = listOf(file)
Expand All @@ -40,11 +38,11 @@ class Compiler {

// Write diagnostics to logger.
diagnosticCollector.diagnostics.forEach {
logger.info(it.toString())
Log.shared.info(it.toString())
}

if (!success) {
logger.fatal("ERROR: compilation failed")
Log.shared.fatal("ERROR: compilation failed")
}

return results.get(file.nameWithoutExtension)
Expand Down
9 changes: 3 additions & 6 deletions src/main/kotlin/compiler/MemoryClassLoader.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package technology.idlab.compiler

import technology.idlab.logging.createLogger
import technology.idlab.logging.fatal
import technology.idlab.logging.Log

class MemoryClassLoader : ClassLoader() {
private val logger = createLogger()

fun fromBytes(bytes: ByteArray, name: String): Class<*> {
logger.info("Loading class $name")
Log.shared.info("Loading class $name")

return try {
defineClass(name, bytes, 0, bytes.size)
} catch (e: ClassFormatError) {
logger.fatal("Failed to load class $name")
Log.shared.fatal("Failed to load class $name")
}
}
}
6 changes: 2 additions & 4 deletions src/main/kotlin/compiler/MemoryFileManager.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package technology.idlab.compiler

import technology.idlab.logging.createLogger
import technology.idlab.logging.fatal
import technology.idlab.logging.Log
import java.io.ByteArrayOutputStream
import java.io.OutputStream
import java.net.URI
Expand All @@ -19,7 +18,6 @@ class MemoryFileManager(
fileManager: JavaFileManager,
) : ForwardingJavaFileManager<JavaFileManager>(fileManager) {
private val results: MutableMap<String, ByteArray> = HashMap()
private val logger = createLogger()

override fun getJavaFileForOutput(
location: JavaFileManager.Location?,
Expand All @@ -46,6 +44,6 @@ class MemoryFileManager(
}

fun get(className: String): ByteArray {
return results[className] ?: logger.fatal("Class $className not found")
return results[className] ?: Log.shared.fatal("Class $className not found")
}
}
Loading

0 comments on commit cdd7a39

Please sign in to comment.