Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Debug Print Statements

Reply
Thread Tools

Debug Print Statements

 
 
Ghost
Guest
Posts: n/a
 
      06-10-2005
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.

 
Reply With Quote
 
 
 
 
Andrea Desole
Guest
Posts: n/a
 
      06-10-2005


Ghost wrote:
> 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.
 
Reply With Quote
 
 
 
 
Daniel Dyer
Guest
Posts: n/a
 
      06-10-2005
On Fri, 10 Jun 2005 15:49:05 +0100, Andrea Desole
<(E-Mail Removed)> wrote:

>
>
> Ghost wrote:
>> 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.



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.

--
Daniel Dyer
http://www.footballpredictions.net
 
Reply With Quote
 
Andrea Desole
Guest
Posts: n/a
 
      06-10-2005


Daniel Dyer wrote:
> 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
 
Reply With Quote
 
Tris Orendorff
Guest
Posts: n/a
 
      06-11-2005
"Ghost" <(E-Mail Removed)> wrote in
news:(E-Mail Removed) oups.com:

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

 
Reply With Quote
 
Kenneth P. Turvey
Guest
Posts: n/a
 
      06-11-2005
Tris Orendorff wrote:

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

--
Kenneth P. Turvey <(E-Mail Removed)>
 
Reply With Quote
 
Kenneth P. Turvey
Guest
Posts: n/a
 
      06-11-2005
Tris Orendorff wrote:

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

--
Kenneth P. Turvey <(E-Mail Removed)>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if statements and case statements questions John Crichton Ruby 6 07-12-2010 06:17 PM
Prepare Statements VS Statements Vince Java 12 01-21-2008 01:18 PM
component statements within architecture statements Neil Zanella VHDL 8 10-20-2006 09:05 AM
Re: What's the overhead of assertion or debug statements in Python? Tim Peters Python 0 07-17-2004 06:07 AM
if statements with or w/o else statements Harry George Python 6 02-23-2004 06:48 PM



Advertisments