Debug Print Statements

G

Ghost

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.
 
A

Andrea Desole

Ghost said:
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?

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.
 
D

Daniel Dyer

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.


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.
 
A

Andrea Desole

Daniel said:
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
 
T

Tris Orendorff

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.]
 
K

Kenneth P. Turvey

Tris said:
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.
 
K

Kenneth P. Turvey

Tris said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top