Skip to content

Commit 150fdbf

Browse files
committed
[Revert] Go back to SLF4J
- I truly don't know what past me was on going away from this, chief.
1 parent 7afd808 commit 150fdbf

File tree

5 files changed

+39
-33
lines changed

5 files changed

+39
-33
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
<artifactId>gson</artifactId>
1515
<version>2.8.0</version>
1616
</dependency>
17+
<dependency>
18+
<groupId>org.slf4j</groupId>
19+
<artifactId>slf4j-api</artifactId>
20+
<version>1.7.36</version>
21+
</dependency>
1722
<dependency>
1823
<groupId>com.kohlschutter.junixsocket</groupId>
1924
<artifactId>junixsocket-common</artifactId>

src/main/java/com/jagrosh/discordipc/IPCClient.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323
import com.jagrosh.discordipc.entities.pipe.Pipe;
2424
import com.jagrosh.discordipc.entities.pipe.PipeStatus;
2525
import com.jagrosh.discordipc.exceptions.NoDiscordClientException;
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
2628

2729
import java.io.Closeable;
2830
import java.io.IOException;
2931
import java.lang.management.ManagementFactory;
3032
import java.util.HashMap;
31-
import java.util.logging.Logger;
3233

3334
/**
3435
* Represents a Discord IPC Client that can send and receive
@@ -54,7 +55,7 @@
5455
* @author John Grosh ([email protected])
5556
*/
5657
public final class IPCClient implements Closeable {
57-
private final Logger LOGGER = Logger.getLogger("IPC-Client");
58+
private static final Logger LOGGER = LoggerFactory.getLogger(IPCClient.class);
5859
private final long clientId;
5960
private final boolean debugMode, verboseLogging, autoRegister;
6061
private final HashMap<String, Callback> callbacks = new HashMap<>();
@@ -202,15 +203,6 @@ private static int getPID() {
202203
return Integer.parseInt(pr.substring(0, pr.indexOf('@')));
203204
}
204205

205-
/**
206-
* Retrieves this IPCClient's {@link Logger} to handle received events.
207-
*
208-
* @return the current {@link Logger} instance
209-
*/
210-
public Logger getLogger() {
211-
return this.LOGGER;
212-
}
213-
214206
/**
215207
* Sets this IPCClient's {@link IPCListener} to handle received events.
216208
* <p>
@@ -341,7 +333,7 @@ public void connect(DiscordBuild... preferredOrder) throws NoDiscordClientExcept
341333
if (debugMode) {
342334
ex.printStackTrace();
343335
} else {
344-
LOGGER.severe("Unable to register application, enable debug mode for trace...");
336+
LOGGER.error("Unable to register application, enable debug mode for trace...");
345337
}
346338
}
347339
}
@@ -698,7 +690,7 @@ private void readPipe(final IPCClient instance) {
698690
break;
699691
}
700692
} catch (Exception e) {
701-
LOGGER.severe(String.format("Exception when handling event: %s", e));
693+
LOGGER.error(String.format("Exception when handling event: %s", e));
702694
}
703695
}
704696
}
@@ -707,7 +699,7 @@ private void readPipe(final IPCClient instance) {
707699
if (listener != null)
708700
listener.onClose(instance, p.getJson());
709701
} catch (IOException | JsonParseException ex) {
710-
LOGGER.severe(String.format("Reading thread encountered an Exception: %s", ex));
702+
LOGGER.error(String.format("Reading thread encountered an Exception: %s", ex));
711703

712704
pipe.setStatus(PipeStatus.DISCONNECTED);
713705
if (listener != null)

src/main/java/com/jagrosh/discordipc/entities/pipe/Pipe.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@
2626
import com.jagrosh.discordipc.entities.Packet;
2727
import com.jagrosh.discordipc.entities.User;
2828
import com.jagrosh.discordipc.exceptions.NoDiscordClientException;
29+
import org.slf4j.Logger;
30+
import org.slf4j.LoggerFactory;
2931

3032
import java.io.File;
3133
import java.io.IOException;
3234
import java.util.HashMap;
3335
import java.util.UUID;
3436

3537
public abstract class Pipe {
38+
private static final Logger LOGGER = LoggerFactory.getLogger(Pipe.class);
3639
private static final int VERSION = 1;
3740
// a list of system property keys to get IPC file from different unix systems.
3841
private final static String[] unixPaths = {"XDG_RUNTIME_DIR", "TMPDIR", "TMP", "TEMP"};
@@ -62,14 +65,14 @@ public static Pipe openPipe(IPCClient ipcClient, long clientId, HashMap<String,
6265
for (int i = 0; i < 10; i++) {
6366
String location = getPipeLocation(i);
6467
if (ipcClient.isDebugMode()) {
65-
ipcClient.getLogger().info(String.format("[DEBUG] Searching for IPC Pipe: \"%s\"", location));
68+
LOGGER.info(String.format("[DEBUG] Searching for IPC Pipe: \"%s\"", location));
6669
}
6770

6871
try {
6972
File fileLocation = new File(location);
7073
if (fileLocation.exists()) {
7174
if (ipcClient.isDebugMode()) {
72-
ipcClient.getLogger().info(String.format("[DEBUG] Found valid file, attempting connection to IPC: \"%s\"", location));
75+
LOGGER.info(String.format("[DEBUG] Found valid file, attempting connection to IPC: \"%s\"", location));
7376
}
7477
pipe = createPipe(ipcClient, callbacks, fileLocation);
7578

@@ -99,14 +102,14 @@ public static Pipe openPipe(IPCClient ipcClient, long clientId, HashMap<String,
99102
);
100103

101104
if (ipcClient.isDebugMode()) {
102-
ipcClient.getLogger().info(String.format("[DEBUG] Found a valid client (%s) with packet: %s", pipe.build.name(), p));
103-
ipcClient.getLogger().info(String.format("[DEBUG] Found a valid user (%s) with id: %s", pipe.currentUser.getName(), pipe.currentUser.getId()));
105+
LOGGER.info(String.format("[DEBUG] Found a valid client (%s) with packet: %s", pipe.build.name(), p));
106+
LOGGER.info(String.format("[DEBUG] Found a valid user (%s) with id: %s", pipe.currentUser.getName(), pipe.currentUser.getId()));
104107
}
105108

106109
// we're done if we found our first choice
107110
if (pipe.build == preferredOrder[0] || DiscordBuild.ANY == preferredOrder[0]) {
108111
if (ipcClient.isDebugMode()) {
109-
ipcClient.getLogger().info(String.format("[DEBUG] Found preferred client: %s", pipe.build.name()));
112+
LOGGER.info(String.format("[DEBUG] Found preferred client: %s", pipe.build.name()));
110113
}
111114
break;
112115
}
@@ -119,7 +122,7 @@ public static Pipe openPipe(IPCClient ipcClient, long clientId, HashMap<String,
119122
}
120123
} else {
121124
if (ipcClient.isDebugMode()) {
122-
ipcClient.getLogger().info(String.format("[DEBUG] Unable to locate IPC Pipe: \"%s\"", location));
125+
LOGGER.info(String.format("[DEBUG] Unable to locate IPC Pipe: \"%s\"", location));
123126
}
124127
}
125128
} catch (IOException | JsonParseException ex) {
@@ -133,7 +136,7 @@ public static Pipe openPipe(IPCClient ipcClient, long clientId, HashMap<String,
133136
for (int i = 1; i < preferredOrder.length; i++) {
134137
DiscordBuild cb = preferredOrder[i];
135138
if (ipcClient.isDebugMode()) {
136-
ipcClient.getLogger().info(String.format("[DEBUG] Looking for client build: %s", cb.name()));
139+
LOGGER.info(String.format("[DEBUG] Looking for client build: %s", cb.name()));
137140
}
138141

139142
if (open[cb.ordinal()] != null) {
@@ -150,7 +153,7 @@ public static Pipe openPipe(IPCClient ipcClient, long clientId, HashMap<String,
150153
} else pipe.build = cb;
151154

152155
if (ipcClient.isDebugMode()) {
153-
ipcClient.getLogger().info(String.format("[DEBUG] Found preferred client: %s", pipe.build.name()));
156+
LOGGER.info(String.format("[DEBUG] Found preferred client: %s", pipe.build.name()));
154157
}
155158
break;
156159
}
@@ -170,7 +173,7 @@ public static Pipe openPipe(IPCClient ipcClient, long clientId, HashMap<String,
170173
// This isn't really important to applications and better
171174
// as debug info
172175
if (ipcClient.isDebugMode()) {
173-
ipcClient.getLogger().info(String.format("[DEBUG] Failed to close an open IPC pipe: %s", ex));
176+
LOGGER.info(String.format("[DEBUG] Failed to close an open IPC pipe: %s", ex));
174177
}
175178
}
176179
}
@@ -252,13 +255,13 @@ public void send(Packet.OpCode op, JsonObject data, Callback callback) {
252255
callbacks.put(nonce, callback);
253256
write(p.toBytes());
254257
if (ipcClient.isDebugMode()) {
255-
ipcClient.getLogger().info(String.format("[DEBUG] Sent packet: %s", p.toDecodedString()));
258+
LOGGER.info(String.format("[DEBUG] Sent packet: %s", p.toDecodedString()));
256259
}
257260

258261
if (listener != null)
259262
listener.onPacketSent(ipcClient, p);
260263
} catch (IOException ex) {
261-
ipcClient.getLogger().severe("Encountered an IOException while sending a packet and disconnected!");
264+
LOGGER.error("Encountered an IOException while sending a packet and disconnected!");
262265
status = PipeStatus.DISCONNECTED;
263266
}
264267
}
@@ -285,7 +288,7 @@ public Packet receive(Packet.OpCode op, byte[] data) {
285288
Packet p = new Packet(op, packetData, ipcClient.getEncoding());
286289

287290
if (ipcClient.isDebugMode()) {
288-
ipcClient.getLogger().info(String.format("[DEBUG] Received packet: %s", p));
291+
LOGGER.info(String.format("[DEBUG] Received packet: %s", p));
289292
}
290293

291294
if (listener != null)

src/main/java/com/jagrosh/discordipc/entities/pipe/UnixPipe.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import com.jagrosh.discordipc.entities.Packet;
2424
import org.newsclub.net.unix.AFUNIXSocket;
2525
import org.newsclub.net.unix.AFUNIXSocketAddress;
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
2628

2729
import java.io.File;
2830
import java.io.FileWriter;
@@ -34,6 +36,7 @@
3436
import java.util.HashMap;
3537

3638
public class UnixPipe extends Pipe {
39+
private static final Logger LOGGER = LoggerFactory.getLogger(UnixPipe.class);
3740
private final AFUNIXSocket socket;
3841

3942
UnixPipe(IPCClient ipcClient, HashMap<String, Callback> callbacks, File location) throws IOException {
@@ -67,7 +70,7 @@ public Packet read() throws IOException, JsonParseException {
6770
ByteBuffer bb = ByteBuffer.wrap(d);
6871

6972
if (ipcClient.isDebugMode() && ipcClient.isVerboseLogging()) {
70-
ipcClient.getLogger().info(String.format("[DEBUG] Read Byte Data: %s with result %s", new String(d), readResult));
73+
LOGGER.info(String.format("[DEBUG] Read Byte Data: %s with result %s", new String(d), readResult));
7174
}
7275

7376
Packet.OpCode op = Packet.OpCode.values()[Integer.reverseBytes(bb.getInt())];
@@ -76,7 +79,7 @@ public Packet read() throws IOException, JsonParseException {
7679
int reversedResult = is.read(d);
7780

7881
if (ipcClient.isDebugMode() && ipcClient.isVerboseLogging()) {
79-
ipcClient.getLogger().info(String.format("[DEBUG] Read Reversed Byte Data: %s with result %s", new String(d), reversedResult));
82+
LOGGER.info(String.format("[DEBUG] Read Reversed Byte Data: %s with result %s", new String(d), reversedResult));
8083
}
8184

8285
return receive(op, d);
@@ -90,7 +93,7 @@ public void write(byte[] b) throws IOException {
9093
@Override
9194
public void close() throws IOException {
9295
if (ipcClient.isDebugMode()) {
93-
ipcClient.getLogger().info("[DEBUG] Closing IPC pipe...");
96+
LOGGER.info("[DEBUG] Closing IPC pipe...");
9497
}
9598

9699
status = PipeStatus.CLOSING;
@@ -133,17 +136,17 @@ public void registerApp(String applicationId, String command) {
133136
String desktopFilePath = home + "/.local";
134137

135138
if (this.mkdir(desktopFilePath))
136-
ipcClient.getLogger().warning("[DEBUG] Failed to create directory '" + desktopFilePath + "', may already exist");
139+
LOGGER.warn("[DEBUG] Failed to create directory '" + desktopFilePath + "', may already exist");
137140

138141
desktopFilePath += "/share";
139142

140143
if (this.mkdir(desktopFilePath))
141-
ipcClient.getLogger().warning("[DEBUG] Failed to create directory '" + desktopFilePath + "', may already exist");
144+
LOGGER.warn("[DEBUG] Failed to create directory '" + desktopFilePath + "', may already exist");
142145

143146
desktopFilePath += "/applications";
144147

145148
if (this.mkdir(desktopFilePath))
146-
ipcClient.getLogger().warning("[DEBUG] Failed to create directory '" + desktopFilePath + "', may already exist");
149+
LOGGER.warn("[DEBUG] Failed to create directory '" + desktopFilePath + "', may already exist");
147150

148151
desktopFilePath += desktopFileName;
149152

src/main/java/com/jagrosh/discordipc/entities/pipe/WindowsPipe.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import com.jagrosh.discordipc.entities.Callback;
2323
import com.jagrosh.discordipc.entities.Packet;
2424
import com.jagrosh.discordipc.impl.WinRegistry;
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
2527

2628
import java.io.File;
2729
import java.io.FileNotFoundException;
@@ -30,6 +32,7 @@
3032
import java.util.HashMap;
3133

3234
public class WindowsPipe extends Pipe {
35+
private static final Logger LOGGER = LoggerFactory.getLogger(WindowsPipe.class);
3336
private static final Float javaSpec = Float.parseFloat(System.getProperty("java.specification.version"));
3437
private final int targetKey = WinRegistry.HKEY_CURRENT_USER;
3538
private final long targetLongKey = targetKey;
@@ -77,7 +80,7 @@ public Packet read() throws IOException, JsonParseException {
7780
@Override
7881
public void close() throws IOException {
7982
if (ipcClient.isDebugMode()) {
80-
ipcClient.getLogger().info("[DEBUG] Closing IPC pipe...");
83+
LOGGER.info("[DEBUG] Closing IPC pipe...");
8184
}
8285

8386
status = PipeStatus.CLOSING;

0 commit comments

Comments
 (0)