aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/gui/config/storage/ConfigLoadContext.kt
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2025-09-14 16:37:57 +0200
committerLinnea Gräf <nea@nea.moe>2025-09-14 16:37:57 +0200
commit9abe9f46f04f188037687adb2740b32220ad21b2 (patch)
tree48dbd9cdf48c59853310c0b2e9bc59801522400e /src/main/kotlin/gui/config/storage/ConfigLoadContext.kt
parent2851c1d6834fafdaeb009dce2a3485df1388907e (diff)
downloadFirmament-9abe9f46f04f188037687adb2740b32220ad21b2.tar.gz
Firmament-9abe9f46f04f188037687adb2740b32220ad21b2.tar.bz2
Firmament-9abe9f46f04f188037687adb2740b32220ad21b2.zip
snapshot
Diffstat (limited to 'src/main/kotlin/gui/config/storage/ConfigLoadContext.kt')
-rw-r--r--src/main/kotlin/gui/config/storage/ConfigLoadContext.kt65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/main/kotlin/gui/config/storage/ConfigLoadContext.kt b/src/main/kotlin/gui/config/storage/ConfigLoadContext.kt
new file mode 100644
index 0000000..59afaa1
--- /dev/null
+++ b/src/main/kotlin/gui/config/storage/ConfigLoadContext.kt
@@ -0,0 +1,65 @@
+package moe.nea.firmament.gui.config.storage
+
+import java.io.PrintWriter
+import java.nio.file.Path
+import org.apache.commons.io.output.StringBuilderWriter
+import kotlin.io.path.Path
+import kotlin.io.path.createParentDirectories
+import kotlin.io.path.writeText
+import moe.nea.firmament.Firmament
+
+data class ConfigLoadContext(
+ val loadId: String,
+) : AutoCloseable {
+ val logFile = Path("logs")
+ .resolve(Firmament.MOD_ID)
+ .resolve("config-$loadId.log")
+ .toAbsolutePath()
+ val logBuffer = StringBuilder()
+
+ var shouldSaveLogBuffer = false
+ fun markShouldSaveLogBuffer() {
+ shouldSaveLogBuffer = true
+ }
+
+ fun logDebug(message: String) {
+ logBuffer.append("[DEBUG] ").append(message).appendLine()
+ }
+
+ fun logInfo(message: String) {
+ Firmament.logger.info("[ConfigUpgrade] $message")
+ logBuffer.append("[INFO] ").append(message).appendLine()
+ }
+
+ fun logError(message: String, exception: Throwable) {
+ markShouldSaveLogBuffer()
+ Firmament.logger.error("[ConfigUpgrade] $message", exception)
+ logBuffer.append("[ERROR] ").append(message).appendLine()
+ PrintWriter(StringBuilderWriter(logBuffer)).use {
+ exception.printStackTrace(it)
+ }
+ logBuffer.appendLine()
+ }
+
+ fun logError(message: String) {
+ markShouldSaveLogBuffer()
+ Firmament.logger.error("[ConfigUpgrade] $message")
+ logBuffer.append("[ERROR] ").append(message).appendLine()
+ }
+
+ fun ensureWritable(path: Path) {
+ path.createParentDirectories()
+ }
+
+ override fun close() {
+ logInfo("Closing out config load.")
+ if (shouldSaveLogBuffer) {
+ try {
+ ensureWritable(logFile)
+ logFile.writeText(logBuffer.toString())
+ } catch (ex: Exception) {
+ logError("Could not save config load log", ex)
+ }
+ }
+ }
+}