Add Log::printException with minimum log level required

This commit is contained in:
Bixilon 2020-10-30 14:30:30 +01:00
parent a0c39e835d
commit ddd33e4132
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
5 changed files with 19 additions and 18 deletions

View File

@ -78,9 +78,7 @@ public class AssetsManager {
try {
downloadAssetsIndex();
} catch (IOException e) {
if (Log.getLevel().ordinal() >= LogLevels.DEBUG.ordinal()) {
e.printStackTrace();
}
Log.printException(e, LogLevels.DEBUG);
Log.warn("Could not download assets index. Please check your internet connection");
}
assets.putAll(parseAssetsIndex(ASSETS_INDEX_HASH));

View File

@ -161,4 +161,17 @@ public class Log {
public static void info(String message) {
log(LogLevels.INFO, message, ChatColors.getColorByName("white"));
}
public static boolean printException(Exception exception, LogLevels minimumLogLevel) {
// ToDo: log to file, print also exceptions that are not printed with this method
if (Log.getLevel().ordinal() >= minimumLogLevel.ordinal()) {
exception.printStackTrace();
return true;
}
return false;
}
public static boolean printException(Exception exception) {
return printException(exception, LogLevels.FATAL); // always print
}
}

View File

@ -158,9 +158,7 @@ public class Connection {
try {
Versions.loadVersionMappings(version.getVersionId());
} catch (IOException e) {
if (Log.getLevel().ordinal() >= LogLevels.DEBUG.ordinal()) {
e.printStackTrace();
}
Log.printException(e, LogLevels.DEBUG);
Log.fatal(String.format("Could not load mapping for %s. This version seems to be unsupported!", version));
lastException = new RuntimeException(String.format("Mappings could not be loaded: %s", e.getLocalizedMessage()));
setConnectionState(ConnectionStates.FAILED_NO_RETRY);
@ -223,9 +221,7 @@ public class Connection {
}
packet.handle(getHandler());
} catch (Exception e) {
if (Log.getLevel().ordinal() >= LogLevels.PROTOCOL.ordinal()) {
e.printStackTrace();
}
Log.printException(e, LogLevels.PROTOCOL);
}
}
}, String.format("%d/Handling", connectionId));

View File

@ -233,18 +233,14 @@ public class SocketNetwork implements Network {
e.printStackTrace();
}
} catch (Exception e) {
Log.printException(e, LogLevels.DEBUG);
Log.protocol(String.format("An error occurred while parsing an packet (%s): %s", packet, e));
if (Log.getLevel().ordinal() >= LogLevels.DEBUG.ordinal()) {
e.printStackTrace();
}
}
}
disconnect();
} catch (IOException e) {
// Could not connect
if (Log.getLevel().ordinal() >= LogLevels.DEBUG.ordinal()) {
e.printStackTrace();
}
Log.printException(e, LogLevels.DEBUG);
if (socketSThread != null) {
socketSThread.interrupt();
}

View File

@ -33,9 +33,7 @@ public final class HTTP {
try {
return client.send(request, HttpResponse.BodyHandlers.ofString());
} catch (IOException | InterruptedException e) {
if (Log.getLevel().ordinal() >= LogLevels.DEBUG.ordinal()) {
e.printStackTrace();
}
Log.printException(e, LogLevels.DEBUG);
}
return null;
}