com.isti.util
Class ThreadLogger

java.lang.Object
  extended by com.isti.util.ThreadLogger

public class ThreadLogger
extends java.lang.Object

Class ThreadLogger defines a debugging utility that generates periodic log messages showing information about the current threads running in a program. A new thread is created to generate the log messages. The default behavior is that log messages are only generated when a change in the threads is detected. This behavior may be changed via the 'setOutputOnChangeFlag()' method.


Nested Class Summary
protected  class ThreadLogger.LoggerRunningThread
          Class LoggerRunningThread defines a separate thread used to build and output the log file entries.
 
Field Summary
protected  boolean closeOnTerminateFlag
           
protected  java.lang.String errorMessageString
           
protected  int intervalMSecs
           
protected  ThreadLogger.LoggerRunningThread loggerRunningThreadObj
           
protected  int logLevelVal
           
protected  LogFile logObj
           
protected static java.lang.String newlineStr
           
protected  boolean outputOnChangeFlag
           
protected  java.lang.ThreadGroup rootThreadGroupObj
           
protected  java.lang.StringBuffer stringBufferObj
           
protected  int totalThreadCount
           
 
Constructor Summary
ThreadLogger()
          Creates a thread logger object with no log output file.
ThreadLogger(LogFile logObj, int intervalMSecs)
          Creates a thread logger object and starts a new log-message-generation thread.
ThreadLogger(java.lang.String logFileName, int intervalMSecs)
          Creates a thread logger object and starts a new log-message-generation thread.
ThreadLogger(java.lang.String logFileName, int intervalMSecs, boolean useDateInFnameFlag)
          Creates a thread logger object and starts a new log-message-generation thread.
ThreadLogger(java.lang.String logFileName, int intervalMSecs, int maxLogFileAgeDays)
          Creates a thread logger object and starts a new log-message-generation thread.
ThreadLogger(java.lang.String logFileName, int intervalMSecs, int maxLogFileAgeDays, int logFileSwitchIntervalDays)
          Creates a thread logger object and starts a new log-message-generation thread.
 
Method Summary
 void clearErrorMessageString()
          Clears any current error message.
protected  boolean fetchThreadsInfo(java.lang.ThreadGroup threadGroupObj, java.lang.String indentStr)
          Enters information for all of the threads in the given ThreadGroup and all of its subgroups into the StringBuffer object.
 java.lang.String getErrorMessageString()
          Returns the current error message (if any).
static java.lang.ThreadGroup getRootThreadGroup()
          Returns the root ThreadGroup object.
static java.lang.String getThreadGroupInfoStr(java.lang.ThreadGroup threadGroupObj)
          Returns an informational string about the given thread group.
static java.lang.String getThreadInfoStr(java.lang.Thread threadObj)
          Returns an informational string about the given thread.
 java.lang.String getThreadsInfoStr()
          Returns an information string for all of the current threads.
 java.lang.String getThreadsInfoStr(boolean outChangeFlag)
          Returns an information string for all of the current threads.
 boolean logThreadsInfo()
          Logs information for all of the current threads.
 void setCloseLogOnTerminateFlag(boolean flgVal)
          Sets whether or not the log file is closed when the logging thread terminates.
 void setConsoleOutputFlag(boolean flgVal)
          Sets the flag to enable console output of log messages.
 void setLogLevelVal(int levelVal)
          Sets the log-level value to be used.
 void setOutputOnChangeFlag(boolean flgVal)
          Sets the flag that determines if threads information is logged only when a change is detected or all the time regardless.
protected  boolean setupRootThreadGroupObj()
          Sets up the 'rootThreadGroupObj' handle.
protected  void startLoggerThread(int intervalMSecs)
          Starts the thread-logging thread.
 void terminate()
          Terminates the logging thread created by this ThreadLogger.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logObj

protected final LogFile logObj

intervalMSecs

protected int intervalMSecs

stringBufferObj

protected java.lang.StringBuffer stringBufferObj

outputOnChangeFlag

protected boolean outputOnChangeFlag

rootThreadGroupObj

protected java.lang.ThreadGroup rootThreadGroupObj

totalThreadCount

protected int totalThreadCount

errorMessageString

protected java.lang.String errorMessageString

loggerRunningThreadObj

protected ThreadLogger.LoggerRunningThread loggerRunningThreadObj

newlineStr

protected static final java.lang.String newlineStr

logLevelVal

protected int logLevelVal

closeOnTerminateFlag

protected boolean closeOnTerminateFlag
Constructor Detail

ThreadLogger

public ThreadLogger(LogFile logObj,
                    int intervalMSecs)
Creates a thread logger object and starts a new log-message-generation thread.

Parameters:
logObj - the LogFile object to use for output.
intervalMSecs - the number of milliseconds between log outputs.

ThreadLogger

public ThreadLogger(java.lang.String logFileName,
                    int intervalMSecs)
Creates a thread logger object and starts a new log-message-generation thread.

Parameters:
logFileName - the name of the log file to be created and used for output.
intervalMSecs - the number of milliseconds between log outputs.

ThreadLogger

public ThreadLogger(java.lang.String logFileName,
                    int intervalMSecs,
                    boolean useDateInFnameFlag)
