Skip to content

Commit

Permalink
[Revert] Go back to SLF4J
Browse files Browse the repository at this point in the history
- I truly don't know what past me was on going away from this, chief.
  • Loading branch information
CDAGaming committed Sep 25, 2022
1 parent 7afd808 commit 150fdbf
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 33 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>com.kohlschutter.junixsocket</groupId>
<artifactId>junixsocket-common</artifactId>
Expand Down
20 changes: 6 additions & 14 deletions src/main/java/com/jagrosh/discordipc/IPCClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
import com.jagrosh.discordipc.entities.pipe.Pipe;
import com.jagrosh.discordipc.entities.pipe.PipeStatus;
import com.jagrosh.discordipc.exceptions.NoDiscordClientException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Closeable;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.HashMap;
import java.util.logging.Logger;

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

/**
* Retrieves this IPCClient's {@link Logger} to handle received events.
*
* @return the current {@link Logger} instance
*/
public Logger getLogger() {
return this.LOGGER;
}

/**
* Sets this IPCClient's {@link IPCListener} to handle received events.
* <p>
Expand Down Expand Up @@ -341,7 +333,7 @@ public void connect(DiscordBuild... preferredOrder) throws NoDiscordClientExcept
if (debugMode) {
ex.printStackTrace();
} else {
LOGGER.severe("Unable to register application, enable debug mode for trace...");
LOGGER.error("Unable to register application, enable debug mode for trace...");
}
}
}
Expand Down Expand Up @@ -698,7 +690,7 @@ private void readPipe(final IPCClient instance) {
break;
}
} catch (Exception e) {
LOGGER.severe(String.format("Exception when handling event: %s", e));
LOGGER.error(String.format("Exception when handling event: %s", e));
}
}
}
Expand All @@ -707,7 +699,7 @@ private void readPipe(final IPCClient instance) {
if (listener != null)
listener.onClose(instance, p.getJson());
} catch (IOException | JsonParseException ex) {
LOGGER.severe(String.format("Reading thread encountered an Exception: %s", ex));
LOGGER.error(String.format("Reading thread encountered an Exception: %s", ex));

pipe.setStatus(PipeStatus.DISCONNECTED);
if (listener != null)
Expand Down
27 changes: 15 additions & 12 deletions src/main/java/com/jagrosh/discordipc/entities/pipe/Pipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@
import com.jagrosh.discordipc.entities.Packet;
import com.jagrosh.discordipc.entities.User;
import com.jagrosh.discordipc.exceptions.NoDiscordClientException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;

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

try {
File fileLocation = new File(location);
if (fileLocation.exists()) {
if (ipcClient.isDebugMode()) {
ipcClient.getLogger().info(String.format("[DEBUG] Found valid file, attempting connection to IPC: \"%s\"", location));
LOGGER.info(String.format("[DEBUG] Found valid file, attempting connection to IPC: \"%s\"", location));
}
pipe = createPipe(ipcClient, callbacks, fileLocation);

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

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

// we're done if we found our first choice
if (pipe.build == preferredOrder[0] || DiscordBuild.ANY == preferredOrder[0]) {
if (ipcClient.isDebugMode()) {
ipcClient.getLogger().info(String.format("[DEBUG] Found preferred client: %s", pipe.build.name()));
LOGGER.info(String.format("[DEBUG] Found preferred client: %s", pipe.build.name()));
}
break;
}
Expand All @@ -119,7 +122,7 @@ public static Pipe openPipe(IPCClient ipcClient, long clientId, HashMap<String,
}
} else {
if (ipcClient.isDebugMode()) {
ipcClient.getLogger().info(String.format("[DEBUG] Unable to locate IPC Pipe: \"%s\"", location));
LOGGER.info(String.format("[DEBUG] Unable to locate IPC Pipe: \"%s\"", location));
}
}
} catch (IOException | JsonParseException ex) {
Expand All @@ -133,7 +136,7 @@ public static Pipe openPipe(IPCClient ipcClient, long clientId, HashMap<String,
for (int i = 1; i < preferredOrder.length; i++) {
DiscordBuild cb = preferredOrder[i];
if (ipcClient.isDebugMode()) {
ipcClient.getLogger().info(String.format("[DEBUG] Looking for client build: %s", cb.name()));
LOGGER.info(String.format("[DEBUG] Looking for client build: %s", cb.name()));
}

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

if (ipcClient.isDebugMode()) {
ipcClient.getLogger().info(String.format("[DEBUG] Found preferred client: %s", pipe.build.name()));
LOGGER.info(String.format("[DEBUG] Found preferred client: %s", pipe.build.name()));
}
break;
}
Expand All @@ -170,7 +173,7 @@ public static Pipe openPipe(IPCClient ipcClient, long clientId, HashMap<String,
// This isn't really important to applications and better
// as debug info
if (ipcClient.isDebugMode()) {
ipcClient.getLogger().info(String.format("[DEBUG] Failed to close an open IPC pipe: %s", ex));
LOGGER.info(String.format("[DEBUG] Failed to close an open IPC pipe: %s", ex));
}
}
}
Expand Down Expand Up @@ -252,13 +255,13 @@ public void send(Packet.OpCode op, JsonObject data, Callback callback) {
callbacks.put(nonce, callback);
write(p.toBytes());
if (ipcClient.isDebugMode()) {
ipcClient.getLogger().info(String.format("[DEBUG] Sent packet: %s", p.toDecodedString()));
LOGGER.info(String.format("[DEBUG] Sent packet: %s", p.toDecodedString()));
}

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

if (ipcClient.isDebugMode()) {
ipcClient.getLogger().info(String.format("[DEBUG] Received packet: %s", p));
LOGGER.info(String.format("[DEBUG] Received packet: %s", p));
}

if (listener != null)
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/com/jagrosh/discordipc/entities/pipe/UnixPipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.jagrosh.discordipc.entities.Packet;
import org.newsclub.net.unix.AFUNIXSocket;
import org.newsclub.net.unix.AFUNIXSocketAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileWriter;
Expand All @@ -34,6 +36,7 @@
import java.util.HashMap;

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

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

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

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

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

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

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

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

desktopFilePath += "/share";

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

desktopFilePath += "/applications";

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

desktopFilePath += desktopFileName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.jagrosh.discordipc.entities.Callback;
import com.jagrosh.discordipc.entities.Packet;
import com.jagrosh.discordipc.impl.WinRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileNotFoundException;
Expand All @@ -30,6 +32,7 @@
import java.util.HashMap;

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

status = PipeStatus.CLOSING;
Expand Down

0 comments on commit 150fdbf

Please sign in to comment.