Debug Print Statements

Discussion in 'Java' started by Ghost, Jun 10, 2005.

  1. Ghost

    Ghost Guest

    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.
    Ghost, Jun 10, 2005
    #1
    1. Advertising

  2. 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.
    Andrea Desole, Jun 10, 2005
    #2
    1. Advertising

  3. Ghost

    Daniel Dyer Guest

    On Fri, 10 Jun 2005 15:49:05 +0100, Andrea Desole
    <> 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
    Daniel Dyer, Jun 10, 2005
    #3
  4. 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
    Andrea Desole, Jun 10, 2005
    #4
  5. "Ghost" <> wrote in
    news::

    > 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.]
    Tris Orendorff, Jun 11, 2005
    #5
  6. 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 <>
    Kenneth P. Turvey, Jun 11, 2005
    #6
  7. 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 <>
    Kenneth P. Turvey, Jun 11, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neil Zanella
    Replies:
    8
    Views:
    1,181
    mfmehdi
    Oct 20, 2006
  2. Harry George
    Replies:
    6
    Views:
    371
    Bart Nessux
    Feb 23, 2004
  3. keto
    Replies:
    0
    Views:
    927
  4. Vince
    Replies:
    12
    Views:
    746
    Martin Gregorie
    Jan 21, 2008
  5. John Crichton
    Replies:
    6
    Views:
    261
    John Crichton
    Jul 12, 2010
Loading...

Share This Page