Skip to content

Commit 84a6f73

Browse files
Implement /logging command and config to enable/disable debug logging
You can now enable debug logging by running `/playit logging enable` and disable debug logging by using `/playit logging disable`. The plugin now uses SLF4J for logging. Fixes #21
1 parent a7c5908 commit 84a6f73

File tree

10 files changed

+139
-59
lines changed

10 files changed

+139
-59
lines changed

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@
4545
<version>4.1.82.Final</version>
4646
<scope>provided</scope>
4747
</dependency>
48+
<dependency>
49+
<groupId>org.slf4j</groupId>
50+
<artifactId>slf4j-api</artifactId>
51+
<version>2.0.7</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.slf4j</groupId>
55+
<artifactId>slf4j-simple</artifactId>
56+
<version>2.0.7</version>
57+
</dependency>
4858
<dependency>
4959
<groupId>org.spigotmc</groupId>
5060
<artifactId>spigot-api</artifactId>

src/main/java/gg/playit/control/ChannelSetup.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66
import gg.playit.messages.ControlRequestWriter;
77
import gg.playit.messages.DecodeException;
88
import gg.playit.minecraft.utils.DecoderException;
9+
import gg.playit.minecraft.utils.Logger;
910

1011
import java.io.IOException;
1112
import java.net.*;
1213
import java.nio.BufferUnderflowException;
1314
import java.nio.ByteBuffer;
1415
import java.util.Arrays;
1516
import java.util.Comparator;
16-
import java.util.logging.Logger;
1717

