Skip to content
Merged
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
@@ -1,6 +1,8 @@
package me.devnatan.inventoryframework;

import java.util.function.Supplier;

import me.devnatan.inventoryframework.logging.Logger;
import org.intellij.lang.annotations.PrintFormat;
import org.jetbrains.annotations.ApiStatus;

Expand All @@ -11,31 +13,23 @@
@ApiStatus.Internal
public final class IFDebug {

private static final String PREFIX = "[IF]";
private static final String SYSTEM_PROPERTY = "me.devnatan.inventoryframework.debug";

private static Boolean DEBUG_ENABLED = null;

private IFDebug() {}
private static Logger logger;

/**
* Returns if debug is enabled.
*
* @return If debug is enabled.
*/
public static boolean isDebugEnabled() {
if (DEBUG_ENABLED == null) DEBUG_ENABLED = Boolean.parseBoolean(System.getProperty(SYSTEM_PROPERTY, "false"));
static {
DEBUG_ENABLED = Boolean.parseBoolean(System.getProperty(SYSTEM_PROPERTY, "false"));
}

return DEBUG_ENABLED;
}
private IFDebug() {}

/**
* Enables InventoryFramework debug.
*
* @param enabled If debug should be enabled.
*/
public static void setEnabled(boolean enabled) {
System.setProperty(SYSTEM_PROPERTY, String.valueOf(enabled));
/** Enables InventoryFramework debug. */
public static void enable(Logger logger) {
IFDebug.logger = logger;
DEBUG_ENABLED = true;
debug("Debug enabled");
}

/**
Expand All @@ -45,8 +39,8 @@ public static void setEnabled(boolean enabled) {
* @param args Arguments to apply to the message
*/
public static void debug(Supplier<String> message, Object... args) {
if (!isDebugEnabled()) return;
System.out.println(PREFIX + " " + String.format(message.get(), args));
if (!DEBUG_ENABLED) return;
logger.debug(String.format(message.get(), args));
}

/**
Expand All @@ -56,7 +50,7 @@ public static void debug(Supplier<String> message, Object... args) {
* @param args Arguments to apply to the message
*/
public static void debug(@PrintFormat String message, Object... args) {
if (!isDebugEnabled()) return;
System.out.println(PREFIX + " " + String.format(message, args));
if (!DEBUG_ENABLED) return;
logger.debug(String.format(message, args));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import me.devnatan.inventoryframework.feature.FeatureInstaller;
import me.devnatan.inventoryframework.internal.BukkitElementFactory;
import me.devnatan.inventoryframework.internal.PlatformUtils;
import me.devnatan.inventoryframework.logging.BukkitLogger;
import me.devnatan.inventoryframework.runtime.thirdparty.Metrics;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
Expand Down Expand Up @@ -186,6 +187,10 @@ public final void openEndless(
public final ViewFrame register() {
if (isRegistered()) throw new IllegalStateException("This view frame is already registered");

if (debugEnabled) {
IFDebug.enable(new BukkitLogger(getOwner().getLogger(), "global", true));
}

PlatformUtils.setFactory(new BukkitElementFactory(getOwner()));
tryEnableMetrics();
checkRelocationIssues();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public abstract class IFViewFrame<S extends IFViewFrame<S, V>, V extends Platfor
protected final Map<String, Viewer> viewerById = new HashMap<>();
protected Consumer<ViewConfigBuilder> defaultConfig;

protected boolean debugEnabled;

@SuppressWarnings("rawtypes")
private final Pipeline<IFViewFrame> pipeline = new Pipeline<>(FRAME_REGISTERED, FRAME_UNREGISTERED);

Expand Down Expand Up @@ -264,7 +266,7 @@ final Consumer<ViewConfigBuilder> getDefaultConfig() {
@SuppressWarnings("unchecked")
@ApiStatus.Internal
public final S enableDebug() {
IFDebug.setEnabled(true);
this.debugEnabled = true;
return (S) this;
}
}
Loading