Просмотр исходного кода

增加了log输出到SNGL/log目录下的功能

Snow 4 недель назад
Родитель
Сommit
c910f03480
1 измененных файлов с 70 добавлено и 33 удалено
  1. 70 33
      src/main/java/com/snowchen/snglfx17/HelloController.java

+ 70 - 33
src/main/java/com/snowchen/snglfx17/HelloController.java

@@ -13,9 +13,13 @@ import org.to2mbn.jmccc.launch.LaunchException;
 import oshi.hardware.GlobalMemory;
 
 import java.io.*;
+import java.text.SimpleDateFormat;
+import java.util.Date;
 import java.util.Objects;
 
 public class HelloController {
+    private String logFilePath;
+
     /**
      * 初始化方法,用于设置控制台输出重定向、初始化Java环境和配置
      * 此方法依次重定向系统输出和错误流到自定义的ConsoleOutputStream,
@@ -25,11 +29,16 @@ public class HelloController {
      *
      * @throws InterruptedException 如果在初始化过程中线程被中断
      */
-    public void initialize() throws InterruptedException {
-        // 将标准输出流重定向到TextArea
-        System.setOut(new ConsoleOutputStream(System.out, LogArea::appendText));
-        // 将标准错误流重定向到TextArea
-        System.setErr(new ConsoleOutputStream(System.err, LogArea::appendText));
+    public void initialize() throws InterruptedException, IOException {
+        // 初始化日志文件路径
+        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
+        logFilePath = "SNGL/log/" + dateFormat.format(new Date()) + ".log";
+        System.out.println("日志文件路径: " + logFilePath);
+
+        // 将标准输出流重定向到TextArea和文件
+        System.setOut(new ConsoleOutputStream(System.out, LogArea::appendText, logFilePath));
+        // 将标准错误流重定向到TextArea和文件
+        System.setErr(new ConsoleOutputStream(System.err, LogArea::appendText, logFilePath));
 
         // 打印应用程序启动的ASCII艺术
         System.out.println(" ____  _   _  ____ _     \n" +
@@ -65,6 +74,51 @@ public class HelloController {
         } else {
             System.out.println("文件 " + fileName + " 已存在于 " + directoryPath + " 目录下");
         }
+        // 定义SNGL目录下的log目录名
+        String logDirectoryName = "log";
+        String logDirectoryPath = directoryPath + File.separator + logDirectoryName;
+
+        // 创建SNGL目录下的log目录,如果不存在的话
+        ConfigCore.CreateDirectory.createDirectoryIfNotExists(logDirectoryPath);
+    }
+
+    private static class ConsoleOutputStream extends java.io.PrintStream {
+        private java.io.PrintStream original;
+        private java.util.function.Consumer<String> consumer;
+        private java.io.FileWriter fileWriter;
+
+        public ConsoleOutputStream(java.io.PrintStream original, java.util.function.Consumer<String> consumer, String filePath) throws IOException {
+            super(original);
+            this.original = original;
+            this.consumer = consumer;
+            this.fileWriter = new java.io.FileWriter(filePath, true); // 追加模式
+        }
+
+        @Override
+        public void println(final String x) {
+            original.println(x); // 保持原始输出功能
+            Platform.runLater(() -> consumer.accept(x + "\n")); // 在JavaFX应用程序线程上更新TextArea
+            try {
+                fileWriter.append(x + "\n"); // 写入文件
+            } catch (IOException e) {
+                throw new RuntimeException(e);
+            }
+            try {
+                fileWriter.flush(); // 刷新缓冲区,确保写入
+            } catch (IOException e) {
+                throw new RuntimeException(e);
+            }
+        }
+
+        @Override
+        public void close() {
+            super.close();
+            try {
+                fileWriter.close(); // 关闭文件写入流
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
     }
 
     /**
@@ -88,10 +142,10 @@ public class HelloController {
     }
 
     @FXML//启动按钮实现
-/**
- * 当用户点击启动按钮时调用此方法
- * 它收集用户输入,如玩家名称、游戏版本、Java环境设置等,并启动游戏
- */
+    /**
+     * 当用户点击启动按钮时调用此方法
+     * 它收集用户输入,如玩家名称、游戏版本、Java环境设置等,并启动游戏
+     */
     protected void StartButtonClick() {
         // 获取玩家名称输入框中的文本,并打印
         LaunchCore.Player_Name = PlayerNameInput.getText();
@@ -168,12 +222,12 @@ public class HelloController {
         }
     }
 
-
     @FXML//刷新游戏文件夹按钮
     protected void ReloadGameDirectoryClick(){
         InitializeVersionChoice();
         System.out.println("游戏文件夹已重载");
     }
+
     private String[] VersionList = {};
     private void InitializeVersionChoice() {
         // 指定目录路径
@@ -215,6 +269,7 @@ public class HelloController {
             }
         });
     }
+
     private void InitializeMaxMem(){//初始化最大内存设置
         GlobalMemory memory = OshiUtil.getMemory();
         double memoryTotal = memory.getTotal()/ (1024 * 1024);
@@ -224,6 +279,7 @@ public class HelloController {
         MaxMemInput.setText(String.valueOf(SetMaxMem.getValue()));
         LaunchCore.Max_Mem = (int) SetMaxMem.getValue();
     }
+
     @FXML
     private TextArea LogArea;//日志输出框
 
@@ -240,28 +296,13 @@ public class HelloController {
         }
     }
 
-    private static class ConsoleOutputStream extends java.io.PrintStream {
-        private java.io.PrintStream original;
-        private java.util.function.Consumer<String> consumer;
-
-        public ConsoleOutputStream(java.io.PrintStream original, java.util.function.Consumer<String> consumer) {
-            super(original);
-            this.original = original;
-            this.consumer = consumer;
-        }
-
-        @Override
-        public void println(final String x) {
-            original.println(x); // 保持原始输出功能
-            Platform.runLater(() -> consumer.accept(x + "\n")); // 在JavaFX应用程序线程上更新TextArea
-        }
-    }
     private void SetMaxMemListen() {//监听并更改滑块
         SetMaxMem.valueProperty().addListener((observable, oldValue, newValue) -> {
-            MaxMemInput.setText(String.valueOf( newValue));
+            MaxMemInput.setText(String.valueOf(newValue));
             LaunchCore.Max_Mem = (int) SetMaxMem.getValue();
         });
     }
+
     /**
      * 初始化Java环境方法
      * 该方法主要用于检测当前系统的Java版本和安装路径,
@@ -300,14 +341,15 @@ public class HelloController {
     private void DataSave(){
 
     }
+
     @FXML
     protected void TestDownload(){
         System.out.println("开始下载1.20.1");
         DownloadCore.DownloadMinecraftVersion();
         //DownloadCore.DownloadRemoteVersionList();
         InitializeVersionChoice();
-
     }
+
     @FXML
     private CheckBox SetFullScreen;
 
@@ -348,9 +390,4 @@ public class HelloController {
 
     @FXML
     private Button ManualSave;
-
-
-
 }
-
-