diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/util/FileLoggerUtilities.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/util/FileLoggerUtilities.java index 73ba71a..86b65e5 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/util/FileLoggerUtilities.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/util/FileLoggerUtilities.java @@ -2,9 +2,14 @@ package land.chipmunk.chayapak.chomens_bot.util; import land.chipmunk.chayapak.chomens_bot.Main; -import java.io.*; -import java.nio.charset.StandardCharsets; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStream; import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; @@ -14,9 +19,10 @@ import java.util.zip.GZIPOutputStream; // totallynotskidded™ from HBot public class FileLoggerUtilities { - public static final File logDir = new File("logs"); - public static final File logFile = new File(logDir, "log.txt"); - public static OutputStreamWriter logWriter; + public static final Path logDirectory = Path.of("logs"); + public static final Path logPath = Paths.get(logDirectory.toString(), "log.txt"); + + public static BufferedWriter logWriter; public static LocalDate currentLogDate; public static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("'['dd/MM/yyyy HH:mm:ss']' "); @@ -29,8 +35,22 @@ public class FileLoggerUtilities { public static long freezeTime = 0; static { + init(); + } + + public static void init() { try { - init(); + if (!Files.exists(logDirectory)) Files.createDirectory(logDirectory); + + if (!Files.exists(logPath)) { + makeNewLogFile(); + } else if (!logIsCurrent(logPath)) { + compressLogFile(); + makeNewLogFile(); + } else { + openLogFile(); + } + executor.scheduleAtFixedRate(() -> { try { tick(); @@ -38,23 +58,6 @@ public class FileLoggerUtilities { e.printStackTrace(); } }, 0, 50, TimeUnit.MILLISECONDS); - } catch (Exception e) { - e.printStackTrace(); - } - } - - public static void init() { - if (!logDir.exists()) logDir.mkdir(); - - try { - if (!logFile.exists()) { - makeNewLogFile(); - } else if (!logIsCurrent(logFile)) { - compressLogFile(); - makeNewLogFile(); - } else { - openLogFile(); - } } catch (IOException e) { e.printStackTrace(); } @@ -64,6 +67,7 @@ public class FileLoggerUtilities { if (freezeTime <= System.currentTimeMillis() && spamLevel > 0) { spamLevel--; } + if (!currentLogDate.equals(LocalDate.now())) { try { compressLogFile(); @@ -74,26 +78,32 @@ public class FileLoggerUtilities { } } - public static synchronized void makeNewLogFile() throws IOException { + public static void makeNewLogFile() throws IOException { currentLogDate = LocalDate.now(); - logWriter = new OutputStreamWriter(new FileOutputStream(logFile, false), StandardCharsets.UTF_8); + + Files.createFile(logPath); + + logWriter = Files.newBufferedWriter(logPath, StandardOpenOption.APPEND); logWriter.write(currentLogDate.toString() + '\n'); logWriter.flush(); } - public static synchronized void openLogFile() throws IOException { - currentLogDate = LocalDate.parse(getLogDate(logFile)); - logWriter = new OutputStreamWriter(new FileOutputStream(logFile, true), StandardCharsets.UTF_8); + public static void openLogFile() throws IOException { + currentLogDate = LocalDate.parse(getLogDate(logPath)); + logWriter = Files.newBufferedWriter(logPath, StandardOpenOption.APPEND); } - public static synchronized void compressLogFile() throws IOException { - if (Files.size(logFile.toPath()) > 100 * 1024 * 1024) { // Will not save because log file is too big + public static void compressLogFile() throws IOException { + if (Files.size(logPath) > 100 * 1024 * 1024) { // Will not save because log file is too big return; } - File gzipFile = new File(logDir, getLogDate(logFile) + ".txt.gz"); - FileInputStream in = new FileInputStream(logFile); - GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzipFile)); + final Path path = Paths.get(logDirectory.toString(), getLogDate(logPath) + ".txt.gz"); + + Files.createFile(path); + + InputStream in = Files.newInputStream(logPath); + GZIPOutputStream out = new GZIPOutputStream(Files.newOutputStream(path)); byte[] buffer = new byte[1024]; int size; @@ -105,20 +115,19 @@ public class FileLoggerUtilities { out.close(); } - public static synchronized String getLogDate(File file) throws IOException { - InputStreamReader isr = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8); - BufferedReader reader = new BufferedReader(isr); + public static String getLogDate(Path path) throws IOException { + BufferedReader reader = Files.newBufferedReader(path); String date = reader.readLine(); reader.close(); return date; } - public static synchronized boolean logIsCurrent(File file) throws IOException { + public static boolean logIsCurrent(Path path) throws IOException { LocalDate date = LocalDate.now(); - return getLogDate(file).equals(date.toString()); + return getLogDate(path).equals(date.toString()); } - public static synchronized void log(String str) { + public static void log(String str) { if (freezeTime > System.currentTimeMillis()) { return; } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/util/PersistentDataUtilities.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/util/PersistentDataUtilities.java index ddf7c9c..009a547 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/util/PersistentDataUtilities.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/util/PersistentDataUtilities.java @@ -6,15 +6,24 @@ import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; import land.chipmunk.chayapak.chomens_bot.Main; -import java.io.*; -import java.util.*; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; +import java.util.LinkedHashMap; +import java.util.Map; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; +import java.util.stream.Stream; public class PersistentDataUtilities { - public static final File file = new File("persistent.json"); + public static final Path path = Path.of("persistent.json"); - private static FileWriter writer; + public static Stream file; + + private static BufferedWriter writer; public static JsonObject jsonObject = new JsonObject(); @@ -56,19 +65,18 @@ public class PersistentDataUtilities { private static void init () { try { - if (!file.exists()) file.createNewFile(); + if (!Files.exists(path)) Files.createFile(path); // loads the persistent data from the last session else { - final InputStream opt = new FileInputStream(file); - final BufferedReader reader = new BufferedReader(new InputStreamReader(opt)); + final BufferedReader reader = Files.newBufferedReader(path); final Gson gson = new Gson(); jsonObject = gson.fromJson(reader, JsonObject.class); } - writer = new FileWriter(file, false); + writer = Files.newBufferedWriter(path, StandardOpenOption.WRITE); } catch (IOException e) { e.printStackTrace(); } @@ -78,9 +86,9 @@ public class PersistentDataUtilities { try { writer.close(); - // ? how do i clear the file contents without making a completely new FileWriter + // ? how do i clear the file contents without making a completely new writer? // or is this the only way? - writer = new FileWriter(file, false); + writer = Files.newBufferedWriter(path, StandardOpenOption.WRITE); writer.write(string); writer.flush();