1818
public class ChannelSetup {
1919
public static final int CONTROL_PORT = 5525;
2020

21-
static Logger log = Logger.getLogger(ChannelSetup.class.getName());
21+
static Logger log = new Logger(ChannelSetup.class.getName());
2222

2323
public static FindSuitableChannel start() throws UnknownHostException {
2424
InetAddress[] allByName = InetAddress.getAllByName("control.playit.gg");
@@ -155,7 +155,7 @@ public PlayitControlChannel authenticate(String secretKey) throws IOException {
155155

156156
if (response instanceof ControlFeedReader.Error error) {
157157
if (error == ControlFeedReader.Error.RequestQueued) {
158-
log.info("request queued, waiting 1 second before resend");
158+
log.debug("request queued, waiting 1 second before resend");
159159

160160
try {
161161
Thread.sleep(1000);

src/main/java/gg/playit/control/PlayitControlChannel.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import gg.playit.messages.ControlFeedReader;
55
import gg.playit.messages.ControlRequestWriter;
66
import gg.playit.messages.DecodeException;
7+
import gg.playit.minecraft.utils.Logger;
78

89
import java.io.Closeable;
910
import java.io.IOException;
@@ -16,12 +17,11 @@
1617
import java.time.Instant;
1718
import java.util.Arrays;
1819
import java.util.Optional;
19-
import java.util.logging.Logger;
2020

2121
import static gg.playit.control.ChannelSetup.CONTROL_PORT;
2222

2323
public class PlayitControlChannel implements Closeable {
24-
static Logger log = Logger.getLogger(ChannelSetup.class.getName());
24+
static Logger log = new Logger(ChannelSetup.class.getName());
2525

2626
ApiClient apiClient;
2727
DatagramSocket socket;
@@ -57,7 +57,7 @@ public Optional<ControlFeedReader.ControlFeed> update() throws IOException {
5757

5858
var tillExpire = this.registered.expiresAt - now;
5959
if (tillExpire < 60_000 && 10_000 < now - lastKeepAlive) {
60-
log.info("send keep alive");
60+
log.debug("send keep alive");
6161
lastKeepAlive = now;
6262

6363
this.sendKeepAlive();

src/main/java/gg/playit/minecraft/PlayitBukkit.java

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import gg.playit.api.ApiClient;
44
import gg.playit.api.ApiError;
55
import gg.playit.api.models.Notice;
6+
import gg.playit.minecraft.utils.Logger;
67
import io.netty.channel.EventLoopGroup;
78
import io.netty.channel.nio.NioEventLoopGroup;
89
import org.bukkit.Bukkit;
@@ -17,13 +18,14 @@
1718

1819
import java.io.IOException;
1920
import java.util.List;
20-
import java.util.logging.Logger;
2121

2222
public final class PlayitBukkit extends JavaPlugin implements Listener {
2323
public static final String CFG_AGENT_SECRET_KEY = "agent-secret";
2424
public static final String CFG_CONNECTION_TIMEOUT_SECONDS = "mc-timeout-sec";
25+
public static final String CFG_DEBUG_LOGGING = "debug-logging";
26+
public static boolean debugLoggingEnabled = false;
2527

26-
static Logger log = Logger.getLogger(PlayitBukkit.class.getName());
28+
static Logger log = new Logger(PlayitBukkit.class.getName());
2729
final EventLoopGroup eventGroup = new NioEventLoopGroup();
2830

2931
private final Object managerSync = new Object();
@@ -40,15 +42,23 @@ public void onEnable() {
4042
command.setExecutor(this);
4143
command.setTabCompleter(this);
4244
} else {
43-
log.severe("failed to setup command /playit");
45+
log.error("failed to setup command /playit");
4446
}
4547

4648
getConfig().addDefault("agent-secret", "");
49+
getConfig().addDefault("debug-logging", "false");
4750
saveDefaultConfig();
4851

4952
var secretKey = getConfig().getString("agent-secret");
5053
resetConnection(secretKey);
5154

55+
var debugLogging = getConfig().getString(CFG_DEBUG_LOGGING);
56+
if ("true".equals(debugLogging)) {
57+
debugLoggingEnabled = true;
58+
} else if ("false".equals(debugLogging)) {
59+
debugLoggingEnabled = false;
60+
}
61+
5262
try {
5363
PluginManager pm = Bukkit.getServer().getPluginManager();
5464
pm.registerEvents(this, this);
@@ -239,14 +249,32 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
239249
log.warning("failed to create guest secret: " + e);
240250
sender.sendMessage("error: " + e.getMessage());
241251
} catch (IOException e) {
242-
log.severe("failed to create guest secret: " + e);
252+
log.error("failed to create guest secret: " + e);
243253
}
244254
}).start();
245255

246256
return true;
247257
}
248258
}
249259

260+
if (args.length > 0 && args[0].equals("logging")) {
261+
if (args.length > 1 && args[1].equals("enable")) {
262+
getConfig().set(CFG_DEBUG_LOGGING, true);
263+
debugLoggingEnabled = true;
264+
265+
sender.sendMessage("enabled debug logging");
266+
return true;
267+
}
268+
269+
if (args.length > 1 && args[1].equals("disable")) {
270+
getConfig().set(CFG_DEBUG_LOGGING, false);
271+
debugLoggingEnabled = false;
272+
273+
sender.sendMessage("disabled debug logging");
274+
return true;
275+
}
276+
}
277+
250278
return false;
251279
}
252280

@@ -286,7 +314,7 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
286314
}
287315

288316
if (argCount == 0) {
289-
return List.of("agent", "tunnel", "prop", "account");
317+
return List.of("agent", "tunnel", "prop", "account", "logging");
290318
}
291319

292320
if (args[0].equals("account")) {
@@ -321,6 +349,12 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
321349
}
322350
}
323351

352+
if (args[0].equals("logging")) {
353+
if (argCount == 1) {
354+
return List.of("enable", "disable");
355+
}
356+
}
357+
324358
return null;
325359
}
326360

src/main/java/gg/playit/minecraft/PlayitKeysSetup.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
import gg.playit.api.models.PortType;
99
import gg.playit.api.models.TunnelType;
1010
import gg.playit.minecraft.utils.Hex;
11+
import gg.playit.minecraft.utils.Logger;
1112

1213
import java.io.IOException;
1314
import java.net.InetAddress;
1415
import java.util.Random;
1516
import java.util.concurrent.atomic.AtomicInteger;
16-
import java.util.logging.Logger;
1717

1818
public class PlayitKeysSetup {
19-
private static final Logger log = Logger.getLogger(PlayitKeysSetup.class.getName());
19+
private static final Logger log = new Logger(PlayitKeysSetup.class.getName());
2020
public final AtomicInteger state;
2121
public static final int STATE_INIT = 1;
2222
public static final int STATE_MISSING_SECRET = 2;
@@ -55,18 +55,18 @@ public PlayitKeys progress() throws IOException {
5555
}
5656

5757
state.compareAndSet(STATE_INIT, STATE_CHECKING_SECRET);
58-
log.info("secret key found, checking");
58+
log.debug("secret key found, checking");
5959
return null;
6060
}
6161
case STATE_MISSING_SECRET -> {
6262
if (claimCode == null) {
6363
byte[] array = new byte[8];
6464
new Random().nextBytes(array);
6565
claimCode = Hex.encodeHexString(array);
66-
log.info("secret key not set, generate claim code: " + claimCode);
66+
log.debug("secret key not set, generate claim code: " + claimCode);
6767
}
6868

69-
log.info("trying to exchange claim code for secret");
69+
log.debug("trying to exchange claim code for secret");
7070
keys.secretKey = openClient.exchangeClaimForSecret(claimCode);
7171

7272
if (keys.secretKey == null) {
@@ -78,7 +78,7 @@ public PlayitKeys progress() throws IOException {
7878
return null;
7979
}
8080
case STATE_CHECKING_SECRET -> {
81-
log.info("check secret");
81+
log.debug("check secret");
8282

8383
var api = new ApiClient(keys.secretKey);
8484
try {
@@ -94,11 +94,11 @@ public PlayitKeys progress() throws IOException {
9494
} catch (ApiError e) {
9595
if (e.statusCode == 401 || e.statusCode == 400) {
9696
if (claimCode == null) {
97-
log.info("secret key invalid, starting over");
97+
log.debug("secret key invalid, starting over");
9898
state.compareAndSet(STATE_CHECKING_SECRET, STATE_MISSING_SECRET);
9999
} else {
100100
state.compareAndSet(STATE_CHECKING_SECRET, STATE_ERROR);
101-
log.info("secret failed verification after creating, moving to error state");
101+
log.debug("secret failed verification after creating, moving to error state");
102102
}
103103

104104
return null;
@@ -116,12 +116,12 @@ public PlayitKeys progress() throws IOException {
116116
for (AccountTunnel tunnel : tunnels.tunnels) {
117117
if (tunnel.tunnelType == TunnelType.MinecraftJava) {
118118
keys.tunnelAddress = tunnel.displayAddress;
119-
log.info("found minecraft java tunnel: " + keys.tunnelAddress);
119+
log.debug("found minecraft java tunnel: " + keys.tunnelAddress);
120120
return keys;
121121
}
122122
}
123123

124-
log.info("create new minecraft java tunnel");
124+
log.debug("create new minecraft java tunnel");
125125

126126
var create = new CreateTunnel();
127127
create.localIp = "127.0.0.1";

src/main/java/gg/playit/minecraft/PlayitManager.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
import gg.playit.api.models.Notice;
55
import gg.playit.control.PlayitControlChannel;
66
import gg.playit.messages.ControlFeedReader;
7+
import gg.playit.minecraft.utils.Logger;
78
import org.bukkit.Bukkit;
89
import org.bukkit.ChatColor;
910

1011
import java.io.IOException;
1112
import java.net.InetAddress;
1213
import java.net.InetSocketAddress;
1314
import java.util.concurrent.atomic.AtomicInteger;
14-
import java.util.logging.Logger;
1515

1616
public class PlayitManager implements Runnable {
17-
static Logger log = Logger.getLogger(PlayitManager.class.getName());
17+
static Logger log = new Logger(PlayitManager.class.getName());
1818
private final AtomicInteger state = new AtomicInteger(STATE_INIT);
1919
private final PlayitConnectionTracker tracker = new PlayitConnectionTracker();
2020

@@ -86,11 +86,11 @@ public void run() {
8686
keys = setup.progress();
8787

8888
if (keys != null) {
89-
log.info("keys and tunnel setup");
89+
log.debug("keys and tunnel setup");
9090
break;
9191
}
9292
} catch (IOException e) {
93-
log.severe("got error during setup: " + e);
93+
log.error("got error during setup: " + e);
9494

9595
try {
9696
Thread.sleep(3000);
@@ -120,7 +120,7 @@ public void run() {
120120
}
121121

122122
if (keys == null) {
123-
log.info("shutdown reached, tunnel connection never started");
123+
log.debug("shutdown reached, tunnel connection never started");
124124
return;
125125
}
126126

@@ -136,7 +136,7 @@ public void run() {
136136
try {
137137
var key = api.createGuestWebSessionKey();
138138
var url = "https://playit.gg/login/guest-account/" + key;
139-
log.info("setup playit.gg account: " + url);
139+
log.debug("setup playit.gg account: " + url);
140140

141141
if (state.get() == STATE_SHUTDOWN) {
142142
return;
@@ -149,7 +149,7 @@ public void run() {
149149
}
150150
}
151151
} catch (IOException e) {
152-
log.severe("failed to generate web session key: " + e);
152+
log.error("failed to generate web session key: " + e);
153153
}
154154
} else if (!keys.isEmailVerified) {
155155
plugin.broadcast(ChatColor.RED + "WARNING: " + ChatColor.RESET + "email associated with playit.gg account is not verified");
@@ -174,11 +174,11 @@ public void run() {
174174
var feedMessage = messageOpt.get();
175175

176176
if (feedMessage instanceof ControlFeedReader.NewClient newClient) {
177-
log.info("got new client: " + feedMessage);
177+
log.debug("got new client: " + feedMessage);
178178

179179
var key = newClient.peerAddr + "-" + newClient.connectAddr;
180180
if (tracker.addConnection(key)) {
181-
log.info("starting tcp tunnel for client");
181+
log.debug("starting tcp tunnel for client");
182182

183183
new PlayitTcpTunnel(
184184
new InetSocketAddress(InetAddress.getByAddress(newClient.peerAddr.ipBytes), Short.toUnsignedInt(newClient.peerAddr.portNumber)),
@@ -197,7 +197,7 @@ public void run() {
197197
}
198198
} catch (IOException e) {
199199
state.compareAndSet(STATE_ONLINE, STATE_ERROR_WAITING);
200-
log.severe("failed when communicating with tunnel server, error: " + e);
200+
log.error("failed when communicating with tunnel server, error: " + e);
201201

202202
if (e.getMessage().contains("invalid authentication")) {
203203
state.set(STATE_INVALID_AUTH);
@@ -209,17 +209,17 @@ public void run() {
209209
}
210210
} finally {
211211
if (state.compareAndSet(STATE_SHUTDOWN, STATE_OFFLINE)) {
212-
log.info("control channel shutdown");
212+
log.debug("control channel shutdown");
213213
} else if (state.compareAndSet(STATE_ERROR_WAITING, STATE_CONNECTING)) {
214-
log.info("trying to connect again");
214+
log.debug("trying to connect again");
215215
} else if (state.compareAndSet(STATE_ONLINE, STATE_CONNECTING)) {
216216
log.warning("unexpected state ONLINE, moving to CONNECTING");
217217
}
218218
if (state.get() == STATE_CONNECTING) {
219-
log.info("failed to connect, retrying");
219+
log.debug("failed to connect, retrying");
220220
}
221221
if (state.get() == STATE_INVALID_AUTH) {
222-
log.info("invalid auth, done trying");
222+
log.debug("invalid auth, done trying");
223223
}
224224
}
225225
}

0 commit comments

Comments
 (0)