From 20cb3de4546c414ad9cf85a9fd305b3929c85b8d Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 2 Jan 2022 12:04:54 -0500 Subject: [PATCH] Creating a global default exception handler and standardizing the legacy logging setup --- src/megameklab/com/MegaMekLab.java | 51 ++++++++++++++---------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/src/megameklab/com/MegaMekLab.java b/src/megameklab/com/MegaMekLab.java index bce504306..e43ea4e1e 100644 --- a/src/megameklab/com/MegaMekLab.java +++ b/src/megameklab/com/MegaMekLab.java @@ -1,5 +1,6 @@ /* - * MegaMekLab - Copyright (C) 2008 + * MegaMekLab + * Copyright (c) 2008-2022 - The MegaMek Team. All Rights Reserved. * * Original author - jtighe (torren@users.sourceforge.net) * @@ -28,7 +29,8 @@ import javax.swing.*; import java.awt.*; -import java.io.*; +import java.io.File; +import java.io.IOException; import java.text.NumberFormat; import java.time.LocalDate; import java.util.ArrayList; @@ -37,39 +39,35 @@ import java.util.Locale; public class MegaMekLab { - public static void main(String[] args) { + public static void main(String... args) { + // First, create a global default exception handler + Thread.setDefaultUncaughtExceptionHandler((thread, t) -> { + LogManager.getLogger().error("Uncaught Exception Detected", t); + final String name = t.getClass().getName(); + JOptionPane.showMessageDialog(null, + String.format("Uncaught %s detected. Please open up an issue containing all logs and the current unit file at https://github.com/MegaMek/megameklab/issues", name), + "Uncaught " + name, JOptionPane.ERROR_MESSAGE); + }); + + // Second, let's handle logging + MegaMek.showInfo(); + showInfo(); + MegaMek.handleLegacyLogging(); + + // Third, let's set some default properties System.setProperty("apple.laf.useScreenMenuBar", "true"); System.setProperty("com.apple.mrj.application.apple.menu.about.name","MegaMekLab"); - redirectOutput(); - // Register any fonts in the fonts directory + + // Fourth, register any fonts in the fonts directory GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); List fontList = new ArrayList<>(); collectFontsFromDir(Configuration.fontsDir(), fontList); for (Font font : fontList) { ge.registerFont(font); } - startup(); - } - private static void redirectOutput() { - try { - System.out.println("Redirecting output to megameklablog.txt"); - File logDir = new File("logs"); - if (!logDir.exists()) { - logDir.mkdir(); - } - final String logFilename = "logs" + File.separator + "megameklablog.txt"; - MegaMek.resetLogFile(logFilename); - PrintStream ps = new PrintStream( - new BufferedOutputStream( - new FileOutputStream(logFilename, - true), - 64)); - System.setOut(ps); - System.setErr(ps); - } catch (Exception e) { - LogManager.getLogger().error("Unable to redirect output to megameklablog.txt", e); - } + // Finally, let's handle startup + startup(); } /** @@ -119,7 +117,6 @@ public static void showInfo() { } private static void startup() { - showInfo(); Locale.setDefault(Locale.US); EquipmentType.initializeTypes(); MechSummaryCache.getInstance();