Skip to content

Commit

Permalink
Fix ICE4J logging to stdout and not using our loggers
Browse files Browse the repository at this point in the history
rtm516 committed Aug 28, 2024
1 parent 450931c commit 8f21d2e
Showing 8 changed files with 238 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -12,7 +12,6 @@
import com.rtm516.mcxboxbroadcast.core.storage.FileStorageManager;
import org.cloudburstmc.protocol.bedrock.BedrockPong;
import org.slf4j.LoggerFactory;
import org.slf4j.bridge.SLF4JBridgeHandler;

import java.io.File;
import java.io.FileOutputStream;
@@ -21,7 +20,6 @@
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.logging.LogManager;

public class StandaloneMain {
private static StandaloneConfig config;
@@ -31,10 +29,6 @@ public class StandaloneMain {
public static SessionManager sessionManager;

public static void main(String[] args) throws Exception {
// Redirect all logging to SLF4J since ice4j uses java.util.logging
LogManager.getLogManager().reset();
SLF4JBridgeHandler.install();

logger = new StandaloneLoggerImpl(LoggerFactory.getLogger(StandaloneMain.class));

logger.info("Starting MCXboxBroadcast Standalone " + BuildData.VERSION);
8 changes: 0 additions & 8 deletions bootstrap/standalone/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -17,13 +17,5 @@
<AppenderRef ref="Console"/>
<AppenderRef ref="File"/>
</Root>

<!-- Disable logging for ice4j and jitsi -->
<Logger name="org.ice4j" level="OFF">
</Logger>
<Logger name="org.jitsi" level="OFF">
</Logger>
<Logger name="JitsiConfig" level="OFF">
</Logger>
</Loggers>
</Configuration>
Original file line number Diff line number Diff line change
@@ -2,14 +2,20 @@

import kotlin.jvm.functions.Function1;
import kotlin.reflect.KType;
import org.ice4j.ice.harvest.MappingCandidateHarvesters;
import org.jetbrains.annotations.NotNull;
import org.jitsi.config.JitsiConfig;
import org.jitsi.metaconfig.ConfigSource;
import org.jitsi.utils.logging2.LoggerImpl;

import java.lang.reflect.Field;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;

public class CustomIceConfigSource implements ConfigSource {
/**
@@ -152,7 +158,32 @@ private String cleanType(KType type) {
return typeName;
}

public static void install() {
public static void install(com.rtm516.mcxboxbroadcast.core.Logger logger) {
// Disable the Jitsi loggers
try {
Function<String, Logger> newLoggerFactory = s -> {
Logger newLogger = Logger.getLogger(s);
newLogger.setLevel(Level.OFF);
return newLogger;
};

Field loggerFactoryField = LoggerImpl.class.getDeclaredField("loggerFactory");
loggerFactoryField.setAccessible(true);
loggerFactoryField.set(null, newLoggerFactory);

// Disable the MappingCandidateHarvesters logger
disableClassLogger(MappingCandidateHarvesters.class);
} catch (NoSuchFieldException | IllegalAccessException e) {
logger.error("Failed to disable Jitsi logger, may see more logs than expected", e);
}

// TODO Disable the logger for config
JitsiConfig.Companion.useDebugNewConfig(new CustomIceConfigSource());
}

private static void disableClassLogger(Class<?> clazz) throws IllegalAccessException, NoSuchFieldException {
Field loggerField = clazz.getDeclaredField("logger");
loggerField.setAccessible(true);
((Logger)loggerField.get(null)).setLevel(Level.OFF);
}
}
24 changes: 24 additions & 0 deletions core/src/main/java/com/rtm516/mcxboxbroadcast/core/Logger.java
Original file line number Diff line number Diff line change
@@ -2,6 +2,9 @@

import net.raphimc.minecraftauth.util.logging.ILogger;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
* A basic logger interface to allow for custom logger implementations and wrappers
*/
@@ -17,4 +20,25 @@ public interface Logger extends ILogger {
void debug(String message);

Logger prefixed(String prefixString);

/**
* Helper to get the stack trace as a string
*
* @param ex the exception
* @return the stack trace as a string
*/
default String getStackTrace(Throwable ex) {
// Create a PrintWriter to write the stack trace to
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);

// Write the stack trace to the PrintWriter
ex.printStackTrace(pw);

// Close the PrintWriter
pw.close();

// Return the stack trace as a string
return sw.toString();
}
}
Original file line number Diff line number Diff line change
@@ -69,7 +69,7 @@ public SessionManagerCore(StorageManager storageManager, Logger logger) {
this.friendManager = new FriendManager(httpClient, logger, this);

// Load the ICE configuration
CustomIceConfigSource.install();
CustomIceConfigSource.install(logger);
}

/**
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package com.rtm516.mcxboxbroadcast.core.webrtc;

import com.rtm516.mcxboxbroadcast.core.Logger;

import java.util.Map;
import java.util.function.Supplier;
import java.util.logging.Handler;
import java.util.logging.Level;

public class IceLogger implements org.jitsi.utils.logging2.Logger {
private final Logger logger;

public IceLogger(Logger logger) {
this.logger = logger.prefixed("Ice4J");
}


@Override
public org.jitsi.utils.logging2.Logger createChildLogger(String name, Map<String, String> context) {
return this;
}

@Override
public org.jitsi.utils.logging2.Logger createChildLogger(String name) {
return this;
}

@Override
public void setUseParentHandlers(boolean useParentHandlers) {

}

@Override
public void addHandler(Handler handler) throws SecurityException {

}

@Override
public void removeHandler(Handler handler) throws SecurityException {

}

@Override
public boolean isTraceEnabled() {
return false;
}

@Override
public void trace(Object msg) {

}

@Override
public void trace(Supplier<String> msgSupplier) {

}

@Override
public boolean isDebugEnabled() {
return true; // We handle this in the logger so just claim its always enabled
}

@Override
public void debug(Object msg) {
logger.debug(msg.toString());
}

@Override
public void debug(Supplier<String> msgSupplier) {
logger.debug(msgSupplier.get());
}

@Override
public boolean isInfoEnabled() {
return true;
}

@Override
public void info(Object msg) {
logger.debug(msg.toString());
}

@Override
public void info(Supplier<String> msgSupplier) {
logger.debug(msgSupplier.get());
}

@Override
public boolean isWarnEnabled() {
return true;
}

@Override
public void warn(Object msg) {
logger.debug(msg.toString());
}

@Override
public void warn(Supplier<String> msgSupplier) {
logger.debug(msgSupplier.get());
}

@Override
public void warn(Object msg, Throwable t) {
logger.debug(msg.toString() + "\n" + logger.getStackTrace(t));
}

@Override
public void error(Object msg) {
logger.error(msg.toString());
}

@Override
public void error(Supplier<String> msgSupplier) {
logger.error(msgSupplier.get());
}

@Override
public void error(Object msg, Throwable t) {
logger.error(msg.toString(), t);
}

@Override
public void setLevelError() {

}

@Override
public void setLevelWarn() {

}

@Override
public void setLevelInfo() {

}

@Override
public void setLevelDebug() {

}

@Override
public void setLevelTrace() {

}

@Override
public void setLevelAll() {

}

@Override
public void setLevelOff() {

}

@Override
public void setLevel(Level level) {

}

@Override
public Level getLevel() {
return null;
}

@Override
public void addContext(Map<String, String> addedContext) {

}

@Override
public void addContext(String key, String value) {

}
}
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@ public class PeerSession {
public PeerSession(RtcWebsocketClient rtcWebsocket, List<CandidateHarvester> candidateHarvesters) {
this.rtcWebsocket = rtcWebsocket;

this.agent = new Agent();
this.agent = new Agent(new IceLogger(rtcWebsocket.logger()));
for (CandidateHarvester harvester : candidateHarvesters) {
agent.addCandidateHarvester(harvester);
}
9 changes: 3 additions & 6 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -10,10 +10,9 @@ terminalconsoleappender = "1.3.0"
methanol = "1.7.0"
minecraftauth = "4.1.1-20240822.193417-8"
gson = "2.11.0"
ice4j = "3.0-72-g824cd4b"
ice4j = "15836a645f"
bouncycastle = "1.78.1"
sctp4j = "c0c23cd1c8"
slf4j-bridge = "2.0.16"

shadow = "8.3.0"
indra = "3.1.3"
@@ -45,7 +44,6 @@ terminalconsoleappender = { group = "net.minecrell", name = "terminalconsoleappe
log4j-api = { group = "org.apache.logging.log4j", name = "log4j-api", version.ref = "log4j" }
log4j-core = { group = "org.apache.logging.log4j", name = "log4j-core", version.ref = "log4j" }
log4j-slf4j2-impl = { group = "org.apache.logging.log4j", name = "log4j-slf4j2-impl", version.ref = "log4j" }
slf4j-bridge = { group = "org.slf4j", name = "jul-to-slf4j", version.ref = "slf4j-bridge" }

jline-terminal = { group = "org.jline", name = "jline-terminal", version.ref = "jline" }
jline-terminal-jna = { group = "org.jline", name = "jline-terminal-jna", version.ref = "jline" }
@@ -59,7 +57,7 @@ bouncycastle-prov = { group = "org.bouncycastle", name = "bcprov-jdk18on", versi
bouncycastle-tls = { group = "org.bouncycastle", name = "bctls-jdk18on", version.ref = "bouncycastle" }
bouncycastle-pkix = { group = "org.bouncycastle", name = "bcpkix-jdk18on", version.ref = "bouncycastle" }

ice4j = { group = "org.jitsi", name = "ice4j", version.ref = "ice4j" }
ice4j = { group = "com.github.MCXboxBroadcast", name = "ice4j", version.ref = "ice4j" }
sctp4j = { group = "com.github.MCXboxBroadcast", name = "sctp4j", version.ref = "sctp4j" }

[bundles]
@@ -74,8 +72,7 @@ geyser = [
log4j = [
"log4j-api",
"log4j-core",
"log4j-slf4j2-impl",
"slf4j-bridge"
"log4j-slf4j2-impl"
]
jline = [
"jline-terminal",

0 comments on commit 8f21d2e

Please sign in to comment.