1 package org.hyphenType.debug; 2 3 import java.io.PrintStream; 4 5 /** 6 * A logger that only outputs things to a {@link PrintStream} if the 7 * {@link HTLogger#HYPHEN_TYPE_DEBUG_MODE} environment variable is set to true (ignoring case). 8 * This class also allows for the user to replace the {@link PrintStream}. The default 9 * {@link PrintStream} is {@link System#out}. 10 * 11 * @author akira 12 */ 13 public class HTLogger { 14 15 /** 16 * System environment key to allow for users to change the debug mode when calling the JVM 17 */ 18 public static final String HYPHEN_TYPE_DEBUG_MODE = "org.hyphenType.debug.debug-mode"; 19 20 /** 21 * If true, this class was already initialized. 22 */ 23 private static boolean initialized = false; 24 /** 25 * The debug mode. If true, this class writes to the print writer. If false, this class does 26 * nothing when {@link HTLogger#log(String)} is called. 27 */ 28 private static boolean debugMode = false; 29 /** 30 * The {@link PrintStream} to be used. Can be replaced at runtime by calling 31 * {@link HTLogger#setOutput(PrintStream)}. 32 */ 33 private static PrintStream ps = null; 34 35 /** 36 * Lazy initialization of this class. This initialization reads the environment variable and 37 * makes sure that {@link HTLogger#ps} is not null; 38 */ 39 private static void lazyInitialize() { 40 if(!initialized) { 41 debugMode = Boolean.parseBoolean(System.getProperty(HYPHEN_TYPE_DEBUG_MODE)); 42 /* 43 * ps may be not null if the user already set it to be something different from null 44 * before calling the log method. 45 */ 46 if(ps == null) { 47 ps = System.out; 48 } 49 initialized = true; 50 } 51 } 52 53 /** 54 * Logs a message to the {@link PrintStream}, if debug mode is true. 55 * 56 * @param message The message to log 57 */ 58 public static void log(final String message) { 59 lazyInitialize(); 60 61 if(debugMode) { 62 StackTraceElement ste = Thread.currentThread().getStackTrace()[2]; 63 ps.println(String.format("[%s.%s(%d)] %s", ste.getClassName(), ste.getMethodName(), ste.getLineNumber(), message)); 64 } 65 } 66 67 /** 68 * Logs the full stack trace of a throwable. 69 * 70 * @param message The throwable to log 71 */ 72 public static void log(final Throwable t) { 73 lazyInitialize(); 74 if(debugMode) { 75 t.printStackTrace(ps); 76 } 77 } 78 79 /** 80 * Replaces the {@link PrintStream} used by this class to log messages. Does nothing if the 81 * provided argument is null. If you want this class to stop writing log outputs, you should 82 * set the debug mode to false using the method {@link HTLogger#setDebugMode(boolean)}. 83 * 84 * @param newPs The new print stream to be used 85 */ 86 public static void setOutput(final PrintStream newPs) { 87 if(newPs!=null) { 88 ps = newPs; 89 } 90 } 91 92 /** 93 * Allows it for programmatic change of the debug mode. 94 * 95 * @param newDebugMode The new debug mode 96 */ 97 public static void setDebugMode(final boolean newDebugMode) { 98 debugMode = newDebugMode; 99 } 100 101 public static boolean debugMode() { 102 return debugMode; 103 } 104 }