Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add static GeyserLogger instance to the GeyserLogger interface #4996

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import net.md_5.bungee.protocol.ProtocolConstants;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.GeyserLogger;
import org.geysermc.geyser.ping.GeyserPingInfo;
import org.geysermc.geyser.ping.IGeyserPingPassthrough;

Expand Down Expand Up @@ -68,7 +69,7 @@ public GeyserPingInfo getPingInformation(InetSocketAddress inetSocketAddress) {
event = future.get(100, TimeUnit.MILLISECONDS);
} catch (Throwable cause) {
String address = GeyserImpl.getInstance().getConfig().isLogPlayerIpAddresses() ? inetSocketAddress.toString() : "<IP address withheld>";
GeyserImpl.getInstance().getLogger().error("Failed to get ping information for " + address, cause);
GeyserLogger.get().error("Failed to get ping information for " + address, cause);
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import net.minecraft.server.network.ServerConnectionListener;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.geyser.GeyserBootstrap;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.GeyserLogger;
import org.geysermc.geyser.network.netty.GeyserInjector;
import org.geysermc.geyser.network.netty.LocalServerChannelWrapper;
import org.geysermc.geyser.platform.mod.platform.GeyserModPlatform;
Expand Down Expand Up @@ -149,7 +149,7 @@ public void shutdown() {
eventLoopGroup.shutdownGracefully().sync();
eventLoopGroup = null;
} catch (Exception e) {
GeyserImpl.getInstance().getLogger().error("Unable to shut down injector! " + e.getMessage());
GeyserLogger.get().error("Unable to shut down injector! " + e.getMessage());
e.printStackTrace();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ public void debug(String message) {
}
}

@Override
public void debug(String message, Object... parameters) {
if (debug) {
logger.info(message, parameters);
}
}

@Override
public void setDebug(boolean debug) {
this.debug = debug;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.GeyserLogger;
import org.geysermc.geyser.command.GeyserCommandSource;
import org.geysermc.geyser.text.ChatColor;

Expand All @@ -58,7 +59,7 @@ public void sendMessage(@NonNull String message) {
if (source.getEntity() instanceof ServerPlayer) {
((ServerPlayer) source.getEntity()).displayClientMessage(Component.literal(message), false);
} else {
GeyserImpl.getInstance().getLogger().info(ChatColor.toANSI(message + ChatColor.RESET));
GeyserLogger.get().info(ChatColor.toANSI(message + ChatColor.RESET));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import org.bukkit.Bukkit;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.GeyserLogger;

/**
* Disables the compression packet (and the compression handlers from being added to the pipeline) for Geyser clients
Expand All @@ -56,7 +56,7 @@ public class GeyserSpigotCompressionDisabler extends ChannelOutboundHandlerAdapt
loginSuccessPacketClass = findLoginSuccessPacket();
enabled = true;
} catch (Exception e) {
GeyserImpl.getInstance().getLogger().error("Could not initialize compression disabler!", e);
GeyserLogger.get().error("Could not initialize compression disabler!", e);
}
COMPRESSION_PACKET_CLASS = compressionPacketClass;
LOGIN_SUCCESS_PACKET_CLASS = loginSuccessPacketClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@

package org.geysermc.geyser.platform.spigot;

import org.geysermc.mcprotocollib.protocol.data.DefaultComponentSerializer;
import net.kyori.adventure.text.Component;
import org.bukkit.command.CommandSender;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.GeyserLogger;
import org.geysermc.mcprotocollib.protocol.data.DefaultComponentSerializer;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
Expand Down Expand Up @@ -79,7 +79,7 @@ public final class PaperAdventure {
nativeGsonComponentSerializerDeserializeMethodBound = nativeGsonComponentSerializerDeserializeMethod
.bindTo(nativeGsonComponentSerializerGsonGetter.invoke());
} catch (final Throwable throwable) {
GeyserImpl.getInstance().getLogger().error("Failed to access native GsonComponentSerializer", throwable);
GeyserLogger.get().error("Failed to access native GsonComponentSerializer", throwable);
}
}
}
Expand All @@ -94,7 +94,7 @@ public final class PaperAdventure {
try {
playerComponentSendMessage = CommandSender.class.getMethod("sendMessage", nativeComponentClass);
} catch (final NoSuchMethodException e) {
if (GeyserImpl.getInstance().getLogger().isDebug()) {
if (GeyserLogger.get().isDebug()) {
e.printStackTrace();
}
}
Expand All @@ -104,21 +104,21 @@ public final class PaperAdventure {

public static @Nullable Object toNativeComponent(final Component component) {
if (NATIVE_GSON_COMPONENT_SERIALIZER_DESERIALIZE_METHOD_BOUND == null) {
GeyserImpl.getInstance().getLogger().error("Illegal state where Component serialization was called when it wasn't available!");
GeyserLogger.get().error("Illegal state where Component serialization was called when it wasn't available!");
return null;
}

try {
return NATIVE_GSON_COMPONENT_SERIALIZER_DESERIALIZE_METHOD_BOUND.invoke(DefaultComponentSerializer.get().serialize(component));
} catch (final Throwable throwable) {
GeyserImpl.getInstance().getLogger().error("Failed to create native Component message", throwable);
GeyserLogger.get().error("Failed to create native Component message", throwable);
return null;
}
}

public static void sendMessage(final CommandSender sender, final Component component) {
if (SEND_MESSAGE_COMPONENT == null) {
GeyserImpl.getInstance().getLogger().error("Illegal state where Component sendMessage was called when it wasn't available!");
GeyserLogger.get().error("Illegal state where Component sendMessage was called when it wasn't available!");
return;
}

Expand All @@ -127,7 +127,7 @@ public static void sendMessage(final CommandSender sender, final Component compo
try {
SEND_MESSAGE_COMPONENT.invoke(sender, nativeComponent);
} catch (final InvocationTargetException | IllegalAccessException e) {
GeyserImpl.getInstance().getLogger().error("Failed to send native Component message", e);
GeyserLogger.get().error("Failed to send native Component message", e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import org.geysermc.erosion.bukkit.BukkitUtils;
import org.geysermc.erosion.bukkit.PickBlockUtils;
import org.geysermc.erosion.bukkit.SchedulerUtils;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.GeyserLogger;
import org.geysermc.geyser.level.GameRule;
import org.geysermc.geyser.level.WorldManager;
import org.geysermc.geyser.registry.BlockRegistries;
Expand Down Expand Up @@ -94,7 +94,7 @@ public boolean hasOwnChunkCache() {
public boolean getGameRuleBool(GeyserSession session, GameRule gameRule) {
org.bukkit.GameRule<?> bukkitGameRule = org.bukkit.GameRule.getByName(gameRule.getJavaID());
if (bukkitGameRule == null) {
GeyserImpl.getInstance().getLogger().debug("Unknown game rule " + gameRule.getJavaID());
GeyserLogger.get().debug("Unknown game rule " + gameRule.getJavaID());
return gameRule.getDefaultBooleanValue();
}

Expand All @@ -103,23 +103,23 @@ public boolean getGameRuleBool(GeyserSession session, GameRule gameRule) {
if (value instanceof Boolean booleanValue) {
return booleanValue;
}
GeyserImpl.getInstance().getLogger().debug("Expected a bool for " + gameRule + " but got " + value);
GeyserLogger.get().debug("Expected a bool for " + gameRule + " but got " + value);
return gameRule.getDefaultBooleanValue();
}

@Override
public int getGameRuleInt(GeyserSession session, GameRule gameRule) {
org.bukkit.GameRule<?> bukkitGameRule = org.bukkit.GameRule.getByName(gameRule.getJavaID());
if (bukkitGameRule == null) {
GeyserImpl.getInstance().getLogger().debug("Unknown game rule " + gameRule.getJavaID());
GeyserLogger.get().debug("Unknown game rule " + gameRule.getJavaID());
return gameRule.getDefaultIntValue();
}
Player bukkitPlayer = Objects.requireNonNull(Bukkit.getPlayer(session.getPlayerEntity().getUuid()));
Object value = bukkitPlayer.getWorld().getGameRuleValue(bukkitGameRule);
if (value instanceof Integer intValue) {
return intValue;
}
GeyserImpl.getInstance().getLogger().debug("Expected an int for " + gameRule + " but got " + value);
GeyserLogger.get().debug("Expected an int for " + gameRule + " but got " + value);
return gameRule.getDefaultIntValue();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public void debug(String message) {
log.debug(ChatColor.GRAY + message);
}

@Override
public void debug(String message, Object... parameters) {
log.debug(ChatColor.GRAY + message, parameters);
}

@Override
public void setDebug(boolean debug) {
Configurator.setLevel(log.getName(), debug ? Level.DEBUG : Level.INFO);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.GeyserLogger;

import java.lang.reflect.Method;

Expand Down Expand Up @@ -61,7 +61,7 @@ public class GeyserVelocityCompressionDisabler extends ChannelDuplexHandler {
.getMethod("setCompressionThreshold", int.class);
enabled = true;
} catch (Exception e) {
GeyserImpl.getInstance().getLogger().error("Could not initialize compression disabler!", e);
GeyserLogger.get().error("Could not initialize compression disabler!", e);
}

ENABLED = enabled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,11 @@ public void debug(String message) {
info(message);
}
}
}

@Override
public void debug(String message, Object... parameters) {
if (debug) {
logger.info(message, parameters);
}
}
}
5 changes: 3 additions & 2 deletions core/src/main/java/org/geysermc/geyser/GeyserImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ private GeyserImpl(PlatformType platformType, GeyserBootstrap bootstrap) {
instance = this;

Geyser.set(this);
GeyserLogger.INSTANCE.set(bootstrap.getGeyserLogger());

this.platformType = platformType;
this.bootstrap = bootstrap;
Expand Down Expand Up @@ -591,7 +592,7 @@ private void startInstance() {
String authChain = gson.toJson(javaSession.toJson(fullJavaSession));
savedAuthChains.put(user, authChain);
} catch (Exception e) {
GeyserImpl.getInstance().getLogger().warning("Could not migrate " + entry.getKey() + " to an auth chain! " +
GeyserLogger.get().warning("Could not migrate " + entry.getKey() + " to an auth chain! " +
"They will need to sign in the next time they join Geyser.");
}

Expand Down Expand Up @@ -914,7 +915,7 @@ private void scheduleAuthChainsWrite() {
.withDefaultPrettyPrinter()
.writeValue(writer, this.savedAuthChains);
} catch (IOException e) {
getLogger().error("Unable to write saved refresh tokens!", e);
GeyserLogger.get().error("Unable to write saved refresh tokens!", e);
}
});
}
Expand Down
22 changes: 22 additions & 0 deletions core/src/main/java/org/geysermc/geyser/GeyserLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,19 @@
import org.geysermc.geyser.command.GeyserCommandSource;

import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;

public interface GeyserLogger extends GeyserCommandSource {

AtomicReference<GeyserLogger> INSTANCE = new AtomicReference<>();

/**
* Returns the current GeyserLogger instance.
*/
static GeyserLogger get() {
return INSTANCE.get();
}

/**
* Logs a severe message to console
*
Expand Down Expand Up @@ -103,6 +113,18 @@ default void debug(@Nullable Object object) {
debug(String.valueOf(object));
}

/**
* Logs a message with parameters if debug mode is enabled.
*
* @param message the message with placeholders for the objects
* @param parameters the parameters for the message
*/
default void debug(String message, Object... parameters) {
if (isDebug()) {
debug(String.format(message, parameters));
}
}

/**
* Sets if the logger should print debug messages
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
package org.geysermc.geyser.command;

import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.GeyserLogger;
import org.geysermc.geyser.session.GeyserSession;
import org.incendo.cloud.SenderMapper;
Expand Down Expand Up @@ -95,7 +94,7 @@ public static <P, S> CommandSourceConverter<S> layered(Class<S> senderType,
}

if (!(source instanceof GeyserSession)) {
GeyserLogger logger = GeyserImpl.getInstance().getLogger();
GeyserLogger logger = GeyserLogger.get();
if (logger.isDebug()) {
logger.debug("Falling back to UUID for command sender lookup for a command source that is not a GeyserSession: " + source);
Thread.dumpStack();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
package org.geysermc.geyser.command;

import io.leangen.geantyref.GenericTypeReflector;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.GeyserLogger;
import org.geysermc.geyser.text.ChatColor;
import org.geysermc.geyser.text.GeyserLocale;
Expand Down Expand Up @@ -112,7 +111,7 @@ private static void handleNoPermission(GeyserCommandSource source, NoPermissionE
return;
}
} else {
GeyserLogger logger = GeyserImpl.getInstance().getLogger();
GeyserLogger logger = GeyserLogger.get();
if (logger.isDebug()) {
logger.debug("Expected a GeyserPermission.Result for %s but instead got %s from %s".formatted(exception.currentChain(), exception.permissionResult(), exception.missingPermission()));
}
Expand All @@ -124,6 +123,6 @@ private static void handleNoPermission(GeyserCommandSource source, NoPermissionE

private static void handleUnexpectedThrowable(GeyserCommandSource source, Throwable throwable) {
source.sendMessage(MinecraftLocale.getLocaleString("command.failed", source.locale())); // java edition translation key
GeyserImpl.getInstance().getLogger().error("Exception while executing command handler", throwable);
GeyserLogger.get().error("Exception while executing command handler", throwable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.GeyserLogger;
import org.geysermc.geyser.api.util.TriState;
import org.geysermc.geyser.command.GeyserCommand;
import org.geysermc.geyser.command.GeyserCommandSource;
Expand Down Expand Up @@ -155,7 +156,7 @@ public void execute(CommandContext<GeyserCommandSource> context) {
}

// Issue: does Loopback need applying?
if (LoopbackUtil.needsLoopback(GeyserImpl.getInstance().getLogger())) {
if (LoopbackUtil.needsLoopback(GeyserLogger.get())) {
source.sendMessage("Loopback is not applied on this computer! You will have issues connecting from the same computer. " +
"See here for steps on how to resolve: " + "https://wiki.geysermc.org/geyser/fixing-unable-to-connect-to-world/#using-geyser-on-the-same-computer");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import org.cloudburstmc.protocol.bedrock.codec.BedrockCodec;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.GeyserLogger;
import org.geysermc.geyser.api.util.PlatformType;
import org.geysermc.geyser.api.util.TriState;
import org.geysermc.geyser.command.GeyserCommand;
Expand Down Expand Up @@ -98,7 +99,7 @@ public void execute(CommandContext<GeyserCommandSource> context) {
source.locale(), (latestBuildNumber - buildNumber), "https://geysermc.org/download"
));
} catch (IOException e) {
GeyserImpl.getInstance().getLogger().error(GeyserLocale.getLocaleStringLog("geyser.commands.version.failed"), e);
GeyserLogger.get().error(GeyserLocale.getLocaleStringLog("geyser.commands.version.failed"), e);
source.sendMessage(ChatColor.RED + GeyserLocale.getPlayerLocaleString("geyser.commands.version.failed", source.locale()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.GeyserLogger;
import org.geysermc.geyser.api.event.lifecycle.GeyserRegisterPermissionCheckersEvent;
import org.geysermc.geyser.api.event.lifecycle.GeyserRegisterPermissionsEvent;
import org.geysermc.geyser.api.permission.PermissionChecker;
Expand Down Expand Up @@ -75,7 +76,7 @@ public StandaloneCloudCommandManager(GeyserImpl geyser) {
PermissionConfiguration config = FileUtils.loadConfig(permissionsFile, PermissionConfiguration.class);
basePermissions.addAll(config.getDefaultPermissions());
} catch (Exception e) {
geyser.getLogger().error("Failed to load permissions.yml - proceeding without it", e);
GeyserLogger.get().error("Failed to load permissions.yml - proceeding without it", e);
}
}

Expand Down
Loading