option to color console messages

This commit is contained in:
Bixilon 2020-06-23 20:54:51 +02:00
parent 09d8307100
commit ff679a36d1
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
3 changed files with 31 additions and 14 deletions

View File

@ -17,4 +17,5 @@ public class Config {
public static String homeDir;
public static final String configFileName = "game.yml";
public static final boolean skipAuthentication = true; // only for offline development
public static final boolean colorLog = true;
}

View File

@ -13,22 +13,39 @@
package de.bixilon.minosoft.logging;
import de.bixilon.minosoft.Config;
import de.bixilon.minosoft.game.datatypes.TextComponent;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
public class Log {
static LogLevel level = LogLevel.PROTOCOL;
final static SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
final static List<String> queue = new ArrayList<>();
static LogLevel level = LogLevel.PROTOCOL;
static Thread logThread;
public static void log(LogLevel l, String message) {
public static void log(LogLevel l, String message, TextComponent.ChatAttributes color) {
if (l.getId() > level.getId()) {
// log level too low
return;
}
queue.add(String.format("[%s] [%s] %s", timeFormat.format(System.currentTimeMillis()), l.name(), message));
StringBuilder builder = new StringBuilder();
builder.append("[");
builder.append(timeFormat.format(System.currentTimeMillis()));
builder.append("] [");
builder.append(l.name());
builder.append("] ");
if (color != null && Config.colorLog) {
builder.append(color);
builder.append(message);
builder.append(TextComponent.ChatAttributes.RESET);
} else {
builder.append(message);
}
queue.add(builder.toString());
logThread.interrupt();
}
@ -61,7 +78,7 @@ public class Log {
* @param message Raw message to log
*/
public static void game(String message) {
log(LogLevel.GAME, message);
log(LogLevel.GAME, message, TextComponent.ChatAttributes.GREEN);
}
/**
@ -70,7 +87,7 @@ public class Log {
* @param message Raw message to log
*/
public static void fatal(String message) {
log(LogLevel.FATAL, message);
log(LogLevel.FATAL, message, TextComponent.ChatAttributes.DARK_RED);
}
/**
@ -79,7 +96,7 @@ public class Log {
* @param message Raw message to log
*/
public static void info(String message) {
log(LogLevel.INFO, message);
log(LogLevel.INFO, message, TextComponent.ChatAttributes.WHITE);
}
/**
@ -88,7 +105,7 @@ public class Log {
* @param message Raw message to log
*/
public static void warn(String message) {
log(LogLevel.WARNING, message);
log(LogLevel.WARNING, message, TextComponent.ChatAttributes.RED);
}
/**
@ -97,7 +114,7 @@ public class Log {
* @param message Raw message to log
*/
public static void debug(String message) {
log(LogLevel.DEBUG, message);
log(LogLevel.DEBUG, message, TextComponent.ChatAttributes.GRAY);
}
/**
@ -106,7 +123,7 @@ public class Log {
* @param message Raw message to log
*/
public static void verbose(String message) {
log(LogLevel.VERBOSE, message);
log(LogLevel.VERBOSE, message, TextComponent.ChatAttributes.YELLOW);
}
/**
@ -115,7 +132,7 @@ public class Log {
* @param message Raw message to log
*/
public static void protocol(String message) {
log(LogLevel.PROTOCOL, message);
log(LogLevel.PROTOCOL, message, TextComponent.ChatAttributes.BLUE);
}
/**
@ -124,7 +141,7 @@ public class Log {
* @param message Raw message to log
*/
public static void mojang(String message) {
log(LogLevel.MOJANG, message);
log(LogLevel.MOJANG, message, TextComponent.ChatAttributes.AQUA);
}
public static LogLevel getLevel() {

View File

@ -13,7 +13,6 @@
package de.bixilon.minosoft.protocol.network;
import de.bixilon.minosoft.game.datatypes.TextComponent;
import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.protocol.packets.ClientboundPacket;
import de.bixilon.minosoft.protocol.packets.ServerboundPacket;
@ -202,7 +201,7 @@ public class Network {
Class<? extends ClientboundPacket> clazz = Protocol.getPacketByPacket(p);
if (clazz == null) {
Log.warn(String.format("[IN] Unknown packet with command 0x%x (%s) and %d bytes of data", inPacketBuffer.getCommand(), ((p != null) ? p.name() : "UNKNOWN"), inPacketBuffer.getBytesLeft()));
Log.warn(String.format("[IN] Received unknown packet (id=0x%x, name=%s, length=%d, dataLength=%d, version=%s, state=%s)", inPacketBuffer.getCommand(), ((p != null) ? p.name() : "UNKNOWN"), inPacketBuffer.getLength(), inPacketBuffer.getBytesLeft(), connection.getVersion().name(), connection.getConnectionState().name()));
binQueueIn.remove(0);
continue;
}
@ -211,7 +210,7 @@ public class Network {
packet.read(inPacketBuffer, connection.getVersion());
if (inPacketBuffer.getBytesLeft() > 0 && p != Packets.Clientbound.PLAY_ENTITY_METADATA) { // entity meta data uses mostly all data, but this happens in the handling thread
// warn not all data used
Log.warn(String.format(TextComponent.ChatAttributes.RED + "[IN] Could not parse packet %s completely (used=%d, available=%d, total=%d)" + TextComponent.ChatAttributes.RESET, ((p != null) ? p.name() : "null"), inPacketBuffer.getPosition(), inPacketBuffer.getBytesLeft(), inPacketBuffer.getLength()));
Log.warn(String.format("[IN] Could not parse packet %s completely (used=%d, available=%d, total=%d)", ((p != null) ? p.name() : "null"), inPacketBuffer.getPosition(), inPacketBuffer.getBytesLeft(), inPacketBuffer.getLength()));
}
if (packet instanceof PacketLoginSuccess) {