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 { try {
downloadAssetsIndex(); downloadAssetsIndex();
} catch (IOException e) { } catch (IOException e) {
if (Log.getLevel().ordinal() >= LogLevels.DEBUG.ordinal()) { Log.printException(e, LogLevels.DEBUG);
e.printStackTrace();
}
Log.warn("Could not download assets index. Please check your internet connection"); Log.warn("Could not download assets index. Please check your internet connection");
} }
assets.putAll(parseAssetsIndex(ASSETS_INDEX_HASH)); assets.putAll(parseAssetsIndex(ASSETS_INDEX_HASH));

View File

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

View File

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

View File

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