Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging: Default Global Exception Handler and Standardized Legacy Logging #993

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 24 additions & 27 deletions src/megameklab/com/MegaMekLab.java
Original file line number Diff line number Diff line change
@@ -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)
*
Expand Down Expand Up @@ -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;
Expand All @@ -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<Font> 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();
}

/**
Expand Down Expand Up @@ -119,7 +117,6 @@ public static void showInfo() {
}

private static void startup() {
showInfo();
Locale.setDefault(Locale.US);
EquipmentType.initializeTypes();
MechSummaryCache.getInstance();
Expand Down