diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/config/features/dev/DevConfig.java | 6 | ||||
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/LorenzLogger.kt | 38 |
2 files changed, 44 insertions, 0 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/config/features/dev/DevConfig.java b/src/main/java/at/hannibal2/skyhanni/config/features/dev/DevConfig.java index 7c5282cf4..0c1fc01ed 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/features/dev/DevConfig.java +++ b/src/main/java/at/hannibal2/skyhanni/config/features/dev/DevConfig.java @@ -7,6 +7,7 @@ import io.github.moulberry.moulconfig.annotations.Accordion; import io.github.moulberry.moulconfig.annotations.Category; import io.github.moulberry.moulconfig.annotations.ConfigEditorBoolean; import io.github.moulberry.moulconfig.annotations.ConfigEditorKeybind; +import io.github.moulberry.moulconfig.annotations.ConfigEditorSlider; import io.github.moulberry.moulconfig.annotations.ConfigOption; import org.lwjgl.input.Keyboard; @@ -19,6 +20,11 @@ public class DevConfig { public boolean repoAutoUpdate = true; @Expose + @ConfigOption(name = "Log Expiry Time", desc = "Deletes your SkyHanni logs after this time period in days.") + @ConfigEditorSlider(minValue = 1, maxValue = 30, minStep = 1) + public int logExpiryTime = 14; + + @Expose @ConfigOption(name = "Debug", desc = "") @Accordion public DebugConfig debug = new DebugConfig(); diff --git a/src/main/java/at/hannibal2/skyhanni/utils/LorenzLogger.kt b/src/main/java/at/hannibal2/skyhanni/utils/LorenzLogger.kt index a382a5149..e949742cd 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/LorenzLogger.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/LorenzLogger.kt @@ -1,20 +1,27 @@ package at.hannibal2.skyhanni.utils +import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.utils.LorenzUtils.formatCurrentTime +import kotlinx.coroutines.launch import java.io.File import java.io.IOException +import java.nio.file.Files +import java.nio.file.attribute.BasicFileAttributes import java.text.SimpleDateFormat import java.util.logging.FileHandler import java.util.logging.Formatter import java.util.logging.LogRecord import java.util.logging.Logger +import kotlin.time.Duration.Companion.days class LorenzLogger(filePath: String) { private val format = SimpleDateFormat("HH:mm:ss") private val fileName = "$PREFIX_PATH$filePath.log" companion object { + private var LOG_DIRECTORY = File("config/skyhanni/logs") private var PREFIX_PATH: String + var hasDone = false init { val format = SimpleDateFormat("yyyy_MM_dd/HH_mm_ss").formatCurrentTime() @@ -53,6 +60,37 @@ class LorenzLogger(filePath: String) { } catch (e: IOException) { e.printStackTrace() } + + if (!hasDone && LorenzUtils.onHypixel) { + hasDone = true + val directoryFiles = LOG_DIRECTORY.listFiles() ?: run { + println("log directory has no files") + return logger + } + SkyHanniMod.coroutineScope.launch { + val timeToDelete = SkyHanniMod.feature.dev.logExpiryTime.days + + for (file in directoryFiles) { + val path = file.toPath() + try { + val attributes = Files.readAttributes(path, BasicFileAttributes::class.java) + val creationTime = attributes.creationTime().toMillis() + val timeSinceCreation = SimpleTimeMark(creationTime).passedSince() + if (timeSinceCreation > timeToDelete) { + if (!file.deleteRecursively()) { + println("failed to delete directory: ${file.name}") + } + } + } catch (e: SecurityException) { + e.printStackTrace() + } catch (e: IOException) { + e.printStackTrace() + println("Error: Unable to get creation date.") + } + } + } + } + return logger } |