[FEATURE] DebugCommand

This commit is contained in:
Gravita 2022-05-31 17:07:37 +07:00
parent 2c1129aa5b
commit 44ba945b58
4 changed files with 115 additions and 16 deletions

View file

@ -0,0 +1,43 @@
package pro.gravit.launchserver.command.basic;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.command.Command;
public class DebugCommand extends Command {
private transient Logger logger = LogManager.getLogger();
public DebugCommand(LaunchServer server) {
super(server);
}
@Override
public String getArgsDescription() {
return "[true/false]";
}
@Override
public String getUsageDescription() {
return "Enable log level TRACE in LaunchServer";
}
@Override
public void invoke(String... args) throws Exception {
verifyArgs(args, 1);
boolean value = Boolean.parseBoolean(args[0]);
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig("pro.gravit");
loggerConfig.setLevel(value ? Level.TRACE : Level.DEBUG);
ctx.updateLoggers();
if(value) {
logger.info("Log level TRACE enabled");
} else {
logger.info("Log level TRACE disabled");
}
}
}

View file

@ -1,17 +1,13 @@
package pro.gravit.launchserver.command.handler;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.command.basic.BuildCommand;
import pro.gravit.launchserver.command.basic.RestartCommand;
import pro.gravit.launchserver.command.basic.StopCommand;
import pro.gravit.launchserver.command.basic.VersionCommand;
import pro.gravit.launchserver.command.basic.*;
import pro.gravit.launchserver.command.hash.*;
import pro.gravit.launchserver.command.modules.LoadModuleCommand;
import pro.gravit.launchserver.command.modules.ModulesCommand;
import pro.gravit.launchserver.command.service.*;
import pro.gravit.utils.command.BaseCommandCategory;
import pro.gravit.utils.command.basic.ClearCommand;
import pro.gravit.utils.command.basic.DebugCommand;
import pro.gravit.utils.command.basic.GCCommand;
import pro.gravit.utils.command.basic.HelpCommand;
@ -25,7 +21,7 @@ public static void registerCommands(pro.gravit.utils.command.CommandHandler hand
basic.registerCommand("build", new BuildCommand(server));
basic.registerCommand("stop", new StopCommand(server));
basic.registerCommand("restart", new RestartCommand(server));
basic.registerCommand("debug", new DebugCommand());
basic.registerCommand("debug", new DebugCommand(server));
basic.registerCommand("clear", new ClearCommand(handler));
basic.registerCommand("gc", new GCCommand());
basic.registerCommand("loadModule", new LoadModuleCommand(server));

View file

@ -173,31 +173,71 @@ public void registerClient(Channel channel) {
channels.add(channel);
}
public static String getIPFromContext(ChannelHandlerContext ctx) {
var handler = ctx.pipeline().get(WebSocketFrameHandler.class);
if(handler == null || handler.context == null || handler.context.ip == null) {
return IOHelper.getIP(ctx.channel().remoteAddress());
}
return handler.context.ip;
}
public static String getIPFromChannel(Channel channel) {
var handler = channel.pipeline().get(WebSocketFrameHandler.class);
if(handler == null || handler.context == null || handler.context.ip == null) {
return IOHelper.getIP(channel.remoteAddress());
}
return handler.context.ip;
}
public void sendObject(ChannelHandlerContext ctx, Object obj) {
ctx.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, WebSocketEvent.class)), ctx.voidPromise());
String msg = gson.toJson(obj, WebSocketEvent.class);
if(logger.isTraceEnabled()) {
logger.trace("Send to {}: {}", getIPFromContext(ctx), msg);
}
ctx.writeAndFlush(new TextWebSocketFrame(msg), ctx.voidPromise());
}
public void sendObject(ChannelHandlerContext ctx, Object obj, Type type) {
ctx.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, type)), ctx.voidPromise());
String msg = gson.toJson(obj, type);
if(logger.isTraceEnabled()) {
logger.trace("Send to {}: {}", getIPFromContext(ctx), msg);
}
ctx.writeAndFlush(new TextWebSocketFrame(msg), ctx.voidPromise());
}
public void sendObject(Channel channel, Object obj) {
channel.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, WebSocketEvent.class)), channel.voidPromise());
String msg = gson.toJson(obj, WebSocketEvent.class);
if(logger.isTraceEnabled()) {
logger.trace("Send to channel {}: {}", getIPFromChannel(channel), msg);
}
channel.writeAndFlush(new TextWebSocketFrame(msg), channel.voidPromise());
}
public void sendObject(Channel channel, Object obj, Type type) {
channel.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, type)), channel.voidPromise());
String msg = gson.toJson(obj, type);
if(logger.isTraceEnabled()) {
logger.trace("Send to channel {}: {}", getIPFromChannel(channel), msg);
}
channel.writeAndFlush(new TextWebSocketFrame(msg), channel.voidPromise());
}
public void sendObjectAll(Object obj) {
String msg = gson.toJson(obj, WebSocketEvent.class);
if(logger.isTraceEnabled()) {
logger.trace("Send to all: {}", msg);
}
for (Channel ch : channels) {
ch.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, WebSocketEvent.class)), ch.voidPromise());
ch.writeAndFlush(new TextWebSocketFrame(msg), ch.voidPromise());
}
}
public void sendObjectAll(Object obj, Type type) {
String msg = gson.toJson(obj, type);
if(logger.isTraceEnabled()) {
logger.trace("Send to all: {}", msg);
}
for (Channel ch : channels) {
ch.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, type)), ch.voidPromise());
ch.writeAndFlush(new TextWebSocketFrame(msg), ch.voidPromise());
}
}
@ -208,7 +248,11 @@ public void sendObjectToUUID(UUID userUuid, Object obj, Type type) {
if (wsHandler == null) continue;
Client client = wsHandler.getClient();
if (client == null || !userUuid.equals(client.uuid)) continue;
ch.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, type)), ch.voidPromise());
String msg = gson.toJson(obj, type);
if(logger.isTraceEnabled()) {
logger.trace("Send to {}({}): {}", getIPFromChannel(ch), userUuid, msg);
}
ch.writeAndFlush(new TextWebSocketFrame(msg), ch.voidPromise());
}
}
@ -272,15 +316,28 @@ public boolean kickByIP(String ip, boolean isClose) {
}
public void sendObjectAndClose(ChannelHandlerContext ctx, Object obj) {
ctx.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, WebSocketEvent.class))).addListener(ChannelFutureListener.CLOSE);
String msg = gson.toJson(obj, WebSocketEvent.class);
if(logger.isTraceEnabled()) {
logger.trace("Send and close {}: {}", getIPFromContext(ctx), msg);
}
ctx.writeAndFlush(new TextWebSocketFrame(msg)).addListener(ChannelFutureListener.CLOSE);
}
public void sendObjectAndClose(ChannelHandlerContext ctx, Object obj, Type type) {
ctx.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, type))).addListener(ChannelFutureListener.CLOSE);
String msg = gson.toJson(obj, type);
if(logger.isTraceEnabled()) {
logger.trace("Send and close {}: {}", getIPFromContext(ctx), msg);
}
ctx.writeAndFlush(new TextWebSocketFrame(msg)).addListener(ChannelFutureListener.CLOSE);
}
@Deprecated
public void sendEvent(EventResult obj) {
channels.writeAndFlush(new TextWebSocketFrame(gson.toJson(obj)), ChannelMatchers.all(), true);
String msg = gson.toJson(obj, WebSocketEvent.class);
if(logger.isTraceEnabled()) {
logger.trace("Send event: {}", msg);
}
channels.writeAndFlush(new TextWebSocketFrame(msg), ChannelMatchers.all(), true);
}
public static class WebSocketRequestContext {

View file

@ -64,6 +64,9 @@ protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) {
logger.error("WebSocket frame handler hook error", ex);
}
if (frame instanceof TextWebSocketFrame) {
if(logger.isTraceEnabled()) {
logger.trace("Message from {}: {}", context.ip == null ? IOHelper.getIP(ctx.channel().remoteAddress()) : context.ip, ((TextWebSocketFrame) frame).text());
}
try {
service.process(ctx, (TextWebSocketFrame) frame, client, context.ip);
} catch (Throwable ex) {