From 746cc63e633a482caae276fc7ff57fb4f21b1faa Mon Sep 17 00:00:00 2001 From: Mathias-Boulay Date: Wed, 17 May 2023 17:40:13 +0200 Subject: [PATCH] Fix[log]: Unmovable file --- .../pojavlaunch/JavaGUILauncherActivity.java | 2 +- .../main/java/net/kdt/pojavlaunch/Logger.java | 38 +++++++++++++++---- .../net/kdt/pojavlaunch/MainActivity.java | 2 +- app_pojavlauncher/src/main/jni/stdio_is.c | 6 +-- 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/JavaGUILauncherActivity.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/JavaGUILauncherActivity.java index 2b73bb817..a416b5923 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/JavaGUILauncherActivity.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/JavaGUILauncherActivity.java @@ -55,7 +55,7 @@ public class JavaGUILauncherActivity extends BaseActivity implements View.OnTouc super.onCreate(savedInstanceState); setContentView(R.layout.activity_java_gui_launcher); - Logger.begin(new File(Tools.DIR_GAME_HOME, "latestlog.txt").getAbsolutePath()); + Logger.beginLog(new File(Tools.DIR_GAME_HOME, "latestlog.txt")); mTouchCharInput = findViewById(R.id.awt_touch_char); mTouchCharInput.setCharacterSender(new AwtCharSender()); diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Logger.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Logger.java index 168f83d14..e7672b5ce 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Logger.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Logger.java @@ -2,23 +2,47 @@ package net.kdt.pojavlaunch; import androidx.annotation.Keep; -/** Singleton class made to log on one file +import java.io.File; +import java.io.IOException; + +/** + * Singleton class made to log on one file * The singleton part can be removed but will require more implementation from the end-dev */ @Keep public class Logger { - /** Print the text to the log file if not censored */ + /** + * Print the text to the log file if not censored + */ public static native void appendToLog(String text); + public static void beginLog(File logFile) { + try { + if (logFile.exists()) + logFile.delete(); - /** Reset the log file, effectively erasing any previous logs */ + logFile.createNewFile(); + } catch (IOException e) { + throw new RuntimeException(e); + } + + Logger.begin(logFile.getAbsolutePath()); + } + + /** + * Reset the log file, effectively erasing any previous logs + */ public static native void begin(String logFilePath); - /** Small listener for anything listening to the log */ + /** + * Link a log listener to the logger + */ + public static native void setLogListener(eventLogListener logListener); + + /** + * Small listener for anything listening to the log + */ public interface eventLogListener { void onEventLogged(String text); } - - /** Link a log listener to the logger */ - public static native void setLogListener(eventLogListener logListener); } diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/MainActivity.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/MainActivity.java index dab0f67b8..50c8d2469 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/MainActivity.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/MainActivity.java @@ -133,7 +133,7 @@ public class MainActivity extends BaseActivity implements ControlButtonMenuListe drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); try { - Logger.begin(new File(Tools.DIR_GAME_HOME, "latestlog.txt").getAbsolutePath()); + Logger.beginLog(new File(Tools.DIR_GAME_HOME, "latestlog.txt")); // FIXME: is it safe for multi thread? GLOBAL_CLIPBOARD = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); touchCharInput.setCharacterSender(new LwjglCharSender()); diff --git a/app_pojavlauncher/src/main/jni/stdio_is.c b/app_pojavlauncher/src/main/jni/stdio_is.c index 5986ed4f6..2dd50d9a0 100644 --- a/app_pojavlauncher/src/main/jni/stdio_is.c +++ b/app_pojavlauncher/src/main/jni/stdio_is.c @@ -84,9 +84,9 @@ Java_net_kdt_pojavlaunch_Logger_begin(JNIEnv *env, __attribute((unused)) jclass dup2(pfd[1], 2); /* open latestlog.txt for writing */ - const char* logFilePath = (*env)->GetStringUTFChars(env, logPath, NULL); - latestlog_fd = open(logFilePath, O_WRONLY | O_TRUNC | O_CREAT, 644); - if(latestlog_fd == -1) { + const char *logFilePath = (*env)->GetStringUTFChars(env, logPath, NULL); + latestlog_fd = open(logFilePath, O_WRONLY | O_APPEND, 644); + if (latestlog_fd == -1) { latestlog_fd = 0; (*env)->ThrowNew(env, ioeClass, strerror(errno)); return;