From f3ab94dcd1528e27f6925bd998cbeda676f2602e Mon Sep 17 00:00:00 2001 From: Hilbrand Bouwkamp Date: Thu, 16 Feb 2023 11:22:56 +0100 Subject: [PATCH] Only log to console when debug is enabled. Made use of var_args. Not sure why it didn't work as mentioned in the javadoc. Maybe an older version of GWT? --- .../main/java/nl/aerius/wui/dev/GWTProd.java | 76 +++++++------------ 1 file changed, 28 insertions(+), 48 deletions(-) diff --git a/gwt-client-common/src/main/java/nl/aerius/wui/dev/GWTProd.java b/gwt-client-common/src/main/java/nl/aerius/wui/dev/GWTProd.java index ba39116..2fa4056 100644 --- a/gwt-client-common/src/main/java/nl/aerius/wui/dev/GWTProd.java +++ b/gwt-client-common/src/main/java/nl/aerius/wui/dev/GWTProd.java @@ -16,81 +16,61 @@ */ package nl.aerius.wui.dev; +import java.util.stream.Stream; + import elemental2.dom.DomGlobal; /** - * Apparently varargs don't work swimmingly, so implement a bunch of overloads instead. + * Conditional log in the browser console. */ public final class GWTProd { + private static boolean debug = false; private static boolean dev = true; private GWTProd() {} - public static void setIsDev(final boolean dev) { - GWTProd.dev = dev; - } - - public static boolean isDev() { - return dev; + public static void setDebug(final boolean debug) { + GWTProd.debug = debug; } - public static void log(final Object msg) { - DomGlobal.console.log(msg); - } - - public static void log(final Object a, final Object b) { - DomGlobal.console.log(a, b); - } - - public static void log(final Object a, final Object b, final Object c) { - DomGlobal.console.log(a, b, c); - } - - public static void info(final Object msg) { - DomGlobal.console.info(msg); - } - - public static void info(final Object a, final Object b) { - DomGlobal.console.info(a, b); + public static void setIsDev(final boolean dev) { + GWTProd.dev = dev; } - public static void info(final Object a, final Object b, final Object c) { - DomGlobal.console.info(a, b, c); + public static boolean isDebug() { + return debug; } - public static void warn(final Object msg) { - DomGlobal.console.warn(msg); + public static boolean isDev() { + return dev; } - public static void warn(final Object a, final Object b) { - DomGlobal.console.warn(a, b); + public static void log(final Object... msg) { + logIfDebug(() -> DomGlobal.console.log(msg)); } - public static void warn(final Object a, final Object b, final Object c) { - DomGlobal.console.warn(a, b, c); + public static void info(final Object... msg) { + logIfDebug(() -> DomGlobal.console.info(msg)); } - public static void error(final Object msg) { - DomGlobal.console.warn(msg); - tryReport(msg); + public static void warn(final Object... msg) { + logIfDebug(() -> DomGlobal.console.warn(msg)); } - public static void error(final Object a, final Object b) { - DomGlobal.console.error(a, b); - tryReport(a); - tryReport(b); + public static void error(final Object... msg) { + logIfDebug(() -> { + DomGlobal.console.error(msg); + tryReport(msg); + }); } - public static void error(final Object a, final Object b, final Object c) { - DomGlobal.console.error(a, b, c); - tryReport(a); - tryReport(b); - tryReport(c); + private static void tryReport(final Object... ex) { + Stream.of(ex).filter(e -> e instanceof Throwable).forEach(e -> ((Throwable) e).printStackTrace()); } - private static void tryReport(final Object ex) { - if (ex instanceof Throwable) { - ((Throwable) ex).printStackTrace(); + private static void logIfDebug(final Runnable log) { + if (debug) { + log.run(); } } }