Page 3 of 4 FirstFirst 1234 LastLast
Results 41 to 60 of 61
  1. #41
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    That's a good solution for simplifying println(). You could even pass a command line argument to the main to set a flag, saying whether to print debug statements.

  2. #42
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default I Think We Have A Winner!

    SK, thats a really good idea.

    OK for the n00bs, combine everyone's codes so far...
    Java Code:
    public class MyDebugger{
       private static boolean debug = false;
       
       public static void debugPoint(Object oValue){
          if (debug){
             System.err.println(oValue.toString());
             // Thread.dumpStack();
          }
       }
    
       public static void main(String[] args){
          if (args.length > 0 && args[0].toLowerCase().equals("debug")){
            debug = true;
          }
    
          String sValue = "  WELCOME";
          debugPoint(sValue);
          
          sValue = "To Java Forums";
          debugPoint(sValue);
          
          sValue = "saikumar komati";
          debugPoint(sValue);
       }
    }
    Last edited by angryboy; 01-26-2009 at 01:18 AM. Reason: street slangs removed
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #43
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Talking hardcore core at the core of the hardharts

    Quote Originally Posted by Steve11235 View Post
    Has no one mentioned JUnit? Of course, most people not using an IDE are probably not ready for that...
    Have a heart, Steve, tell me if this is a HardCore debugger or a buggy HardCore....

    Java Code:
    S.t.e.v.e.
    Super titainum event vent escape - a new debugger proposed by Captian Hopper, rolling in her Grave at the discussion here. Cue fog machine, Friday Night Channel 11 move, VO rolls a small, blinking device in one hand:" Little did they know, the Code Corruptus among us would rise to the challenge."

    Fade to opening roll: Steam rises from 1934 Sheffield Silver Coffee Pot, discarded on moonlit dirt. pull back + pan up to Cold Spring Granite tombstone: "The Grand Lady" ... Coffin ( ever notice how close that word is to coffee ) has door pushed open from within, dirt falls away, a hollow, gaunt face announces: "We need a new debugger!" ( hold ) face looks down in concern.

    Fade to contemporary Office setting, a recent CS Graduate enters stage right.

    Supervisor asks: "Do you have the headers for tesjeract?"

    Medium on CS Grad, expressionless.

    Supevisor does lookaside to assistant, who announces: "There's a delivery from Carried Away Coffin Company on loading dock twenty one."

    ( to be continued )
    Last edited by Nicholas Jordan; 01-28-2009 at 06:50 AM.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  4. #44
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    I ain't no C.S. major! I'm an old RPG (Never heard of it? Back in the '90's, before there was networking, we used it on the AS/400. It's a cross between COBOL and assembler.) hack who picked up Sun's "Core Java" book and evolved before the sun set on RPG (All my friends are currently unemployed. So am I, but at least I have a chance!)

    You probably think that's a picture of Wally from Dilbert, too. Well, it is, but Wally's my hero...

  5. #45
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Default

    I remember RPG from trade school, we had a Univac with 4K wire-wrap, boot loader was on 80-column cards.

    Ever heard of the Wortmann Mod?

    ( Hint: 028 prints L to R )

    Probably not documented,.....

    Looks to me like you could use angryboy as New Team Lead, who else can we pick up?
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  6. #46
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    Quote Originally Posted by Nicholas Jordan View Post
    Looks to me like you could use angryboy as New Team Lead, who else can we pick up?
    yeah i'm down with that, as long as its GPL. ;)
    Last edited by angryboy; 01-26-2009 at 03:45 AM. Reason: street slangs removed
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  7. #47
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Question The rebellion has to have a license?

    Quote Originally Posted by angryboy View Post
    yeah i'm down with that, as long as its GPL. ;)
    May we use Creative Commons?
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  8. #48
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    Quote Originally Posted by Nicholas Jordan View Post
    May we use Creative Commons?
    Which one?
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  9. #49
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default

    Any one have an example of using Logger with number of classes?

    All I have found so far si examples of one class with main , and when I try using Logger in programs - I have some problems when using the logger in second class

  10. #50
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    my debugging practice is, like many, to use print statements. to be more specific, i print the entry point and return point (and value if necessary) of each method. if i have some kind of errant value, i can more easily pinpoint the neighborhood this way and add in more print statements to print more local values in the culprit method, then if necessary, take the next step to print in anything requiring conditions to see the actual paths taken within a method.

    no real science to it. i'm sure there's a less thorough way of doing it. honestly, when it comes to code i've written, when i see a bug, i know what's causing it already without checking any print statements. i find it easier to debug in larger projects where i'm not using just my own code, especially at work, where i've finally started using the eclipse debugger for java and dbx for c.

  11. #51
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    Quote Originally Posted by itaipee View Post
    Any one have an example of using Logger with number of classes?

    All I have found so far si examples of one class with main , and when I try using Logger in programs - I have some problems when using the logger in second class
    what kind of problems?
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  12. #52
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

  13. #53
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default creative use of assertions

    During testing, turn on and off debug using assertions.
    cmd: java -ea MyApp.java
    Java Code:
      [COLOR=PURPLE]public[/COLOR] [COLOR=PURPLE]static[/COLOR] [COLOR=PURPLE]void[/COLOR] main[COLOR=BLACK]([/COLOR]String[COLOR=BLACK][[/COLOR][COLOR=BLACK]][/COLOR] args[COLOR=BLACK])[/COLOR] [COLOR=BLACK]{[/COLOR]
        [COLOR=PURPLE]boolean[/COLOR] debug [COLOR=BLACK]=[/COLOR] [COLOR=NAVY][B]false[/B][/COLOR][COLOR=BLACK];[/COLOR]
        assert [COLOR=BLACK]([/COLOR]debug[COLOR=BLACK]=[/COLOR][COLOR=NAVY][B]true[/B][/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        
        [COLOR=NAVY]if[/COLOR][COLOR=BLACK]([/COLOR]debug[COLOR=BLACK])[/COLOR] System[COLOR=BLACK].[/COLOR]out[COLOR=BLACK].[/COLOR]println[COLOR=BLACK]([/COLOR][COLOR=SILVER]"debugged"[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        
        System[COLOR=BLACK].[/COLOR]out[COLOR=BLACK].[/COLOR]println[COLOR=BLACK]([/COLOR][COLOR=SILVER]"<END>"[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
      [COLOR=BLACK]}[/COLOR]
    And I realized that if you make debug final (and false) after testing, the compiler will take out all the "if (debug)" statements. So there's no harm leaving it in the code.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  14. #54
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Question how to change that efficiently in fifiteen files.

    Quote Originally Posted by angryboy View Post
    ....And I realized that if you make debug final (and false) after testing, the compiler will take out all the "if (debug)" statements. So there's no harm leaving it in the code.
    Java Code:
    public class TestClass
    {
       private static final boolean debug = ( true / false );
    The next issue is how to change that efficiently in fifiteen files.

    Study recusive descent file lister and regex's.

    What you have posted is advised in AW Professional Series, this is not "apprentice" level work - change your signature to journeyman ...
    The word 'journeyman' comes from the French word journée, meaning the period of one day; this refers to their right to charge a fee for each day's work. They would normally be employed by a master craftsman, but would live apart and might have a family of their own. A journeyman could not employ others. In contrast, an apprentice would be bound to a master, usually for a fixed term of seven years, and lived with the master as a member of the household, receiving most or all of their compensation in terms of room and board.
    Source: Wiki
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  15. #55
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    To suggest a rather interesting solution...

    Create a small program that searches files for System.out.println and displays the lines with it, asking if you want to remove the line. It takes a while, but hey, if it works, you can remove debug lines that you don't want, and leave the ones you do.

    Crazy, I know, but it could work :p

    EDIT: This is for removing print statements :D
    Last edited by Singing Boyo; 05-14-2009 at 02:59 AM.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  16. #56
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Eclipse and Netbeans both do that... It's called Search.

    Back to the original question: As much time as I spend doing it, you would think I'd have a better answer!

    Started using Logger. With a bit of tweaking, as in adding a Formatter class and a factory, it is very useful.

  17. #57
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    @Singing Boyo: you can use Logger and pro guard to strip out the debugging stuff.
    edit:
    the goal is to keep the debuging codes in the source w/o having to maintain separate src files.
    Last edited by angryboy; 05-14-2009 at 08:13 PM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  18. #58
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    I did say it was crazy :p

    @Steve: We are supposedly creating ideas for NON-IDE developers, who don't have a mass search like Eclipse or NetBeans
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  19. #59
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default Dynamic Proxy Rules!

    Java Code:
    [COLOR=NAVY]import[/COLOR] java[COLOR=BLACK].[/COLOR]lang[COLOR=BLACK].[/COLOR]reflect[COLOR=BLACK].[/COLOR][COLOR=BLACK]*[/COLOR][COLOR=BLACK];[/COLOR]
    
    [COLOR=PURPLE]public[/COLOR] [COLOR=PURPLE]class[/COLOR] DynamicProxyTest [COLOR=BLACK]{[/COLOR]
      [COLOR=PURPLE]public[/COLOR] [COLOR=PURPLE]static[/COLOR] [COLOR=PURPLE]void[/COLOR] main[COLOR=BLACK]([/COLOR]String[COLOR=BLACK][[/COLOR][COLOR=BLACK]][/COLOR] args[COLOR=BLACK])[/COLOR][COLOR=BLACK]{[/COLOR]
        IFoo foo [COLOR=BLACK]=[/COLOR] [COLOR=BLACK]([/COLOR]IFoo[COLOR=BLACK])[/COLOR] DebugProxy[COLOR=BLACK].[/COLOR]newInstance[COLOR=BLACK]([/COLOR][COLOR=NAVY]new[/COLOR] Foo[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        foo[COLOR=BLACK].[/COLOR]bar[COLOR=BLACK]([/COLOR][COLOR=NAVY][B]null[/B][/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
      [COLOR=BLACK]}[/COLOR]
    [COLOR=BLACK]}[/COLOR]
    
    [COLOR=PURPLE]interface[/COLOR] IFoo[COLOR=BLACK]{[/COLOR]
      [COLOR=PURPLE]public[/COLOR] Object bar[COLOR=BLACK]([/COLOR]Object obj[COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
    [COLOR=BLACK]}[/COLOR]
    
    [COLOR=PURPLE]class[/COLOR] Foo [COLOR=NAVY]implements[/COLOR] IFoo[COLOR=BLACK]{[/COLOR]
      [COLOR=PURPLE]public[/COLOR] Object bar[COLOR=BLACK]([/COLOR]Object obj[COLOR=BLACK])[/COLOR][COLOR=BLACK]{[/COLOR]
        System[COLOR=BLACK].[/COLOR]out[COLOR=BLACK].[/COLOR]println[COLOR=BLACK]([/COLOR][COLOR=SILVER]"Bar is called!"[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        [COLOR=NAVY]return[/COLOR] obj[COLOR=BLACK];[/COLOR]
      [COLOR=BLACK]}[/COLOR]
    [COLOR=BLACK]}[/COLOR]
    
    [COLOR=PURPLE]class[/COLOR] DebugProxy [COLOR=NAVY]implements[/COLOR] InvocationHandler [COLOR=BLACK]{[/COLOR]
      [COLOR=PURPLE]private[/COLOR] Object obj[COLOR=BLACK];[/COLOR]
    
      [COLOR=GREEN][I]// factory method for creating new instances...[/I][/COLOR]
      [COLOR=PURPLE]public[/COLOR] [COLOR=PURPLE]static[/COLOR] Object newInstance[COLOR=BLACK]([/COLOR]Object obj[COLOR=BLACK])[/COLOR] [COLOR=BLACK]{[/COLOR]
        [COLOR=NAVY]return[/COLOR] Proxy[COLOR=BLACK].[/COLOR]newProxyInstance[COLOR=BLACK]([/COLOR]
                  obj[COLOR=BLACK].[/COLOR]getClass[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK].[/COLOR]getClassLoader[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK],[/COLOR]
                  obj[COLOR=BLACK].[/COLOR]getClass[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK].[/COLOR]getInterfaces[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK],[/COLOR]
                  [COLOR=NAVY]new[/COLOR] DebugProxy[COLOR=BLACK]([/COLOR]obj[COLOR=BLACK])[/COLOR]
        [COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
      [COLOR=BLACK]}[/COLOR]
    
      [COLOR=GREEN][I]// constructor[/I][/COLOR]
      [COLOR=PURPLE]private[/COLOR] DebugProxy[COLOR=BLACK]([/COLOR]Object obj[COLOR=BLACK])[/COLOR] [COLOR=BLACK]{[/COLOR]
        [COLOR=NAVY]this[/COLOR][COLOR=BLACK].[/COLOR]obj [COLOR=BLACK]=[/COLOR] obj[COLOR=BLACK];[/COLOR]
      [COLOR=BLACK]}[/COLOR]
    
      @Override
      [COLOR=PURPLE]public[/COLOR] Object invoke[COLOR=BLACK]([/COLOR]Object proxy[COLOR=BLACK],[/COLOR] Method m[COLOR=BLACK],[/COLOR] Object[COLOR=BLACK][[/COLOR][COLOR=BLACK]][/COLOR] args[COLOR=BLACK])[/COLOR]
      [COLOR=NAVY]throws[/COLOR] Throwable
      [COLOR=BLACK]{[/COLOR]
        Object result[COLOR=BLACK];[/COLOR]
        [COLOR=NAVY]try[/COLOR] [COLOR=BLACK]{[/COLOR]
        
          [COLOR=GREEN][I]// DEBUG CODES HERE:[/I][/COLOR]
          System[COLOR=BLACK].[/COLOR]out[COLOR=BLACK].[/COLOR]println[COLOR=BLACK]([/COLOR][COLOR=SILVER]"before method "[/COLOR] [COLOR=BLACK]+[/COLOR] m[COLOR=BLACK].[/COLOR]getName[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
          result [COLOR=BLACK]=[/COLOR] m[COLOR=BLACK].[/COLOR]invoke[COLOR=BLACK]([/COLOR]obj[COLOR=BLACK],[/COLOR] args[COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
          
        [COLOR=BLACK]}[/COLOR][COLOR=NAVY]catch[/COLOR] [COLOR=BLACK]([/COLOR]InvocationTargetException e[COLOR=BLACK])[/COLOR] [COLOR=BLACK]{[/COLOR]
          [COLOR=NAVY]throw[/COLOR] e[COLOR=BLACK].[/COLOR]getTargetException[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        [COLOR=BLACK]}[/COLOR][COLOR=NAVY]catch[/COLOR] [COLOR=BLACK]([/COLOR]Exception e[COLOR=BLACK])[/COLOR] [COLOR=BLACK]{[/COLOR]
          [COLOR=NAVY]throw[/COLOR] [COLOR=NAVY]new[/COLOR] RuntimeException[COLOR=BLACK]([/COLOR][COLOR=SILVER]"unexpected invocation exception: "[/COLOR] [COLOR=BLACK]+[/COLOR]
          e[COLOR=BLACK].[/COLOR]getMessage[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        [COLOR=BLACK]}[/COLOR][COLOR=NAVY]finally[/COLOR] [COLOR=BLACK]{[/COLOR]
          System[COLOR=BLACK].[/COLOR]out[COLOR=BLACK].[/COLOR]println[COLOR=BLACK]([/COLOR][COLOR=SILVER]"after method "[/COLOR] [COLOR=BLACK]+[/COLOR] m[COLOR=BLACK].[/COLOR]getName[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        [COLOR=BLACK]}[/COLOR]
        [COLOR=NAVY]return[/COLOR] result[COLOR=BLACK];[/COLOR]
      [COLOR=BLACK]}[/COLOR]
    [COLOR=BLACK]}[/COLOR]
    Resources:
    Dynamic Proxy Classes
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  20. #60
    corlettk is offline Member
    Join Date
    Apr 2009
    Location
    Brisbane
    Posts
    86
    Rep Power
    0

    Default krc.utilz.Tracer

    Here's my poormans' debugger....

    Java Code:
    package krc.utilz;
    
    import java.io.PrintStream;
    
    public class Tracer
    {
      public static PrintStream out = null;
      public boolean enabled = true;
    
      public enum Format { LONG, SHORT }
      public Format format;
    
      public Tracer() {
        this(System.err, Tracer.Format.LONG);
      }
      public Tracer(PrintStream out) {
        this(out, Tracer.Format.LONG);
      }
      public Tracer(Format format) {
        this(System.err, format);
      }
      public Tracer(PrintStream out, Format format) {
        this.out = out;
        this.format = format;
      }
    
      public String toString() {
        return get(5);
      }
      public String get() {
        return get(2);
      }
      public String get(int i) {
        StackTraceElement[] st = Thread.currentThread().getStackTrace();
        if (i>=st.length) i = st.length-1; //don't go past top of stack
        StackTraceElement t = st[i];
        String s = t.getFileName()+":"+t.getLineNumber()+":";
        if (this.format == Format.LONG) {
          s += t.getClassName()+"."+t.getMethodName();
        }
        return(s);
      }
    
      public void debug(String msg) {
        if(!enabled)return;
        out.println("DEBUG: "+msg);
      }
    
      public void print(String msg) {
        if(!enabled)return;
        if (out==null) return;
        out.println(get(3)+" : "+msg);
      }
      public void print() {
        this.print(get(3));
      }
      public void print(int i) {
        this.print(get(i));
      }
    
      public static String getCurrentMethodName() {
        StackTraceElement[] st = Thread.currentThread().getStackTrace();
        return(st[2].getMethodName());
      }
    }
    And here's an example of it's usage:

    Java Code:
    package forums.chat;
    
    /**
     * a loopback chat server
     */
    
    import java.io.InputStreamReader;
    import java.io.BufferedReader;
    import java.io.OutputStreamWriter;
    import java.io.BufferedWriter;
    import java.io.PrintWriter;
    import java.io.IOException;
    import java.net.Socket;
    import java.net.ServerSocket;
    
    import krc.utilz.Tracer;
    
    //echo client input until client closes connection.
    class SocketHandler implements Runnable
    {
      private static final boolean AUTO_FLUSH=true;
      private static krc.utilz.Tracer tracer = new Tracer(System.out);
    
      private Socket socket;
      private BufferedReader reader = null;
      private PrintWriter writer = null;
      private static int handlerID=0;
    
      public SocketHandler(Socket socket) throws IOException {
        this.socket = socket;
    
        this.writer = new PrintWriter( new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), AUTO_FLUSH );
        if(writer==null) throw new NullPointerException("socket writer is null");
    
        this.reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        if(reader==null) throw new NullPointerException("socket reader is null");
    
        System.out.println("DEBUG: handlerID="+(++handlerID));
      }
    
      public void run() {
        try {
          while(true) {
    tracer.print();
            String request = reader.readLine();
    tracer.print();
            writer.println(request);
    tracer.print();
          }
        } catch(Exception e) {
          e.printStackTrace();
        } finally {
          closeStreams();
        }
      }
    
      private void closeStreams() {
        try {
          if(writer!=null) writer.close(); 
          if(reader!=null) reader.close();
        } catch (Exception ignore) {
        }
      }
    
    }
    
    /**
     * an endless loopback chat server on port 9090
     */
    public class Server
    {
      public static final int PORT_NUMBER = 9090;
      public static void main(String[] args) {
        krc.utilz.Tracer tracer = new Tracer(System.out);
        try {
    tracer.print();
          ServerSocket server = new ServerSocket(Server.PORT_NUMBER);
          //while(true) {
    tracer.print();
          Socket socket = server.accept();
    tracer.print();
          SocketHandler handler = new SocketHandler(socket);
    tracer.print();
          new Thread(handler).start();
          //}
        } catch(Exception e) {
          tracer.print();
          e.printStackTrace();
        }
      }
    }
    This is most handy when attempting to debug a "time sensitive" context... like in a multithreaded environment, when the delay inherent in stepping through code might affect the outcome.

    As it happens, I spent most of yesterday dealing with one of these, in C#... an OLE server was returning _before_ it had actually completed the command, which was causing contention (expressed a security exception) on an underlying file... So, I just ported the above class to C#. 10 minutes later I had a cluepon... and two minutes after that a fix. All is well that ends well.

    I think "the ultimate" in-code debugger might use introspection and reflection to "wrap" every single method call in pre and post "events"... to produce a complete "trace" of the running application. This would be highly intrusive, prepresenting an unacceptable performance overhead, but the addition of an in-code "switch" to dis/enable tracing of "interesting blocks and/or periods" might just make it practical. I'll leave the implementation of this as an excercise for the student ;-)

    Cheers. Keith.

    Edit: Sorry, I completely missed the second and third page beofre posting... will go read now... this post may still be relevant, so I'm not going to "delete" it, yet.

Page 3 of 4 FirstFirst 1234 LastLast

Similar Threads

  1. Java Debug Interface
    By jbabe in forum Advanced Java
    Replies: 2
    Last Post: 09-02-2008, 05:04 AM
  2. Not able to debug simple hello world program
    By amit123solanki in forum New To Java
    Replies: 1
    Last Post: 06-12-2008, 03:41 PM
  3. Java Debug Level
    By keshari in forum New To Java
    Replies: 1
    Last Post: 05-26-2008, 08:23 AM
  4. how to debug the servlet
    By tommy in forum Java Servlet
    Replies: 1
    Last Post: 08-06-2007, 08:53 PM
  5. how to debug java code in eclipse
    By rajesh058 in forum Advanced Java
    Replies: 3
    Last Post: 07-25-2007, 11:19 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
  •