forked from ChomeNS/chomens-bot-java
move to Files
This commit is contained in:
parent
8fe9c9e0c7
commit
f0f11f19b6
2 changed files with 66 additions and 49 deletions
|
@ -2,9 +2,14 @@ package land.chipmunk.chayapak.chomens_bot.util;
|
||||||
|
|
||||||
import land.chipmunk.chayapak.chomens_bot.Main;
|
import land.chipmunk.chayapak.chomens_bot.Main;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.BufferedReader;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.nio.file.Files;
|
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.LocalDate;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
|
@ -14,9 +19,10 @@ import java.util.zip.GZIPOutputStream;
|
||||||
|
|
||||||
// totallynotskidded™ from HBot
|
// totallynotskidded™ from HBot
|
||||||
public class FileLoggerUtilities {
|
public class FileLoggerUtilities {
|
||||||
public static final File logDir = new File("logs");
|
public static final Path logDirectory = Path.of("logs");
|
||||||
public static final File logFile = new File(logDir, "log.txt");
|
public static final Path logPath = Paths.get(logDirectory.toString(), "log.txt");
|
||||||
public static OutputStreamWriter logWriter;
|
|
||||||
|
public static BufferedWriter logWriter;
|
||||||
|
|
||||||
public static LocalDate currentLogDate;
|
public static LocalDate currentLogDate;
|
||||||
public static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("'['dd/MM/yyyy HH:mm:ss']' ");
|
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;
|
public static long freezeTime = 0;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void init() {
|
||||||
try {
|
try {
|
||||||
init();
|
if (!Files.exists(logDirectory)) Files.createDirectory(logDirectory);
|
||||||
|
|
||||||
|
if (!Files.exists(logPath)) {
|
||||||
|
makeNewLogFile();
|
||||||
|
} else if (!logIsCurrent(logPath)) {
|
||||||
|
compressLogFile();
|
||||||
|
makeNewLogFile();
|
||||||
|
} else {
|
||||||
|
openLogFile();
|
||||||
|
}
|
||||||
|
|
||||||
executor.scheduleAtFixedRate(() -> {
|
executor.scheduleAtFixedRate(() -> {
|
||||||
try {
|
try {
|
||||||
tick();
|
tick();
|
||||||
|
@ -38,23 +58,6 @@ public class FileLoggerUtilities {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}, 0, 50, TimeUnit.MILLISECONDS);
|
}, 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) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -64,6 +67,7 @@ public class FileLoggerUtilities {
|
||||||
if (freezeTime <= System.currentTimeMillis() && spamLevel > 0) {
|
if (freezeTime <= System.currentTimeMillis() && spamLevel > 0) {
|
||||||
spamLevel--;
|
spamLevel--;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!currentLogDate.equals(LocalDate.now())) {
|
if (!currentLogDate.equals(LocalDate.now())) {
|
||||||
try {
|
try {
|
||||||
compressLogFile();
|
compressLogFile();
|
||||||
|
@ -74,26 +78,32 @@ public class FileLoggerUtilities {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static synchronized void makeNewLogFile() throws IOException {
|
public static void makeNewLogFile() throws IOException {
|
||||||
currentLogDate = LocalDate.now();
|
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.write(currentLogDate.toString() + '\n');
|
||||||
logWriter.flush();
|
logWriter.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static synchronized void openLogFile() throws IOException {
|
public static void openLogFile() throws IOException {
|
||||||
currentLogDate = LocalDate.parse(getLogDate(logFile));
|
currentLogDate = LocalDate.parse(getLogDate(logPath));
|
||||||
logWriter = new OutputStreamWriter(new FileOutputStream(logFile, true), StandardCharsets.UTF_8);
|
logWriter = Files.newBufferedWriter(logPath, StandardOpenOption.APPEND);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static synchronized void compressLogFile() throws IOException {
|
public static void compressLogFile() throws IOException {
|
||||||
if (Files.size(logFile.toPath()) > 100 * 1024 * 1024) { // Will not save because log file is too big
|
if (Files.size(logPath) > 100 * 1024 * 1024) { // Will not save because log file is too big
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
File gzipFile = new File(logDir, getLogDate(logFile) + ".txt.gz");
|
final Path path = Paths.get(logDirectory.toString(), getLogDate(logPath) + ".txt.gz");
|
||||||
FileInputStream in = new FileInputStream(logFile);
|
|
||||||
GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzipFile));
|
Files.createFile(path);
|
||||||
|
|
||||||
|
InputStream in = Files.newInputStream(logPath);
|
||||||
|
GZIPOutputStream out = new GZIPOutputStream(Files.newOutputStream(path));
|
||||||
|
|
||||||
byte[] buffer = new byte[1024];
|
byte[] buffer = new byte[1024];
|
||||||
int size;
|
int size;
|
||||||
|
@ -105,20 +115,19 @@ public class FileLoggerUtilities {
|
||||||
out.close();
|
out.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static synchronized String getLogDate(File file) throws IOException {
|
public static String getLogDate(Path path) throws IOException {
|
||||||
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);
|
BufferedReader reader = Files.newBufferedReader(path);
|
||||||
BufferedReader reader = new BufferedReader(isr);
|
|
||||||
String date = reader.readLine();
|
String date = reader.readLine();
|
||||||
reader.close();
|
reader.close();
|
||||||
return date;
|
return date;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static synchronized boolean logIsCurrent(File file) throws IOException {
|
public static boolean logIsCurrent(Path path) throws IOException {
|
||||||
LocalDate date = LocalDate.now();
|
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()) {
|
if (freezeTime > System.currentTimeMillis()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,15 +6,24 @@ import com.google.gson.JsonObject;
|
||||||
import com.google.gson.JsonPrimitive;
|
import com.google.gson.JsonPrimitive;
|
||||||
import land.chipmunk.chayapak.chomens_bot.Main;
|
import land.chipmunk.chayapak.chomens_bot.Main;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.BufferedReader;
|
||||||
import java.util.*;
|
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.ScheduledFuture;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
public class PersistentDataUtilities {
|
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();
|
public static JsonObject jsonObject = new JsonObject();
|
||||||
|
|
||||||
|
@ -56,19 +65,18 @@ public class PersistentDataUtilities {
|
||||||
|
|
||||||
private static void init () {
|
private static void init () {
|
||||||
try {
|
try {
|
||||||
if (!file.exists()) file.createNewFile();
|
if (!Files.exists(path)) Files.createFile(path);
|
||||||
|
|
||||||
// loads the persistent data from the last session
|
// loads the persistent data from the last session
|
||||||
else {
|
else {
|
||||||
final InputStream opt = new FileInputStream(file);
|
final BufferedReader reader = Files.newBufferedReader(path);
|
||||||
final BufferedReader reader = new BufferedReader(new InputStreamReader(opt));
|
|
||||||
|
|
||||||
final Gson gson = new Gson();
|
final Gson gson = new Gson();
|
||||||
|
|
||||||
jsonObject = gson.fromJson(reader, JsonObject.class);
|
jsonObject = gson.fromJson(reader, JsonObject.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
writer = new FileWriter(file, false);
|
writer = Files.newBufferedWriter(path, StandardOpenOption.WRITE);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -78,9 +86,9 @@ public class PersistentDataUtilities {
|
||||||
try {
|
try {
|
||||||
writer.close();
|
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?
|
// or is this the only way?
|
||||||
writer = new FileWriter(file, false);
|
writer = Files.newBufferedWriter(path, StandardOpenOption.WRITE);
|
||||||
|
|
||||||
writer.write(string);
|
writer.write(string);
|
||||||
writer.flush();
|
writer.flush();
|
||||||
|
|
Loading…
Reference in a new issue