Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

Debug Print Statements

Thread view: 
Ghost - 10 Jun 2005 15:20 GMT
I am trying to add some debug print statements to my java code.  I
would like the print statement to display the file name and line number
from where the print statement came from.

ex)
System.out.println(fileName + ":" + lineNum + " Debug statement");

Does anyone know of a way to do this?

Thanks in advance.
Andrea Desole - 10 Jun 2005 15:49 GMT
> I am trying to add some debug print statements to my java code.  I
> would like the print statement to display the file name and line number
[quoted text clipped - 4 lines]
>
> Does anyone know of a way to do this?

I think the only way to do it is via a stack trace, either from a
Throwable object or from a Thread object (if you are using JDK 1.5).
From the stack trace elements you can get the information you need.
Daniel Dyer - 10 Jun 2005 16:38 GMT
>> I am trying to add some debug print statements to my java code.  I
>> would like the print statement to display the file name and line number
[quoted text clipped - 6 lines]
> Throwable object or from a Thread object (if you are using JDK 1.5).  
>  From the stack trace elements you can get the information you need.

You can use the static Thread.dumpStack() method, which is available prior  
to 1.5.  But you still get the whole stack trace rather than just the file  
and line number.  If you use a logging framework such as Log4J  
(http://logging.apache.org) you can output this information without the  
stack trace more easily (but this information is expensive to generate and  
is not available if you compiled your code with the debug information  
omitted).

You don't really need the file name, you can just output the class name  
(this.getClass()), which is pretty much equivalent.  The line number is  
not particularly helpful unless you have multiple debug statements in the  
same file that print out the same message.

Dan.

Signature

Daniel Dyer
http://www.footballpredictions.net

Andrea Desole - 10 Jun 2005 16:49 GMT
> You can use the static Thread.dumpStack() method, which is available
> prior  to 1.5.  But you still get the whole stack trace rather than just

but dumpStack prints the stack trace, which makes it a bit more
difficult to parse. I still find StackTraceElement[] easier.

Just for completeness, I would also like to show something I just read
on the documentation of Throwable.getStackTrace() (but apparently
generally valid, since it refers to printStackTrace):

Some virtual machines may, under some circumstances, omit one or more
stack frames from the stack trace. In the extreme case, a virtual
machine that has no stack trace information concerning this throwable is
permitted to return a zero-length array from this method. Generally
speaking, the array returned by this method will contain one element for
every frame that would be printed by printStackTrace

> the file  and line number.  If you use a logging framework such as
> Log4J  (http://logging.apache.org) you can output this information
> without the  stack trace more easily (but this information is expensive

log4j or any good logging framework is also an option
Tris Orendorff - 11 Jun 2005 17:20 GMT
> I am trying to add some debug print statements to my java code.  I
> would like the print statement to display the file name and line number
> from where the print statement came from.
>
> ex)
> System.out.println(fileName + ":" + lineNum + " Debug statement");

Forget the debug print statements.  Now is the time to start usng jUnit and creating unit tests that can
automatically test your methods.  Both are the same difficulty to create but only one helps you throughout the
life of the software.

Sincerely,

Tris Orendorff
[Two antennae meet on a roof, fall in love and get married. The ceremony wasn't much, but the reception
was excellent.]
Kenneth P. Turvey - 11 Jun 2005 18:29 GMT
> Forget the debug print statements.  Now is the time to start using jUnit
> and creating unit tests that can
> automatically test your methods.  Both are the same difficulty to create
> but only one helps you throughout the life of the software.

That said, if you still want debugging statements in your code you might
look at the Logger facility.  It looks like an improvement on debug print
statements among other things.  

Signature

Kenneth P. Turvey <kt@squeakydolphin.com>

Kenneth P. Turvey - 11 Jun 2005 18:32 GMT
> Forget the debug print statements.  Now is the time to start usng jUnit
> and creating unit tests that can
> automatically test your methods.  Both are the same difficulty to create
> but only one helps you throughout the life of the software.

That said, if you still want debugging output for your code you might want
to look at the logging facility.  I think it was added in 1.5.  It looks
like a good replacement for debug print statements among other uses.

Signature

Kenneth P. Turvey <kt@squeakydolphin.com>



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.