From 922076d807974cefa6ade28f689a20f1a90fddeb Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Tue, 5 Apr 2022 11:19:47 +0200 Subject: provide more control on the logging to users --- .../uic/barcode/asn1/datatypes/Asn1SequenceOf.java | 8 ++-- .../org/uic/barcode/asn1/uper/StringCoder.java | 8 +--- src/main/java/org/uic/barcode/logger/Logger.java | 20 +++++++- .../java/org/uic/barcode/logger/LoggerFactory.java | 55 +++++++++++++++++++++- 4 files changed, 76 insertions(+), 15 deletions(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/uic/barcode/asn1/datatypes/Asn1SequenceOf.java b/src/main/java/org/uic/barcode/asn1/datatypes/Asn1SequenceOf.java index 4924b50..8e24222 100644 --- a/src/main/java/org/uic/barcode/asn1/datatypes/Asn1SequenceOf.java +++ b/src/main/java/org/uic/barcode/asn1/datatypes/Asn1SequenceOf.java @@ -3,8 +3,7 @@ package org.uic.barcode.asn1.datatypes; import java.lang.reflect.ParameterizedType; import java.util.*; -import org.uic.barcode.logger.Logger; -import org.uic.barcode.logger.LoggerFactory; +import org.uic.barcode.asn1.uper.UperEncoder; /** @@ -35,8 +34,7 @@ import org.uic.barcode.logger.LoggerFactory; * @param type of elements contained. */ public abstract class Asn1SequenceOf extends AbstractList { - private final static Logger logger = LoggerFactory.getLogger("asnLogger"); - + private final List bakingList; @Override public T get(int index) { return bakingList.get(index); } @@ -45,7 +43,7 @@ public abstract class Asn1SequenceOf extends AbstractList { public Asn1SequenceOf() { this(new ArrayList()); } public Asn1SequenceOf(Collection coll) { - logger.debug(String.format("Instantiating Sequence Of %s with %s", + UperEncoder.logger.debug(String.format("Instantiating Sequence Of %s with %s", ((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0], coll)); bakingList = new ArrayList<>(coll); diff --git a/src/main/java/org/uic/barcode/asn1/uper/StringCoder.java b/src/main/java/org/uic/barcode/asn1/uper/StringCoder.java index a504096..94e0aad 100644 --- a/src/main/java/org/uic/barcode/asn1/uper/StringCoder.java +++ b/src/main/java/org/uic/barcode/asn1/uper/StringCoder.java @@ -17,14 +17,10 @@ import org.uic.barcode.asn1.datatypes.DefaultAlphabet; import org.uic.barcode.asn1.datatypes.FixedSize; import org.uic.barcode.asn1.datatypes.RestrictedString; import org.uic.barcode.asn1.datatypes.SizeRange; -import org.uic.barcode.logger.Logger; -import org.uic.barcode.logger.LoggerFactory; class StringCoder implements Decoder, Encoder { - private static final Logger LOGGER = LoggerFactory.getLogger("asnLogger"); - @Override public boolean canEncode(T obj, Annotation[] extraAnnotations) { return obj instanceof String || obj instanceof Asn1String; } @@ -231,7 +227,7 @@ class StringCoder implements Decoder, Encoder { try { chars = UperEncoder.instantiate(restriction.alphabet()).chars().toCharArray(); } catch (IllegalArgumentException e) { - LOGGER.info("Uninstantinatable alphabet ", e); + UperEncoder.logger.info("Uninstantinatable alphabet ", e); throw new IllegalArgumentException("Uninstantinatable alphabet" + restriction.alphabet().getName()); } if (BigInteger.valueOf(chars.length - 1).bitLength() < BigInteger.valueOf(126) @@ -293,7 +289,7 @@ class StringCoder implements Decoder, Encoder { try { chars = UperEncoder.instantiate(restrictionAnnotation.alphabet()).chars().toCharArray(); } catch (IllegalArgumentException e) { - LOGGER.info("Uninstantinatable alphabet ", e); + UperEncoder.logger.info("Uninstantinatable alphabet ", e); throw new IllegalArgumentException("Uninstantinatable alphabet " + restrictionAnnotation.alphabet().getName()); } if (BigInteger.valueOf(chars.length - 1).bitLength() < BigInteger.valueOf(126) diff --git a/src/main/java/org/uic/barcode/logger/Logger.java b/src/main/java/org/uic/barcode/logger/Logger.java index b536154..baae70d 100644 --- a/src/main/java/org/uic/barcode/logger/Logger.java +++ b/src/main/java/org/uic/barcode/logger/Logger.java @@ -3,11 +3,23 @@ package org.uic.barcode.logger; import java.util.logging.Level; -public class Logger { +public class Logger { + + private boolean consoleLog = false; + + public Logger(boolean consoleLog) { + this.consoleLog = consoleLog; + } + + public Logger() { + this.consoleLog = false; + } public void debug(String text){ java.util.logging.Logger.getGlobal().log(Level.FINEST,text); - System.out.println(text); + if (consoleLog) { + System.out.println(text); + } } public void log(Level level,String text){ @@ -17,6 +29,10 @@ public class Logger { public void info(String text, IllegalArgumentException e) { java.util.logging.Logger.getGlobal().log(Level.INFO,text); } + + public void activateConsoleLog(boolean consoleLogActive) { + this.consoleLog = consoleLogActive; + } } diff --git a/src/main/java/org/uic/barcode/logger/LoggerFactory.java b/src/main/java/org/uic/barcode/logger/LoggerFactory.java index 0a1a83c..be5736e 100644 --- a/src/main/java/org/uic/barcode/logger/LoggerFactory.java +++ b/src/main/java/org/uic/barcode/logger/LoggerFactory.java @@ -1,9 +1,60 @@ package org.uic.barcode.logger; +import java.util.HashMap; + +/** + * A factory for creating Logger objects. This allows to replace the default logger by your own loggers. + */ public class LoggerFactory { + + /** The registered loggers. */ + private static HashMap registeredLoggers = new HashMap(); + + /** The activate console log. */ + public static boolean activateConsoleLog = false; + + /** + * Gets the logger. + * + * @param name the name of the logger. UperEncoder uses asnLogger + * @return the logger + */ public static Logger getLogger(String name) { - return new Logger(); - } + Logger logger = registeredLoggers.get(name); + if (logger == null) { + logger = new Logger(activateConsoleLog); + registeredLoggers.put(name, logger); + } + return logger; + } + /** + * Checks if console log is on. + * + * @return true, if console log is on + */ + public static boolean isActivateConsoleLog() { + return activateConsoleLog; + } + + /** + * Sets the console log on + * + * @param activateConsoleLog the new activate console log + */ + public static void setActivateConsoleLog(boolean activateConsoleLog) { + LoggerFactory.activateConsoleLog = activateConsoleLog; + } + + /** + * Register logger. + * + * @param name the name of the logger + * @param logger the logger to be registered + */ + public void registerLogger(String name, Logger logger) { + registeredLoggers.put(name, logger); + } + } -- cgit v1.2.3