/* * The Broad Institute * SOFTWARE COPYRIGHT NOTICE AGREEMENT * This software and its documentation are copyright 2009 by the * Broad Institute/Massachusetts Institute of Technology. All rights are reserved. * * This software is supplied without any warranty or guaranteed support whatsoever. Neither * the Broad Institute nor MIT can be responsible for its use, misuse, or functionality. */ package edu.mit.broad.picard.util; import java.io.PrintStream; import java.util.Arrays; /** *

A wafer thin wrapper around System.out that uses var-args to make it * much more efficient to call the logging methods in without having to * surround every call site with calls to Log.isXXXEnabled(). All the methods on this * class take a variable length list of arguments and, only if logging is enabled for * the level and channel being logged to, will those arguments be toString()'d and * appended together.

* * @author Tim Fennell */ public final class Log { /** Enumeration for setting log levels. */ public static enum LogLevel { ERROR, WARNING, INFO, DEBUG } private static LogLevel globalLogLevel = LogLevel.DEBUG; private final Class clazz; private final String className; private final LogLevel level = globalLogLevel; private final PrintStream out = System.out; /** * Private constructor */ private Log(final Class clazz) { this.clazz = clazz; this.className = clazz.getSimpleName(); } /** * Get a Log instance to perform logging within the Class specified. Returns an instance * of this class which wraps an instance of the commons logging Log class. * @param clazz the Class which is going to be doing the logging * @return a Log instance with which to log */ public static Log getInstance(final Class clazz) { return new Log(clazz); } public static void setGlobalLogLevel(final LogLevel logLevel) { globalLogLevel = logLevel; } /** Returns true if the specified log level is enabled otherwise false. */ public final boolean isEnabled(final LogLevel level) { return level.ordinal() <= this.level.ordinal(); } /** * Private method that does the actual printing of messages to a PrintWriter. Outputs the log level, * class name and parts followed by the stack trace if a throwable is provided. * * @param level the Log level being logged at * @param throwable a Throwable if one is available otherwise null * @param parts the parts of the message to be concatenated */ private final void emit(final LogLevel level, final Throwable throwable, final Object... parts) { if (isEnabled(level)) { this.out.print(level.name()); this.out.print('\t'); this.out.print(this.className); this.out.print('\t'); for (final Object part : parts) { if (part != null && part.getClass().isArray()) { final Class component = part.getClass().getComponentType(); if (component.equals(Boolean.TYPE)) this.out.print(Arrays.toString( (boolean[]) part)); else if (component.equals(Byte.TYPE)) this.out.print(Arrays.toString( (byte[]) part)); else if (component.equals(Character.TYPE)) this.out.print(Arrays.toString( (char[]) part)); else if (component.equals(Double.TYPE)) this.out.print(Arrays.toString( (double[]) part)); else if (component.equals(Float.TYPE)) this.out.print(Arrays.toString( (float[]) part)); else if (component.equals(Integer.TYPE)) this.out.print(Arrays.toString( (int[]) part)); else if (component.equals(Long.TYPE)) this.out.print(Arrays.toString( (long[]) part)); else if (component.equals(Short.TYPE)) this.out.print(Arrays.toString( (short[]) part)); else this.out.print(Arrays.toString( (Object[]) part)); } else { this.out.print(part); } } this.out.println(); // Print out the exception if there is one if (throwable != null) { throwable.printStackTrace(this.out); } } } /** * Logs a Throwable and optional message parts at level error. * @param throwable an instance of Throwable that should be logged with stack trace * @param messageParts zero or more objects which should be combined, by calling toString() * to form the log message. */ public final void error(final Throwable throwable, final Object... messageParts) { emit(LogLevel.ERROR, throwable, messageParts); } /** * Logs a Throwable and optional message parts at level warn. * @param throwable an instance of Throwable that should be logged with stack trace * @param messageParts zero or more objects which should be combined, by calling toString() * to form the log message. */ public final void warn(final Throwable throwable, final Object... messageParts) { emit(LogLevel.WARNING, throwable, messageParts); } /** * Logs a Throwable and optional message parts at level info. * @param throwable an instance of Throwable that should be logged with stack trace * @param messageParts zero or more objects which should be combined, by calling toString() * to form the log message. */ public final void info(final Throwable throwable, final Object... messageParts) { emit(LogLevel.INFO, throwable, messageParts); } /** * Logs a Throwable and optional message parts at level debug. * @param throwable an instance of Throwable that should be logged with stack trace * @param messageParts zero or more objects which should be combined, by calling toString() * to form the log message. */ public final void debug(final Throwable throwable, final Object... messageParts) { emit(LogLevel.DEBUG, throwable, messageParts); } // Similar methods, but without Throwables, follow /** * Logs one or more message parts at level error. * @param messageParts one or more objects which should be combined, by calling toString() * to form the log message. */ public final void error(final Object... messageParts) { emit(LogLevel.ERROR, null, messageParts); } /** * Logs one or more message parts at level warn. * @param messageParts one or more objects which should be combined, by calling toString() * to form the log message. */ public final void warn(final Object... messageParts) { emit(LogLevel.WARNING, null, messageParts); } /** * Logs one or more message parts at level info. * @param messageParts one or more objects which should be combined, by calling toString() * to form the log message. */ public final void info(final Object... messageParts) { emit(LogLevel.INFO, null, messageParts); } /** * Logs one or more message parts at level debug. * @param messageParts one or more objects which should be combined, by calling toString() * to form the log message. */ public final void debug(final Object... messageParts) { emit(LogLevel.DEBUG, null, messageParts); } }