From e47b34a47223bd695362075ad9cec894b2497861 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Tue, 3 Aug 2021 15:22:04 +0200 Subject: [PATCH] Improves output logging and fixes some bugs Fixes an error caused by the BuildTools directory not being created Creates functions for writing to files in CommonFunctions Adds some more error info to the log when BuildTools fails to download Fixes some typos Makes sure all visible text is logged --- .../minecraftserverlauncher/Main.java | 8 +--- .../server/Server.java | 2 +- .../userinterface/GUI.java | 14 +++++++ .../userinterface/MessageHandler.java | 29 +++++++++++++- .../userinterface/ServerLauncherGUI.java | 9 +---- .../userinterface/ServerLauncherMenu.java | 16 ++++---- .../utility/CommonFunctions.java | 38 +++++++++++++++++-- .../utility/JarBuilder.java | 15 +++++++- .../utility/JarDownloaderTest.java | 10 ++++- 9 files changed, 109 insertions(+), 32 deletions(-) diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/Main.java b/src/main/java/net/knarcraft/minecraftserverlauncher/Main.java index 80c060f..cd724bf 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/Main.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/Main.java @@ -5,12 +5,12 @@ import net.knarcraft.minecraftserverlauncher.profile.ServerLauncherController; import net.knarcraft.minecraftserverlauncher.server.Server; import net.knarcraft.minecraftserverlauncher.userinterface.ServerConsoles; import net.knarcraft.minecraftserverlauncher.userinterface.ServerLauncherGUI; +import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions; import net.knarcraft.minecraftserverlauncher.utility.Updater; import java.awt.*; import java.io.File; import java.io.IOException; -import java.io.PrintWriter; import java.net.URISyntaxException; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -35,11 +35,7 @@ public class Main { public static void main(String[] args) throws IOException { Updater.checkForUpdate(updateURL, updateChannel); - try (PrintWriter file = new PrintWriter(Main.getApplicationWorkDirectory() + File.separator + "latestrun.log")) { - file.print(""); - } catch (IOException e) { - e.printStackTrace(); - } + CommonFunctions.writeFile(Main.getApplicationWorkDirectory() + File.separator + "latestrun.log", ""); EventQueue.invokeLater(() -> { try { ServerConsoles.instantiate(); diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java b/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java index e200baa..dabbf09 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/server/Server.java @@ -181,7 +181,7 @@ public class Server { int serverNum = 0; for (Collection collection : controller.getCurrentProfile().getCollections()) { if (!collection.getServer().runServer(serverNum++ == 0)) { - controller.getGUI().setStatus("An error occurred. Start aborted"); + controller.getGUI().showError("An error occurred. Start aborted. Please check the BuildTools log."); try { Server.stop(); } catch (IOException | InterruptedException e) { diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/GUI.java b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/GUI.java index c56b869..7f120d9 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/GUI.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/GUI.java @@ -44,6 +44,20 @@ public interface GUI { */ void showMessage(String message); + /** + * Logs a message to the logfile + * + * @param message

The message to log

+ */ + void logMessage(String message); + + /** + * Logs an error to the logfile + * + * @param error

The error to log

+ */ + void logError(String error); + /** * Asks the user for a directory as a file object * diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/MessageHandler.java b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/MessageHandler.java index a2425c0..b2e277c 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/MessageHandler.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/MessageHandler.java @@ -1,7 +1,11 @@ package net.knarcraft.minecraftserverlauncher.userinterface; +import net.knarcraft.minecraftserverlauncher.Main; +import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions; + import javax.swing.*; import java.io.BufferedWriter; +import java.io.File; import java.io.IOException; import java.io.OutputStreamWriter; @@ -12,6 +16,7 @@ public abstract class MessageHandler implements GUI { private final boolean silent; private final BufferedWriter writer; + private final String logFile = Main.getApplicationWorkDirectory() + File.separator + "latestrun.log"; /*** * Initializes a new message handler @@ -23,11 +28,29 @@ public abstract class MessageHandler implements GUI { this.writer = new BufferedWriter(new OutputStreamWriter(System.out)); } + @Override + public void logMessage(String message) { + try { + CommonFunctions.appendFile(logFile, "[Info]: " + message); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Override + public void logError(String message) { + try { + CommonFunctions.appendFile(logFile, "[Error]: " + message); + } catch (IOException e) { + e.printStackTrace(); + } + } + @Override public void showError(String title, String message) { if (silent) { try { - writer.write("Error: "); + writer.write("[Error]: "); writer.write(message); writer.newLine(); writer.flush(); @@ -37,6 +60,7 @@ public abstract class MessageHandler implements GUI { } else { showJOptionPane(title, message, JOptionPane.ERROR_MESSAGE); } + logError(message); } @Override @@ -48,7 +72,7 @@ public abstract class MessageHandler implements GUI { public void showMessage(String title, String message) { if (silent) { try { - writer.write(message); + writer.write("[Info]: " + message); writer.newLine(); writer.flush(); } catch (IOException e) { @@ -57,6 +81,7 @@ public abstract class MessageHandler implements GUI { } else { showJOptionPane(title, message, JOptionPane.INFORMATION_MESSAGE); } + logMessage(message); } @Override diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerLauncherGUI.java b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerLauncherGUI.java index f77438f..00449f8 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerLauncherGUI.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerLauncherGUI.java @@ -14,9 +14,7 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileNotFoundException; -import java.io.FileWriter; import java.io.IOException; -import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import java.util.Scanner; @@ -104,12 +102,7 @@ public class ServerLauncherGUI extends MessageHandler implements ActionListener, @Override public void setStatus(String text) { this.lblStatuslabel.setText(text); - try (PrintWriter file = new PrintWriter(new FileWriter(Main.getApplicationWorkDirectory() + - File.separator + "latestrun.log", true))) { - file.println(text); - } catch (IOException e) { - e.printStackTrace(); - } + this.logMessage(text); } @Override diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerLauncherMenu.java b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerLauncherMenu.java index 9df7390..7213c17 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerLauncherMenu.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/ServerLauncherMenu.java @@ -115,7 +115,7 @@ public class ServerLauncherMenu implements ActionListener { } else if (actionSource == oldJavaCommandInfoMenuItem) { serverLauncherGUI.showMessage("Old Java command", serverLauncherGUI.getMessage("oldJavaCommandText")); } else if (actionSource == deleteBuiltJarsInfoMenuItem) { - serverLauncherGUI.showMessage("Delete build jar files", serverLauncherGUI.getMessage("deleteBuiltJarFilesText")); + serverLauncherGUI.showMessage("Delete built jar files", serverLauncherGUI.getMessage("deleteBuiltJarFilesText")); } else if (actionSource == aboutMenuItem) { serverLauncherGUI.showMessage("About", serverLauncherGUI.getMessage("aboutText")); } else if (actionSource == storyMenuItem) { @@ -134,7 +134,7 @@ public class ServerLauncherMenu implements ActionListener { downloadJarsCheckBoxMenuItem = createCheckBoxMenuItem("Download jars", mnOptions); javaCommandMenuItem = createMenuItem("Java command", mnOptions); oldJavaCommandMenuItem = createMenuItem("Old Java command", mnOptions); - deleteBuiltJarsMenuItem = createMenuItem("Delete build jar files", mnOptions); + deleteBuiltJarsMenuItem = createMenuItem("Delete built jar files", mnOptions); } /** @@ -151,7 +151,7 @@ public class ServerLauncherMenu implements ActionListener { downloadJarsMenuItem = createMenuItem("Download jars", mnOptionsInfo); javaCommandInfoMenuItem = createMenuItem("Java command", mnOptionsInfo); oldJavaCommandInfoMenuItem = createMenuItem("Old Java command", mnOptionsInfo); - deleteBuiltJarsInfoMenuItem = createMenuItem("Delete build jar files", mnOptionsInfo); + deleteBuiltJarsInfoMenuItem = createMenuItem("Delete built jar files", mnOptionsInfo); JMenu mnAbout = new JMenu("About"); mnInfo.add(mnAbout); @@ -210,14 +210,14 @@ public class ServerLauncherMenu implements ActionListener { * Deletes build Spigot and Bukkit .jar files if the user accepts */ private void deleteBuiltJars() { - int answer = JOptionPane.showConfirmDialog(null, "This will delete build .jar files, causing them " + - "to be rebuilt on the next run. Do you want to continue?", "Delete build .jar files", + int answer = JOptionPane.showConfirmDialog(null, "This will delete built .jar files, causing them " + + "to be rebuilt on the next run. Do you want to continue?", "Delete built .jar files", JOptionPane.YES_NO_OPTION ); if (answer == JOptionPane.YES_NO_OPTION) { String jarDirectory = controller.getJarDirectory(); - File spigotFile = new File(jarDirectory + "SpigotLatest"); - File bukkitFile = new File(jarDirectory + "BukkitLatest"); + File spigotFile = new File(jarDirectory + "SpigotLatest.jar"); + File bukkitFile = new File(jarDirectory + "BukkitLatest.jar"); boolean success = true; if (spigotFile.exists() && !spigotFile.delete()) { serverLauncherGUI.showError("Unable to delete latest spigot .jar"); @@ -228,7 +228,7 @@ public class ServerLauncherMenu implements ActionListener { success = false; } if (success) { - serverLauncherGUI.showMessage("Deletion successful", "Deleted build .jar files"); + serverLauncherGUI.showMessage("Deletion successful", "Deleted built .jar files"); } } } diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java index 3cfb2e3..aefc993 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/CommonFunctions.java @@ -12,6 +12,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; +import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -44,7 +45,6 @@ public final class CommonFunctions { public static void createAllFolders() throws FileNotFoundException { createFolder(new File(filesDirectory)); createFolder(new File(filesDirectory + File.separator + "Jars")); - createFolder(new File(filesDirectory + File.separator + "testjars")); } /** @@ -53,7 +53,7 @@ public final class CommonFunctions { * @param folder

The folder to create

* @throws FileNotFoundException

If unable to create the folder

*/ - private static void createFolder(File folder) throws FileNotFoundException { + protected static void createFolder(File folder) throws FileNotFoundException { if (!folder.exists()) { if (!folder.mkdirs()) { throw new FileNotFoundException("Cannot create necessary directory."); @@ -128,6 +128,16 @@ public final class CommonFunctions { return new BufferedReader(new InputStreamReader(new FileInputStream(path))); } + /** + * Gets a buffered writer for writing to a given file + * @param path

The path to the file to write to

+ * @return

A buffered writer for writing to the file

+ * @throws FileNotFoundException

If the file does not exist

+ */ + public static BufferedWriter getFileWriter(String path) throws FileNotFoundException { + return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path))); + } + /** * Reads a file from disk * @@ -139,6 +149,14 @@ public final class CommonFunctions { return CommonFunctions.readBufferedReader(getFileReader(path)); } + + /** + * Writes text to a file and adds a trailing newline + * + * @param path

The path of the file to write to

+ * @param text

The text to write

+ * @throws IOException

If unable to write to the file

+ */ public static void writeFile(String path, String text) throws IOException { writeFile(path, text, true); } @@ -152,7 +170,7 @@ public final class CommonFunctions { * @throws IOException

If unable to write to the file

*/ public static void writeFile(String path, String text, Boolean addTrailingNewline) throws IOException { - BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path))); + BufferedWriter writer = getFileWriter(path); writer.write(text); if (addTrailingNewline) { writer.newLine(); @@ -160,6 +178,20 @@ public final class CommonFunctions { writer.close(); } + /** + * Appends text to a file + * + * @param path

The path to the file to append to

+ * @param text

The text to append

+ * @throws IOException

If unable to append to the file

+ */ + public static void appendFile(String path, String text) throws IOException { + BufferedWriter writer = new BufferedWriter(new FileWriter(path, true)); + writer.write(text); + writer.newLine(); + writer.close(); + } + /** * Downloads a file from a website and replaces the target file * diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilder.java b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilder.java index a1b7e7f..34ca83b 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilder.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilder.java @@ -4,10 +4,12 @@ import net.knarcraft.minecraftserverlauncher.Main; import net.knarcraft.minecraftserverlauncher.profile.ServerLauncherController; import net.knarcraft.minecraftserverlauncher.server.ServerVersionContainer; import net.knarcraft.minecraftserverlauncher.userinterface.GUI; +import net.knarcraft.minecraftserverlauncher.userinterface.ServerLauncherGUI; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; @@ -37,6 +39,11 @@ public class JarBuilder { this.gui = gui; this.versionContainer = ServerVersionContainer.getInstance(); this.javaCommand = ServerLauncherController.getInstance().getJavaCommand(); + try { + CommonFunctions.createFolder(new File(buildDirectory)); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } } /** @@ -75,6 +82,7 @@ public class JarBuilder { * Downloads the latest BuildTools version */ public void downloadBuildTools() { + ServerLauncherGUI gui = Main.getController().getGUI(); try { String latestVersion = getLatestBuildToolsVersion(); boolean exists = new File(buildDirectory + "BuildTools.jar").exists(); @@ -84,13 +92,16 @@ public class JarBuilder { String buildToolsURL = "https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar"; boolean success = CommonFunctions.downloadFile(buildToolsURL, Paths.get(buildDirectory + "BuildTools.jar")); if (!success) { - Main.getController().getGUI().setStatus("Unable to download the latest BuildTools"); + gui.setStatus("Unable to download the latest BuildTools"); + gui.logError("Target: " + buildDirectory + "BuildTools.jar"); + gui.logError("URL: " + buildToolsURL); } else { versionContainer.setDownloadedBuildToolsVersion(latestVersion); } } } catch (IOException e) { - Main.getController().getGUI().setStatus("Unable to download the latest BuildTools"); + gui.setStatus("Unable to download the latest BuildTools"); + gui.logMessage(e.getMessage()); } } diff --git a/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarDownloaderTest.java b/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarDownloaderTest.java index 258d048..fc9c02b 100644 --- a/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarDownloaderTest.java +++ b/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarDownloaderTest.java @@ -10,6 +10,7 @@ import org.junit.jupiter.api.Test; import javax.naming.ConfigurationException; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -21,8 +22,13 @@ public class JarDownloaderTest { @BeforeAll public static void setUp() { - targetDirectory = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator + - "testjars" + File.separator; + String filesDirectory = Main.getApplicationWorkDirectory() + File.separator + "files"; + try { + CommonFunctions.createFolder(new File(filesDirectory + File.separator + "testjars")); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + targetDirectory = filesDirectory + File.separator + "testjars" + File.separator; removeDownloadedFiles(); }