Creates a thread logger object and starts a new log-message-generation thread.

Parameters:
logFileName - the name of the log file to be created and used for output.
intervalMSecs - the number of milliseconds between log outputs.
useDateInFnameFlag - true to use the date in the file name (and create a new log file every time the date changes).

ThreadLogger

public ThreadLogger(java.lang.String logFileName,
                    int intervalMSecs,
                    int maxLogFileAgeDays)
Creates a thread logger object and starts a new log-message-generation thread. The date is used in the file name and a new log file is created every time the date changes).

Parameters:
logFileName - the name of the log file to be created and used for output.
intervalMSecs - the number of milliseconds between log outputs.
maxLogFileAgeDays - the maximum log file age, in days.

ThreadLogger

public ThreadLogger(java.lang.String logFileName,
                    int intervalMSecs,
                    int maxLogFileAgeDays,
                    int logFileSwitchIntervalDays)
Creates a thread logger object and starts a new log-message-generation thread. The date is used in the file name and a new log file is created every time the date changes).

Parameters:
logFileName - the name of the log file to be created and used for output.
intervalMSecs - the number of milliseconds between log outputs.
maxLogFileAgeDays - the maximum log file age, in days.
logFileSwitchIntervalDays - the minimum number of days between log file switches via date changes.

ThreadLogger

public ThreadLogger()
Creates a thread logger object with no log output file. In this configuration, no log-message-generation thread is created, and thread information may be requested on demand via the 'getThreadsInfoStr()' methods.

Method Detail

startLoggerThread

protected void startLoggerThread(int intervalMSecs)
Starts the thread-logging thread.

Parameters:
intervalMSecs - the number of milliseconds between log outputs.

setupRootThreadGroupObj

protected boolean setupRootThreadGroupObj()
Sets up the 'rootThreadGroupObj' handle.

Returns:
true if successful, false if an error occurs (in which case 'errorMessageString' will be set to the error message text).

setConsoleOutputFlag

public void setConsoleOutputFlag(boolean flgVal)
Sets the flag to enable console output of log messages.

Parameters:
flgVal - true to enable console output, false to disable.

setOutputOnChangeFlag

public void setOutputOnChangeFlag(boolean flgVal)
Sets the flag that determines if threads information is logged only when a change is detected or all the time regardless.

Parameters:
flgVal - true to log only when a change is detected (the default), false to send a log message at each interval regardless.

setLogLevelVal

public void setLogLevelVal(int levelVal)
Sets the log-level value to be used. The default value is 'LogFile.NO_LEVEL'.

Parameters:
levelVal - the log-level value to be used.

setCloseLogOnTerminateFlag

public void setCloseLogOnTerminateFlag(boolean flgVal)
Sets whether or not the log file is closed when the logging thread terminates.

Parameters:
flgVal - true to close the log file on terminate, false to not.

terminate

public void terminate()
Terminates the logging thread created by this ThreadLogger.


getThreadsInfoStr

public java.lang.String getThreadsInfoStr(boolean outChangeFlag)
Returns an information string for all of the current threads.

Parameters:
outChangeFlag - if false then the information string will always be returned; if true then the information string will only be returned when a change in the threads is detected, otherwise 'null' will be returned. If an error occurs while fetching the thread information then 'null' will be returned. The error status may be determined via the 'getErrorMessageString()' method.
Returns:
An information string, or 'null'.

getThreadsInfoStr

public java.lang.String getThreadsInfoStr()
Returns an information string for all of the current threads. If an error occurs while fetching the thread information then 'null' will be returned. The error status may be determined via the 'getErrorMessageString()' method.

Returns:
An information string, or 'null'.

logThreadsInfo

public boolean logThreadsInfo()
Logs information for all of the current threads.

Returns:
true if successful, false if error (in which case a error message is logged).

fetchThreadsInfo

protected boolean fetchThreadsInfo(java.lang.ThreadGroup threadGroupObj,
                                   java.lang.String indentStr)
Enters information for all of the threads in the given ThreadGroup and all of its subgroups into the StringBuffer object.

Parameters:
threadGroupObj - the ThreadGroup to use.
indentStr - the indentation string to place in front of each line of log output.
Returns:
true if successful, false if error (in which case a error message is logged).

getErrorMessageString

public java.lang.String getErrorMessageString()
Returns the current error message (if any).

Returns:
The current error message, or null if no errors have occurred.

clearErrorMessageString

public void clearErrorMessageString()
Clears any current error message.


getRootThreadGroup

public static java.lang.ThreadGroup getRootThreadGroup()
Returns the root ThreadGroup object.

Returns:
The root ThreadGroup object.

getThreadInfoStr

public static java.lang.String getThreadInfoStr(java.lang.Thread threadObj)
Returns an informational string about the given thread.

Parameters:
threadObj - the thread to use.
Returns:
An informational string.

getThreadGroupInfoStr

public static java.lang.String getThreadGroupInfoStr(java.lang.ThreadGroup threadGroupObj)
Returns an informational string about the given thread group.

Parameters:
threadGroupObj - the thread group to use.
Returns:
An informational string.