Article View: borland.public.jbuilder.java.language
Article #17935FREE UTILITY :-)
From: Edoardo Comar
Date: Thu, 24 Sep 1998 00:00
Date: Thu, 24 Sep 1998 00:00
190 lines
4995 bytes
4995 bytes
Do you love printing debug messages on the console ?
Do you love test message passing just by filling a method with a simple
print on the console ?
If you do,
you might like try these silly class of mine: call
TDebug.here()
(or its variants) and you'll get on the console ...
package.classname.methodname()
bye!
Edo.
--
ing. Edoardo Comar
<comar@tecnest.it>
Tecnest S.r.l
via Tullio, 11/13 - 33100 Udine Italy
ph. +39 432 511550
fax +39 432 508725
--------
<e.comar@go.nettuno.it> (home email)
PGP RSA key on keyservers http://wwwkeys.pgp.net
key ID 29365395 created 1994/11/03
fingerprint = 5E 10 E1 46 22 35 84 3D 3F 57 E0 35 BE 5C 1E A7
======================================================================
package edo.util;
import java.io.*;
import java.util.*;
/**
* Classe di utilita' da utilizzare per concentrare la
* stampa su console di tutti i messaggi di debugging
* in modo da poterli poi disabilitare in una sola volta!
*/
public class TDebug {
private static PrintStream stream = System.err;
private TDebug() {}
/**
* il metodo da usare invece di System.out/System.err.println()
*/
public static void println(Object sourceObject, String aString) {
println(sourceObject.getClass().getName() + " : " + aString);
}
public static void println(Class sourceClass, String aString) {
println(sourceClass.getName() + " : " + aString);
}
/**
* il metodo da usare invece di System.out/System.err.println()
*/
public static void println(String aString) {
System.err.println(aString);
}
/**
* property debugPrintStream
*/
public static PrintStream getDebugPrintStream() {
return stream;
}
public static void setDebugPrintStream(PrintStream aStream) {
try {
stream.close();
} catch (Exception anExc) {
anExc.printStackTrace();
}
stream = aStream;
}
/**
* imposta debugPrintStream alla CONSOLE
* anche se System.err / System.out sono rediretti !!!
*/
public static void forceConsoleOutput()
{
FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err);
setDebugPrintStream(new PrintStream(new BufferedOutputStream(fdErr, 128),
true));
}
/**
* stampa nome della classe e nome del metodo in cui
* viene eseguita la chiamata TDebug.here() .
*/
public static void here()
{
here("");
}
public static void here(String aString)
{
String _stackTraceRow = StackTrace2String.getOneRowFrom(new
Throwable(),4);
TDebug.println(_stackTraceRow.substring(3) + " " + aString);
}
/**
* utilizzabile per segnalare override necessario
*/
public static void todo()
{
here("TODO YET!");
}
public static void override()
{
here("OVERRIDE NEEDED");
}
}
==================================================
package edo.util;
import java.io.*;
import java.util.*;
public class StackTrace2String
{
/**
* ritorna lo stacktrace come stringa
*/
public static String getAllFrom(Throwable aThrowable)
{
CharArrayWriter os = new CharArrayWriter();
PrintWriter ps = new PrintWriter(os,true);
aThrowable.printStackTrace(ps);
return os.toString();
}
/**
* ritorna lo stacktrace come stringa
* limitatamente alla riga rowIndex
*/
public static String getOneRowFrom(Throwable aThrowable, int rowIndex)
{
String _stackTrace = StackTrace2String.getAllFrom(aThrowable);
StringTokenizer _st = new StringTokenizer(_stackTrace, "\n\r\t", false);
String _rowOfStackTrace ="";
for(int i=0; i<rowIndex && _st.hasMoreTokens(); i++)
{
_rowOfStackTrace = _st.nextToken();
}
return _rowOfStackTrace;
}
/**
* ritorna lo stacktrace come stringa
* limitatamente alla righe da startingRowIndex in poi
*/
public static String getFromRow(Throwable aThrowable, int
startingRowIndex)
{
String _stackTrace = StackTrace2String.getAllFrom(aThrowable);
StringTokenizer _st = new StringTokenizer(_stackTrace, "\n\r\t", false);
StringBuffer _reducedStackTrace = new StringBuffer();
for(int i=0; _st.hasMoreTokens(); i++)
{
String _aRow = _st.nextToken();
if(i>=startingRowIndex)
{
_reducedStackTrace.append((i==startingRowIndex) ? "\r\t" :
"\n\r\t" );
_reducedStackTrace.append(_aRow);
}
}
return _reducedStackTrace.toString();
}
}
Technical Details
Message-ID:
<6ud0p2$2tt17@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,9e26e9b431dd4404,start X-Google-Attributes: gidf74b3,public From: "Edoardo Comar" <comar@tecnest.it> Subject: FREE UTILITY :-) Date: 1998/09/24 Message-ID: <6ud0p2$2tt17@forums.borland.com> X-Deja-AN: 394308853 Organization: Another Netscape Collabra Server User X-Mimeole: Produced By Microsoft MimeOLE V4.72.2106.4 Newsgroups: borland.public.jbuilder.java.language