Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.dinopolis.util.debug
Class Debug  view Debug download Debug.java

java.lang.Object
  extended byorg.dinopolis.util.debug.Debug

public class Debug
extends java.lang.Object

Debug helps the programmer to print various debug messages.

If this class is only used by the getInstance 55 method, it can be used as a singleton. So the instance of this class must not known everywhere in the source code. An easier way to use it, is the usage of the class org.dinopolis.util.Debug which is a static frontend for this class.

For a detailed description of the Debug class see the javadoc of org.dinopolis.util.Debug.

Version:
$Id: Debug.java,v 1.2 2003/02/18 08:10:21 cdaller Exp $

Nested Class Summary
(package private)  class Debug.PrintReaderWriter
          this class is a workaround to let Throwable.printStackTrace(PrintWriter) write somewhere, where it can be reread line by line.
(package private)  class Debug.RefreshPropertiesThread
           
 
Field Summary
protected static java.lang.String CR
          carriage return on this system
static boolean DEBUG
          if false, any Debug-calls should be removed from class-code when compiling them and "if (Debug.DEBUG) stands before every call to Debug.print...
static boolean DEBUG_DEBUG
          Only if true, debug messages for the debug class are printed.
(package private) static Debug debug_instance_
          an object of this class (see getInstance 55
private  boolean enabled_
          if false, no debugging messages will be printed
protected  java.lang.String filename_
          in case the output is written to a file, that's the name of it
(package private) static java.lang.String KEYWORD_DELIMITER
          Delimiter for format keywords
protected  long last_message_time_
          the time the last debug message was printed
protected  java.util.TreeSet levels_to_print_
          Set with all levels chosen to display
protected  java.util.Vector message_format_
          Vector holding the objects creating the message string
protected  java.lang.Object message_format_lock_
          lock to synchronize on the message_format_
(package private) static java.io.Writer out_
          where to write the messages to (default is System.err
private  boolean print_all_levels_
           
protected  long property_file_last_modified_
           
protected  Debug.RefreshPropertiesThread refresh_properties_thread_
          Thread used to refresh properties from user_property_file_
(package private) static int REFRESH_PROPERTIES_THREAD_PRIORITY
          priority of the thread that refreshes the properties
private  java.util.TreeMap timers_
          Where to store timer-information
private  java.io.File user_property_file_
           
private  java.lang.Object user_property_file_lock_
           
protected  java.lang.String user_property_file_name_
          name of property file in home directory of user
protected  boolean write_to_file_
          true, if output is written to a file (must be closed after each access)
 
Constructor Summary
Debug()
           
 
Method Summary
 void addLevelsToPrint(java.lang.String levels)
          adds one or more levels, for which messages should be printed (levels can be separated by space, tab, newline or comma).
 void enable(boolean flag)
          Denable/disable debug output.
protected  void executeMainMethod(java.lang.String classname, java.lang.String[] arguments)
          Calls the main method of the given class.
protected  java.lang.String formatMessage(java.lang.String level, java.lang.String msg)
          Formats a message according to the format string set by the method setMessageFormat.
static Debug getInstance()
          Returns an instance of this class (so Debug can be used as a singleton)
protected  long getLastDebugMessageTime()
          Returns the time in milliseconds the last debug message was printed.
 java.lang.String getStackTrace()
          Returns a stacktrace.
 java.lang.String getStackTrace(int from_line, int num_lines)
          returns StackTrace as a String.
 java.lang.String getStackTrace(java.lang.Throwable throwable)
          Returns the stacktrace of throwable.
 java.lang.String getStackTrace(java.lang.Throwable throwable, int from_line, int num_lines)
          returns StackTrace as a String
 java.lang.String getStackTraceLine()
          Returns the line of the source code from which this method was called.
 java.lang.String getTimer(java.lang.String name)
          Returns the string representation of the named timer, but does not stop it.
 java.io.Writer getWriter()
          Returns the Writer, where the debug messages are written to.
 boolean isEnabled()
          Returns true, if debug output is enabled.
 boolean isEnabled(java.lang.String level)
          Return true, if debug output is enabled for the given level.
protected  boolean isLevelEnabled(java.lang.String level)
          Checks, if messages for the given debug level are printed.
 void loadProperties()
          Loads the property file.
static void main(java.lang.String[] args)
          The main method can be used to set Debug options and then start a given application.
static java.lang.String objectToString(java.lang.Object obj)
          Returns a String representation of the object, this method also handles primitive arrays and object arrays.
 void print(java.lang.Object obj)
          Prints an object (its string-representation).
 void print(java.lang.String level, java.lang.Object obj)
          Prints an object (its string-representation) using the given debug-level.
 void printAllLevels(boolean print_all)
          Should all debug messages be printed, ignoring the level?
 void println(java.lang.Object obj)
          Prints an object (its string-representation).
 void println(java.lang.String level, java.lang.Object obj)
          Prints an object (its string-representation) using the given debug-level.
protected  void printMsg(java.lang.String level, java.lang.String msg)
          Prints a message if the given level is allowed to be printed or if the level is an empty string.
protected  void printRawMsg(java.lang.String msg)
          Prints a message to the specified writer (default is System.err).
 void removeAllLevelsToPrint()
          removes all levels, for which messages should be printed
 void removeLevelsToPrint(java.lang.String levels)
          removes one or more levels, for which messages should be printed (levels can be separated by space, tab, newline or comma).
 void setMessageFormat(java.lang.String format_string)
          Set the format of the debug messages.
 void setPropertyFile(java.lang.String filename)
          Sets the filename from where the debug properties are read.
 void setWriter(java.io.Writer out)
          Sets the writer the debug messages are written to.
 void setWriterToFile(java.lang.String filename)
          Writes all messages to a file.
 void startDebugApplication(java.lang.String[] arguments)
          Parses the given arguments, filters out the debug-util specific ones, applies these (e.g.
 void startRefreshPropertiesThread(long refresh_time)
          The Debug Util class is able to reread its property-file from an * independent thread, so any changes in the file (e.g.
 void startTimer(java.lang.String name)
          Creates a new Timer and names it.
 void stopRefreshPropertiesThread()
          The thread that rereads the debug property file is stopped, so any changes in the file are ignored.
 java.lang.String stopTimer(java.lang.String name)
          Stops the named timer and returns the string representation of it.
protected static java.lang.String[] tokenize(java.lang.String str)
          Makes an array of strings out of a string (the tokens can be separated by tab, space, newline or comma)
static void waitEnterPressed()
          Stops the program and waits for a 'enter' on System.in.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

debug_instance_

static Debug debug_instance_
an object of this class (see getInstance 55


out_

static java.io.Writer out_
where to write the messages to (default is System.err


levels_to_print_

protected java.util.TreeSet levels_to_print_
Set with all levels chosen to display


enabled_

private boolean enabled_
if false, no debugging messages will be printed


print_all_levels_

private boolean print_all_levels_

last_message_time_

protected long last_message_time_
the time the last debug message was printed


timers_

private java.util.TreeMap timers_
Where to store timer-information


user_property_file_

private java.io.File user_property_file_

user_property_file_lock_

private java.lang.Object user_property_file_lock_

user_property_file_name_

protected java.lang.String user_property_file_name_
name of property file in home directory of user


property_file_last_modified_

protected long property_file_last_modified_

refresh_properties_thread_

protected Debug.RefreshPropertiesThread refresh_properties_thread_
Thread used to refresh properties from user_property_file_


REFRESH_PROPERTIES_THREAD_PRIORITY

static final int REFRESH_PROPERTIES_THREAD_PRIORITY
priority of the thread that refreshes the properties

See Also:
Constant Field Values

KEYWORD_DELIMITER

static final java.lang.String KEYWORD_DELIMITER
Delimiter for format keywords

See Also:
Constant Field Values

message_format_

protected java.util.Vector message_format_
Vector holding the objects creating the message string


message_format_lock_

protected java.lang.Object message_format_lock_
lock to synchronize on the message_format_


write_to_file_

protected boolean write_to_file_
true, if output is written to a file (must be closed after each access)


filename_

protected java.lang.String filename_
in case the output is written to a file, that's the name of it


CR

protected static final java.lang.String CR
carriage return on this system


DEBUG

public static final boolean DEBUG
if false, any Debug-calls should be removed from class-code when compiling them and "if (Debug.DEBUG) stands before every call to Debug.print... This variable should not be used for temporarily switching on/off debugging!

See Also:
Constant Field Values

DEBUG_DEBUG

public static final boolean DEBUG_DEBUG
Only if true, debug messages for the debug class are printed. Additionaly its debug levels must be enabled (as normally). This is not done using 'normal' debug levels, as users normally do not want to see these messages, even if 'printAllLevels' is set!

See Also:
Constant Field Values
Constructor Detail

Debug

public Debug()
Method Detail

loadProperties

public void loadProperties()
Loads the property file.


setMessageFormat

public void setMessageFormat(java.lang.String format_string)
Set the format of the debug messages. Key words are delimited by '%' and are replaced at run-time. Valid key words are: %date% - prints actual date //%count% - prints the debug message counter %milliseconds% - prints the actual milliseconds %diffmilliseconds% - prints the time difference since the last debug message was printed (in milliseconds) %level% - prints the levelname of the debugmessage %stacktrace% - prints a stacktrace to ever msg %stacktraceline% - prints last line in stacktrace (caller) %thread% - prints the string rep of the thread of the caller %threadgroup% - prints the name of the threadgroup of the caller %threadname% - prints the name of the thread of the caller %message% - prints the debug message itself You can also insert '\n' (and others) e.g. to force a new line or '\t' to divide the output in columns. The key words are replaced by their value, the rest is left unchanged. Example:
"DEBUG L=%level%: %message%" prints "DEBUG L=defaultlevel: debug message"


printAllLevels

public void printAllLevels(boolean print_all)
Should all debug messages be printed, ignoring the level?


addLevelsToPrint

public void addLevelsToPrint(java.lang.String levels)
adds one or more levels, for which messages should be printed (levels can be separated by space, tab, newline or comma).


removeLevelsToPrint

public void removeLevelsToPrint(java.lang.String levels)
removes one or more levels, for which messages should be printed (levels can be separated by space, tab, newline or comma).


removeAllLevelsToPrint

public void removeAllLevelsToPrint()
removes all levels, for which messages should be printed


startTimer

public void startTimer(java.lang.String name)
Creates a new Timer and names it. The timer starts at zero.


stopTimer

public java.lang.String stopTimer(java.lang.String name)
Stops the named timer and returns the string representation of it.


getTimer

public java.lang.String getTimer(java.lang.String name)
Returns the string representation of the named timer, but does not stop it.


enable

public void enable(boolean flag)
Denable/disable debug output.


isEnabled

public boolean isEnabled()
Returns true, if debug output is enabled.


isEnabled

public boolean isEnabled(java.lang.String level)
Return true, if debug output is enabled for the given level. If debug output is disabled in general, this method returns false.


isLevelEnabled

protected boolean isLevelEnabled(java.lang.String level)
Checks, if messages for the given debug level are printed.


print

public void print(java.lang.Object obj)
Prints an object (its string-representation). There is no newline added to the debug message.


print

public void print(java.lang.String level,
                  java.lang.Object obj)
Prints an object (its string-representation) using the given debug-level. There is no newline added to the debug message.


println

public void println(java.lang.Object obj)
Prints an object (its string-representation). There is a newline added to the debug message.


println

public void println(java.lang.String level,
                    java.lang.Object obj)
Prints an object (its string-representation) using the given debug-level. There is a newline added to the debug message.


getStackTrace

public java.lang.String getStackTrace()
Returns a stacktrace. It returned does not include the trace inside the Debug class.


getStackTrace

public java.lang.String getStackTrace(java.lang.Throwable throwable)
Returns the stacktrace of throwable. It returned does not include the trace inside the Debug class.


getStackTraceLine

public java.lang.String getStackTraceLine()
Returns the line of the source code from which this method was called.


getStackTrace

public java.lang.String getStackTrace(int from_line,
                                      int num_lines)
returns StackTrace as a String. For internal use.


getStackTrace

public java.lang.String getStackTrace(java.lang.Throwable throwable,
                                      int from_line,
                                      int num_lines)
returns StackTrace as a String


objectToString

public static java.lang.String objectToString(java.lang.Object obj)
Returns a String representation of the object, this method also handles primitive arrays and object arrays.


waitEnterPressed

public static void waitEnterPressed()
Stops the program and waits for a 'enter' on System.in. Actually, it waits for any character, but System.in is flushed only on 'enter', so any characters typed before might stay in the keyboard buffer.


tokenize

protected static java.lang.String[] tokenize(java.lang.String str)
Makes an array of strings out of a string (the tokens can be separated by tab, space, newline or comma)


startRefreshPropertiesThread

public void startRefreshPropertiesThread(long refresh_time)
The Debug Util class is able to reread its property-file from an * independent thread, so any changes in the file (e.g. additional * debug-levels set) are reflected in the behaviour of the Debug Util class.


stopRefreshPropertiesThread

public void stopRefreshPropertiesThread()
The thread that rereads the debug property file is stopped, so any changes in the file are ignored.


setWriterToFile

public void setWriterToFile(java.lang.String filename)
Writes all messages to a file.


setPropertyFile

public void setPropertyFile(java.lang.String filename)
Sets the filename from where the debug properties are read. If the file does not exist, the setting is ignored!


setWriter

public void setWriter(java.io.Writer out)
Sets the writer the debug messages are written to. The default is System.err.


getWriter

public java.io.Writer getWriter()
Returns the Writer, where the debug messages are written to.


getInstance

public static Debug getInstance()
Returns an instance of this class (so Debug can be used as a singleton)


startDebugApplication

public void startDebugApplication(java.lang.String[] arguments)
Parses the given arguments, filters out the debug-util specific ones, applies these (e.g. sets the debug levels, ...) and starts the given applications. The first non-debug-util argument is taken as the classname to be started. The rest of the arguments is handed over to the application.


executeMainMethod

protected void executeMainMethod(java.lang.String classname,
                                 java.lang.String[] arguments)
Calls the main method of the given class.


main

public static void main(java.lang.String[] args)
The main method can be used to set Debug options and then start a given application. After execution of the application the thread that rereads the properties is stopped.

Valid options are:

  • --debuglevels level1,level2: sets one or more debug levels.
  • --debugmessage "messageformat": sets the format of the debug messages. Please see method setMessageFormat for details.
  • --debugfilename debugfile.log: sets the name of the file the debug messages are written to.
  • --debugrefresh milliseconds: sets the refresh time for the thread that rereads the properties-file.
  • --debugpropertiesfile filename: sets the name of the properties-file.

One(!) argument must hold the classname to start and the arguments that should be handed to the main method of the class. To ensure that it is only one argument, put the whole command line in quotes.

E.g. javac org.dinopolis.util.debug.Debug --debuglevels test-level --debugrefresh 5000 --debugfilename debug.log "org.testpackage.testclass argument1 argument2 'argument with space' --option1"


printRawMsg

protected void printRawMsg(java.lang.String msg)
Prints a message to the specified writer (default is System.err). In case of a file, it is closed immediately, so no information is lost in case of a program abortion.


printMsg

protected void printMsg(java.lang.String level,
                        java.lang.String msg)
Prints a message if the given level is allowed to be printed or if the level is an empty string.


formatMessage

protected java.lang.String formatMessage(java.lang.String level,
                                         java.lang.String msg)
Formats a message according to the format string set by the method setMessageFormat.


getLastDebugMessageTime

protected long getLastDebugMessageTime()
Returns the time in milliseconds the last debug message was printed.