At 07:52 AM 1/1/02 -0800, you wrote:
>At 10:59 AM 1/1/02 -0500, you wrote:
>>At 05:13 PM 12/31/2001 -0800, you wrote:
>> >
>> >My servlets in Tomcat works just fine, but I was wondering where does the
>>'System.out.println("bla bla...")' goes when the servelet is executed. I
>>would like to use this to debug my servlet.
>> >
>> >thanks,
>> >
>> >Sanjeev
If you want a debugger you can put in easier than using
System.out.println("blah blah ..."), I include one you are welcome to use,
but not sell, with this reply. I use it all the time. Essentially you
just go (where "data" is the variable you want to test): new
Debugger(true).add(data).log(true); If you want to add lots of variables,
new Debugger(true).add(data1).add(data2).add(dataN).log(true); The The
value "true" in log(true) tells the application to save the last run of the
log. Using "false" as in log(false) would overwrite the last run. You can
figure out the rest easily enough. There are a few rather obvious choices
and all data types are overloaded and covered. Enjoy!
- micael
/* tccjava.toolbox.debugger.Debugger: December 9, 2001
*
* Copyright 2001 Swords and Ploughshares, Ltd. All Rights Reserved.
*
* This software is the proprietary information of Swords and Ploughshares, Ltd.
* Use is subject to license terms. */
package tccjava.toolbox.debugger;
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Date;
import java.util.StringTokenizer;
import tccjava.JavaPlatform;
/** This class is for debugging and logging values
* in Java classes and applications -- The class
* will log any values without difficulty -- Further,
* having a log which is time stamped will allow
* the user to make decisions based on looking at
* the history of an application and will allow a
* look at whatever length of values may occur,
* unlike with a DOS window: the application
* overloads add([Whatever]) methods for all data
* types.
* @author Micael Padraig Og mac Grene
* @version 1.2: 21 April 2001
* @since Version 1.2 */
public class Debugger {
private FileWriter writer;
private PrintWriter printer;
private StringBuffer strbuf = new StringBuffer();
private String path;
private boolean showtype;
/** The default constructor, which sets the default path of the debugger
logs. */
public Debugger() {
setPath("C", new String [] { JavaPlatform.version, "jre", "classes",
"tccjava", "bugs" }, "defaultlog", "bug");
}
/** Constructor that informs the application to show the type of variables
* tested dureing debugging.
* @param showtype If set to true, shows the type of variables, e.g. int,
* class, short, etc. The default of the debugger logs is also set. */
public Debugger(boolean showtype) {
this.showtype = showtype;
setPath("C", new String [] { JavaPlatform.version, "jre", "classes",
"tccjava", "bugs" }, "defaultlog", "bug");
}
/** Allows the user to set the path for the debugger logs.
* @param root The root of the path, usually "C" for a PC and ""
* for Linux, Unix.
* @param directories The directories along the path to the file, not
* including the file.
* @param logFileName The name of the log file.
* @param ext The name of the extension of the log file, the default
* is "bug".
* @return The Debugger value. */
public Debugger setPath(String root, String [] directories, String
logFileName, String ext) {
/* Make a buffer to hold values temporarily. */
StringBuffer strbuf = new StringBuffer();
/* Add ":" if the root is a PC root. */
if(root.length() > 0 && Character.isLetter(root.charAt(0))) {
strbuf.append(root + ":");
}
/* Use platform independent File.separator values. */
for (int i = 0; i < directories.length; i++) {
strbuf.append(File.separator + directories[i]);
}
/* Create the directories for the file. */
new File(strbuf.toString()).mkdirs();
/* Add the file name to the directories. */
strbuf.append(File.separator + logFileName);
/* Add the extension to the file name. */
strbuf.append("." + ext);
/* Create the file. */
try {
new File(strbuf.toString()).createNewFile();
} catch (IOException ioe) {
ioe.printStackTrace();
}
/* Set the Debugger class path name of the file. */
path = strbuf.toString();
/* Return the debugger value. */
return this;
}
/** This method logs the values added to the Debugger value.
* @param value If set to true, keeps prior values, else
* overwrites prior results.
* @return The Debugger value. */
public synchronized Debugger log(boolean append) {
try {
/* Create a file writer with the right path and the proper
overwriting
* instructions. */
writer = new FileWriter(path, append);
/* Uses a print writer for the file writer and flushes the
line output. */
printer = new PrintWriter(writer, true);
/* Gives a header to the output, dates the output, then
publishes the
* output. */
printer.println("\n\n\n\nApplication Values: " + new
Date().toString() + "\n\n" + strbuf.toString() + "\n\n");
/* Close the resources. */
printer.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
/* Reset the buffer. */
strbuf = new StringBuffer();
/* Return the debugger value. */
return this;
}
/** This method logs the values added to the Debugger value.
* @param info Provides whatever information the user wants to
* the debugger log.
* @param value If set to true, keeps prior values, else
* overwrites prior results.
* @return The Debugger value. */
public synchronized Debugger log(String info, boolean append) {
try {
/* Create a file writer with the right path and the proper
overwriting
* instructions. */
writer = new FileWriter(path, append);
/* Uses a print writer for the file writer and flushes the
line output. */
printer = new PrintWriter(writer, true);
/* Gives a header to the output, dates the output, then
publishes the
* output. */
printer.println("\n\n\n\nApplication Values: " + new
Date().toString() + "\n\n" + info + ":\n\n" + strbuf.toString() + "\n\n");
/* Close the resources. */
printer.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
/* Reset the buffer. */
strbuf = new StringBuffer();
/* Return the Debugger value. */
return this;
}
/** A method to see what a variable has as an value and what
* the type (if showType is set) and value of the variable is:
* the method is overloaded for all data types.
* @param value The value being tested.
* @return The <code>Debugger</code> value. */
public synchronized Debugger add(Object value) {
if(value == null) {
strbuf.append("The value value is null\n");
return this;
}
if(showtype) {
strbuf.append("Object, " + value.getClass().getName() + "
value is: " + value.toString() + "\n");
return this;
} else {
strbuf.append(value.getClass().getName() + " value is: " +
value.toString() + "\n");
return this;
}
}
/** A method to see what a variable has as an value and what
* the type (if showType is set) and value of the variable is:
* the method is overloaded for all data types.
* @param info Whatever information the user wants to include in the
* log, usually the name of the variable would be included for reference
* purposes.
* @param value The value being tested.
* @return The <code>Debugger</code> value. */
public synchronized Debugger add(String info, Object value) {
if(value == null && showtype) {
strbuf.append("String value is null" + " (Info: " + info + ")
\n");
return this;
} else if(value == null) {
strbuf.append("The String value is null \n");
return this;
} else if(showtype) {
strbuf.append("Object: " + value.getClass().getName() + "
value is: " + value.toString() + " (Info: " + info + ") \n");
return this;
} else {
strbuf.append(value.getClass().getName() + " value is: " +
value.toString() + " \n");
return this;
}
}
/** A method to see what a variable has as an value and what
* the type (if showType is set) and value of the variable is:
* the method is overloaded for all data types.
* @param value The value being tested.
* @return The <code>Debugger</code> value. */
public synchronized Debugger add(String value) {
if(value == null) {
strbuf.append("The String value is null \n");
return this;
} else if(showtype) {
strbuf.append("String value is: " + value + " \n");
return this;
} else {
strbuf.append(value + "\n");
return this;
}
}
/** This method logs the values added to the Debugger value.
* @param info Provides whatever information the user wants to
* the debugger log.
* @param value If set to true, keeps prior values, else
* overwrites prior results.
* @return The Debugger value. */
public synchronized Debugger add(String info, String value) {
if(value == null && showtype) {
strbuf.append("String value is null" + " (Info: " + info + ")
\n");
return this;
} else if(value == null) {
strbuf.append("The String value is null\n");
return this;
} else if(showtype) {
strbuf.append("String value is: " + value + " (Info: " + info
+ ") \n");
return this;
} else {
strbuf.append(info + " value is: " + value + "\n");
return this;
}
}
/** A method to see what a variable has as an value and what
* the type (if showType is set) and value of the variable is:
* the method is overloaded for all data types.
* @param value The value being tested.
* @return The <code>Debugger</code> value. */
public synchronized Debugger add(char value) {
if(showtype) {
strbuf.append("char value is: " + new
Character(value).toString() + "\n");
return this;
} else {
strbuf.append(new Character(value).toString() + "\n");
return this;
}
}
/** This method logs the values added to the Debugger value.
* @param info Provides whatever information the user wants to
* the debugger log.
* @param value If set to true, keeps prior values, else
* overwrites prior results.
* @return The Debugger value. */
public synchronized Debugger add(String info, char value) {
if(showtype) {
strbuf.append("char value is: " + new
Character(value).toString() + " [Info: " + info + "]\n");
return this;
} else {
strbuf.append(info + " value is: " + new
Character(value).toString() + "\n");
return this;
}
}
/** A method to see what a variable has as an value and what
* the type (if showType is set) and value of the variable is:
* the method is overloaded for all data types.
* @param value The value being tested.
* @return The <code>Debugger</code> value. */
public synchronized Debugger add(byte value) {
if(showtype) {
strbuf.append("byte value is: " + Byte.toString(value) + "\n");
return this;
} else {
strbuf.append(Byte.toString(value) + "\n");
return this;
}
}
/** This method logs the values added to the Debugger value.
* @param info Provides whatever information the user wants to
* the debugger log.
* @param value If set to true, keeps prior values, else
* overwrites prior results.
* @return The Debugger value. */
public synchronized Debugger add(String info, byte value) {
if(showtype) {
strbuf.append("byte value is: " + Byte.toString(value) + "
[Info: " + info + "]\n");
return this;
} else {
strbuf.append(info + " value is: " + Byte.toString(value) +
"\n");
return this;
}
}
/** A method to see what a variable has as an value and what
* the type (if showType is set) and value of the variable is:
* the method is overloaded for all data types.
* @param value The value being tested.
* @return The <code>Debugger</code> value. */
public synchronized Debugger add(short value) {
if(showtype) {
strbuf.append("short value is: " + Short.toString(value) +
"\n");
return this;
} else {
strbuf.append(Short.toString(value) + "\n");
return this;
}
}
/** This method logs the values added to the Debugger value.
* @param info Provides whatever information the user wants to
* the debugger log.
* @param value If set to true, keeps prior values, else
* overwrites prior results.
* @return The Debugger value. */
public synchronized Debugger add(String info, short value) {
if(showtype) {
strbuf.append("short value is: " + Short.toString(value) + "
[Info: " + info + "]\n");
return this;
} else {
strbuf.append(info + " value is: " + Short.toString(value) +
"\n");
return this;
}
}
/** A method to see what a variable has as an value and what
* the type (if showType is set) and value of the variable is:
* the method is overloaded for all data types.
* @param value The value being tested.
* @return The <code>Debugger</code> value. */
public synchronized Debugger add(int value) {
if(showtype) {
strbuf.append("int value is: " + Integer.toString(value) +
"\n");
return this;
} else {
strbuf.append(Integer.toString(value) + "\n");
return this;
}
}
/** This method logs the values added to the Debugger value.
* @param info Provides whatever information the user wants to
* the debugger log.
* @param value If set to true, keeps prior values, else
* overwrites prior results.
* @return The Debugger value. */
public synchronized Debugger add(String info, int value) {
if(showtype) {
strbuf.append("int value is: " + Integer.toString(value) + "
[Info: " + info + "]\n");
return this;
} else {
strbuf.append(info + " value is: " + Integer.toString(value) +
"\n");
return this;
}
}
/** A method to see what a variable has as an value and what
* the type (if showType is set) and value of the variable is:
* the method is overloaded for all data types.
* @param value The value being tested.
* @return The <code>Debugger</code> value. */
public synchronized Debugger add(long value) {
if(showtype) {
strbuf.append("long value is: " + Long.toString(value) + "\n");
return this;
} else {
strbuf.append(Long.toString(value) + "\n");
return this;
}
}
/** This method logs the values added to the Debugger value.
* @param info Provides whatever information the user wants to
* the debugger log.
* @param value If set to true, keeps prior values, else
* overwrites prior results.
* @return The Debugger value. */
public synchronized Debugger add(String info, long value) {
if(showtype) {
strbuf.append("long value is: " + Long.toString(value) + "
[Info: " + info + "]\n");
return this;
} else {
strbuf.append(info + " value is: " + Long.toString(value) +
"\n");
return this;
}
}
/** A method to see what a variable has as an value and what
* the type (if showType is set) and value of the variable is:
* the method is overloaded for all data types.
* @param value The value being tested.
* @return The <code>Debugger</code> value. */
public synchronized Debugger add(float value) {
if(showtype) {
strbuf.append("float value is: " + Float.toString(value) +
"\n");
return this;
} else {
strbuf.append(Float.toString(value) + "\n");
return this;
}
}
/** This method logs the values added to the Debugger value.
* @param info Provides whatever information the user wants to
* the debugger log.
* @param value If set to true, keeps prior values, else
* overwrites prior results.
* @return The Debugger value. */
public synchronized Debugger add(String info, float value) {
if(showtype) {
strbuf.append("float value is: " + Float.toString(value) + "
[Info: " + info + "]\n");
return this;
} else {
strbuf.append(info + " value is: " + Float.toString(value) +
"\n");
return this;
}
}
/** A method to see what a variable has as an value and what
* the type (if showType is set) and value of the variable is:
* the method is overloaded for all data types.
* @param value The value being tested.
* @return The <code>Debugger</code> value. */
public synchronized Debugger add(double value) {
if(showtype) {
strbuf.append("double value is: " + Double.toString(value) +
"\n");
return this;
} else {
strbuf.append(Double.toString(value) + "\n");
return this;
}
}
/** This method logs the values added to the Debugger value.
* @param info Provides whatever information the user wants to
* the debugger log.
* @param value If set to true, keeps prior values, else
* overwrites prior results.
* @return The Debugger value. */
public synchronized Debugger add(String info, double value) {
if(showtype) {
strbuf.append("double value is: " + Double.toString(value) + "
[Info: " + info + "]\n");
return this;
} else {
strbuf.append(info + " value is: " + Double.toString(value) +
"\n");
return this;
}
}
/** A method to see what a variable has as an value and what
* the type (if showType is set) and value of the variable is:
* the method is overloaded for all data types.
* @param value The value being tested.
* @return The <code>Debugger</code> value. */
public synchronized Debugger add(boolean value) {
if(showtype) {
strbuf.append("boolean value is: " + new
Boolean(value).toString() + "\n");
return this;
} else {
strbuf.append(new Boolean(value).toString() + "\n");
return this;
}
}
/** This method logs the values added to the Debugger value.
* @param info Provides whatever information the user wants to
* the debugger log.
* @param value If set to true, keeps prior values, else
* overwrites prior results.
* @return The Debugger value. */
public synchronized Debugger add(String info, boolean value) {
if(showtype) {
strbuf.append("boolean value is: " + new
Boolean(value).toString() + " [Info: " + info + "]\n");
return this;
} else {
strbuf.append(info + " value is: " + new
Boolean(value).toString() + "\n");
return this;
}
}
} ///;-)
--
To unsubscribe: <mailto:[EMAIL PROTECTED]>
For additional commands: <mailto:[EMAIL PROTECTED]>
Troubles with the list: <mailto:[EMAIL PROTECTED]>