package org.gametrack.util; import java.io.PrintStream; /** *
Title: Log class
*Description: A nice throw-in Log class, with logging functions including std and err stream rerouting
*Copyright: Copyright (c) 2002
* @author Brian Cairns * @version 0.1 */ // logging functions public class Log { public static final PrintStream SYS_OUT = System.out; public static PrintStream SYS_ERR = System.err; static int DEBUG_TAB = 3; // how many spaces to indent per priority level of message static char NORMAL_DELIM = ':'; // programmatically called Log output will have this static char REDIR_DELIM = '>'; // redirected out and err streams will have this static String ERROR_PREFIX = "[ERROR]"; static String DEBUG_PREFIX = "[DEBUG]"; /** * Determines what debug output to display. * 0 = none at all * -1 = ALL debug output * any other # = the highest level that will be outputted */ public static int debugLevel = -1; /** * Logs the message to standard output. Use this method to log stuff that * the application should ALWAYS log, no matter what. * @param message the message */ public static void log( String message ) { SYS_OUT.println( Formatting.timestamp() + NORMAL_DELIM + " " + message ); } /** * Logs with print() instead of println(), allowing continuation of the line, * usually with logDirect() * @param message */ public static void logCont( String message ) { SYS_OUT.print( Formatting.timestamp() + NORMAL_DELIM + " " + message ); } /** * Write directly to system out * @param output */ public static void direct( String output ) { SYS_OUT.print( output ); } /** * Writes the message to standard error. Use this message to log any error * conditions. * @param message */ public static void err( String message ) { err( NORMAL_DELIM, message ); } /** * Conditionally logs the message to standard output. Use this method for * debugging statements you want to be able to turn on and off. * @param message the message to display * @param priority the priority of this debug statement (priority 1 is highest) */ public static void debug( int priority, String message ) { debug( NORMAL_DELIM, priority, message ); } /** * Convenience function.. debug at level 1 (highest priority) * Use this for quick and dirty debug output... add a priority number if * you're leaving the statement in though! * @param message the message */ public static void debug( String message ) { debug( 1, message ); } /** * Set the debug level for the log (the higher the number, the more output) * @param level the debug level */ public static void setDebugLevel( int level ) { debugLevel = level; } /** * Instruct Log class to intercept System.out.println and treat as a debug statement */ public static void interceptSystemOut() { System.setOut( new LoggingPrintStream( false ) ); } /** * Instruct Log class to intercept System.err.println and treat as an err statement */ public static void interceptSystemErr() { System.setErr( new LoggingPrintStream( true ) ); } /** * Mix err in with standard output */ public static void mixErr() { SYS_ERR = SYS_OUT; } // private ///////////////////////////////////////////////////////////////// private static void err( char delim, String message ) { SYS_ERR.println( Formatting.timestamp() + delim + " " + ERROR_PREFIX + " " + message ); } private static void debug( char delim, int priority, String message ) { if ( debugLevel == -1 || priority <= debugLevel ) { SYS_OUT.println( Formatting.timestamp() + delim + " " + Formatting.repeat( " ", priority * DEBUG_TAB ) + DEBUG_PREFIX + " " + message ); } } // inner class ///////////////////////////////////////////////////////////// private static class LoggingPrintStream extends PrintStream { boolean bError; public LoggingPrintStream( boolean error ) { super( error ? System.err : System.out, true ); bError = error; } public void println( String s ) { if ( bError ) { err( REDIR_DELIM, s ); } else { debug( REDIR_DELIM, 0, s ); } } } }