aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni
diff options
context:
space:
mode:
authorCalMWolfs <94038482+CalMWolfs@users.noreply.github.com>2023-11-18 10:38:42 +1100
committerGitHub <noreply@github.com>2023-11-18 00:38:42 +0100
commitade5b675c2c4bb703a0698c016a1f833d20247d9 (patch)
tree66f7861e158c80b48ea823e597e6fb6716f44d31 /src/main/java/at/hannibal2/skyhanni
parent0dbe01932333c89ed1e6ae19f0cb950e09315a63 (diff)
downloadskyhanni-ade5b675c2c4bb703a0698c016a1f833d20247d9.tar.gz
skyhanni-ade5b675c2c4bb703a0698c016a1f833d20247d9.tar.bz2
skyhanni-ade5b675c2c4bb703a0698c016a1f833d20247d9.zip
add log clearing (#698)
Add log clearing. #698
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/config/features/dev/DevConfig.java6
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/LorenzLogger.kt38
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
}