Results 1 to 10 of 10
  1. #1
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    57
    Rep Power
    0

    Default Need advice for debugging strategy

    I am working on a fairly complicated application that includes as one of its features, simulation of a DSP chip's operation. I need to debug some of the functions which are not working 100% correctly. I didn't write this part of the code - I inherited it from an open source library. However I have studied it a lot and find that I need to add a couple printf statements in widely separated areas of the code to capture the values of certain variables.

    What I'm doing right now is to comment in/out those printf statements as needed. If I leave them in all the time, the program cannot run fast enough to do its simulation in real time. Also I would rather capture these debug statements to a file rather than stdout as I am doing currently. However I do not want to revise the instructions I need to debug so that I can pass a file I/O context down there. I'd rather send information from these low level functions back to the class which manages the menus and files etc. and have that class take care of it. This is also the class that would have code to initialize and stop the simulation.

    I get the impression that I can do this with notifications or listeners or something but I'm not clear about the best approach to take. I'm also concerned that everything comes out in the actual order that it happened.

    Any suggestions?

    Thanks,

    DL

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,001
    Rep Power
    6

    Default Re: Need advice for debugging strategy

    Uh, that's a bit of a mess of a question that jumps left, right and then hides under the bed. From debugging statements to listeners to low level functions.

    I really don't know what you are actually asking about. I do spot one thing in there that I can respond to, wanting to send the debug output to a file without changing all the code. You say you use System.out.printf(), you can actually replace System.out (System.setOut()) so you put a PrintStream there that wraps around a FileOutputStream.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    57
    Rep Power
    0

    Default Re: Need advice for debugging strategy

    Quote Originally Posted by gimbal2 View Post
    Uh, that's a bit of a mess of a question that jumps left, right and then hides under the bed. From debugging statements to listeners to low level functions.

    I really don't know what you are actually asking about. I do spot one thing in there that I can respond to, wanting to send the debug output to a file without changing all the code. You say you use System.out.printf(), you can actually replace System.out (System.setOut()) so you put a PrintStream there that wraps around a FileOutputStream.
    Thanks for your response.

    Part #1 - I'm trying to trace things which happen in 2 different routines. I can do this just fine with System.out.printf, but I'd like an easier way to turn it off without needing to comment out the lines.

    Part #2 - I'd like to send debug output to a file (or stream), but I'd like to avoid the need to rewrite all of the methods in order to pass this stream instance as a parameter between the point where the simulation is initiated, down to where the information I'm interested is generated - which is probably 5 or more calls away. If there's no way around it then I'll probably do it. And to "turn it off" I can just pass null and then nothing would happen.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Need advice for debugging strategy

    Here is a suggestion for you:

    Java Code:
    public class DebugIdea {
        private final static boolean DEBUG = false;
       public static void main(String [] args) {
          if (DEBUG) {
             System.out.println("Hello There");
          }
          System.out.println("Goodbye");
       }
    }
    The code within the DEBUG block will not be included in the compiled class. The reason is that the
    variable DEBUG is declared final so the compiler knows it can't change so why include the code
    that can never execute? If you remove the final keyword, then look at the class file you can
    see that it is now included, even though the value is still false. So set it to true to debug the code
    and to false when you are done. The down side is that you need to recompile the code to get rid of
    the debug statements. The plus side is that you can leave them in the source.

    Another option would be to use assertions which are only available to run when enabled.

    Regards,
    Jim
    Last edited by jim829; 08-02-2014 at 03:47 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    57
    Rep Power
    0

    Default Re: Need advice for debugging strategy

    Thanks Jim.

    It is not a big deal to recompile my code. 90% of the time I am running my program in Eclipse.

    Now how do I make it so that I can access this DEBUG in two different classes while only having to set it in one place?

    I'll answer my own question...

    Just create the Debug class with one public variable, DEBUG, as shown. Then in the other classes that want to use the value of Debug.DEBUG, just inport the class and then you can reference it. Sweet, looks like it does just what I wanted it to. Thanks!

    Many thanks,

    DL
    Last edited by Digital Larry; 08-02-2014 at 10:11 PM.

  6. #6
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    57
    Rep Power
    0

    Default Re: Need advice for debugging strategy

    Quote Originally Posted by gimbal2 View Post
    I do spot one thing in there that I can respond to, wanting to send the debug output to a file without changing all the code. You say you use System.out.printf(), you can actually replace System.out (System.setOut()) so you put a PrintStream there that wraps around a FileOutputStream.
    Thanks for the hint. This looks like it might work for me. How do I reset the stdout when I am done? I only want to redirect the messages that get sent to stdout during the simulate() method.

  7. #7
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    57
    Rep Power
    0

    Default Re: Need advice for debugging strategy

    OK I found a way to reset the stdout, which seems to work. That is to call:

    Java Code:
    PrintStream ps = new PrintStream(new FileOutputStream(FileDescriptor.out));
    System.setOut(ps);
    However, the file that I redirected stdout to, only moments before, with this set of instructions:

    Java Code:
    f = new FileOutputStream("simulator-debug.txt");
    System.setOut(new PrintStream(f));
    generates files which gedit doesn't want to open, claiming it cannot determine the encoding used. Ah what did I do wrong?

    According to Java documentation -

    FileOutputStream is meant for writing streams of raw bytes such as image data. For writing streams of characters, consider using FileWriter.
    Last edited by Digital Larry; 08-04-2014 at 06:05 AM.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Need advice for debugging strategy

    If I were you I would use this as an opportunity to get to grips with a proper logging framework.
    They're not that complicated.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Posts
    57
    Rep Power
    0

    Default Re: Need advice for debugging strategy

    That may be true, however from the suggestions I have received so far, I am just ONE STEP from accomplishing exactly what I had in mind. So if there's a simple way to redirect System.out that allows me to generate a text file instead of a binary thing, that would be most expedient.

    Regarding a "proper logging framework", it wouldn't hurt to learn about it - any good tutorial(s) online that you can recommend?

    Thanks,

    DL

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Need advice for debugging strategy

    No idea.
    I just use whatever's in front of me...at the moment it's logback.
    The last contract was log4j (I think).

    They're all much of a muchness...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Is this a good solution strategy?
    By hedonist in forum New To Java
    Replies: 2
    Last Post: 07-31-2009, 08:30 PM
  2. Battery - Online Strategy-RPG
    By M77 in forum Reviews / Advertising
    Replies: 1
    Last Post: 11-16-2008, 09:21 PM
  3. Strategy Pattern
    By mew in forum New To Java
    Replies: 0
    Last Post: 01-25-2008, 07:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •