Introduction
I have written articles on dynamically generating trace files in multi-threaded C++ and VB (script) applications. This is an attempt to do the same for Java applications. Since every function has to belong to a class in Java, my code consists of a single class XYTrace, with three thread-safe static methods.
The first method is SetTraceFilePrefix which determines where the trace file will be created and how it will be named. Without calling this method, the default trace file will be created in the current working directory with name Trace_yyyymmdd_HHMMSSMLS.txt, where yyyymmdd_HHMMSSMLS is a date-time stamp down to milliseconds. If you make the following call anywhere in your Java program,
XYTrace.SetTraceFilePrefix("c:\\winnt\\temp\\MyTrace");
the trace file will be created in the directory c:\winnt\temp and the file name will be MyTrace_yyyymmdd_HHMMSSMLS.txt. Note that, calling this method will close the previous trace file (if there is any) and create a new one at a later time.
The second method is SetTraceLevel. It can be called at run-time to adjust the current trace level. The current trace level is just a non-negative integer, where 0 means no tracing, and the larger the current trace level is, the more info will be written to the trace file.
The third method WriteTrace is where the main work is done. This method takes two arguments, an integer representing the intended trace level and a string representing the text you want to output to the trace file. For example, you can have the following statements in your Java program,
XYTrace.WriteTrace(10, "Hello, world");
XYTrace.WriteTrace(20, "Today is "+new Date().toString());
XYTrace.WriteTrace(30, "I am feeling wild");
If the current trace level is 0, then nothing will be written to the trace file when the code is executed. If the current trace level is 20, then only the first two messages will be written. If the current trace level is 30 or above, then all three messages will be written. Note that each trace message written to the trace file will be prefixed with a string of the form HH:MM:SS_MLS(current_thread_name):, where HH:MM:SS_MLS is a time stamp.
The trace file won't get out of hand even if your program keeps running for days or weeks, because in each new day, the old trace file will be closed and a new one will be used automatically. Here is a simple test program for the XYTrace class.
import java.lang.Thread;
public class TraceTest implements Runnable
{
final public void run()
{
int nCount = 1;
while(true)
{
XYTrace.WriteTrace(10, "Hello, world");
nCount++;
XYTrace.WriteTrace(20, "How are you?");
nCount++;
XYTrace.WriteTrace(30, "This is message "+nCount);
nCount++;
try
{
Thread.currentThread().sleep(5*1000);
}
catch(Exception e)
{
}
}
}
public static void main (String[] args)
{
XYTrace.SetTraceFilePrefix("c:\\temp\\MyTrace");
XYTrace.SetTraceLevel(30);
XYTrace.WriteTrace(30, "This is the main thread.");
for(int i=0;i<3;i++) new Thread(new TraceTest()).start();
while(true)
{
try
{
Thread.currentThread().sleep(20*1000);
XYTrace.SetTraceLevel(10);
Thread.currentThread().sleep(20*1000);
XYTrace.SetTraceLevel(20);
Thread.currentThread().sleep(20*1000);
XYTrace.SetTraceLevel(30);
}
catch(Exception e)
{
}
}
}
}
Thank you for reading my articles. Have a great day!