Thread: Dump stacktraces to a string...

Results 1 to 8 of 8
  1. #1 Dump stacktraces to a string... 
    Renown Programmer

    Nikki's Avatar
    Join Date
    Aug 2008
    Posts
    3,992
    Thanks given
    553
    Thanks received
    1,078
    Rep Power
    5000
    Figured some people might like this in case they miss it for some reason, they can log it to a file with this string...

    Code:
    	public static String generateStack(Throwable throwable) {
    		try {
    			StackTraceElement[] element = throwable.getStackTrace();
    			StringBuilder builder = new StringBuilder();
    			builder.append(throwable+"\n");
    			for(StackTraceElement e : element) {
    				builder.append("\tat "+e+"\n");
    			}
    			throwable.printStackTrace();
    			Throwable cause = throwable.getCause();
    			if(cause != null)
    				addCause(cause, element, builder);
    			return builder.toString();
    		} catch(Exception e) {
    			e.printStackTrace();
    		}
    		return "Could not generate stacktrace.";
    	}
    	
    	public static void addCause(Throwable cause, StackTraceElement[] causedTrace, StringBuilder builder) {
    		StackTraceElement[] trace = cause.getStackTrace();
            int m = trace.length-1, n = causedTrace.length-1;
            while (m >= 0 && n >=0 && trace[m].equals(causedTrace[n])) {
                m--; n--;
            }
            int framesInCommon = trace.length - 1 - m;
    
            builder.append("Caused by: " + cause + "\n");
            for (int i=0; i <= m; i++)
            	 builder.append("\tat " + trace[i]+"\n");
            if (framesInCommon != 0)
            	 builder.append("\t... " + framesInCommon + " more"+"\n");
    
            // Recurse if we have a cause
            Throwable ourCause = cause.getCause();
            if (ourCause != null)
                addCause(ourCause, trace, builder);
    	}
    Taken from the Throwable.printStackTrace() method, which prints it to System.err, and changed to a stringbuilder.
    Please don't add/pm me asking for RSPS help!

    Links:
    - Pastebin
    - Sleeksnap

    Reply With Quote  
     

  2. Thankful user:


  3. #2  
    RuneFatality

    Join Date
    May 2009
    Age
    27
    Posts
    2,350
    Thanks given
    1,099
    Thanks received
    388
    Rep Power
    531
    Good idea. I wonder why no-one else has never thought of this.
    Reply With Quote  
     

  4. Thankful user:


  5. #3  
    Renown Programmer

    Nikki's Avatar
    Join Date
    Aug 2008
    Posts
    3,992
    Thanks given
    553
    Thanks received
    1,078
    Rep Power
    5000
    I forgot to say, this will ALSO print it out to the console, so you can have 2 copies of it.
    Please don't add/pm me asking for RSPS help!

    Links:
    - Pastebin
    - Sleeksnap

    Reply With Quote  
     

  6. Thankful user:


  7. #4  
    Renown Programmer
    veer's Avatar
    Join Date
    Nov 2007
    Posts
    3,746
    Thanks given
    354
    Thanks received
    1,370
    Rep Power
    3032
    StringWriter out = new StringWriter();
    throwable.printStackTrace(new PrintWriter(out));
    String output = out.toString();
    Reply With Quote  
     


  8. #5  
    Renown Programmer

    Nikki's Avatar
    Join Date
    Aug 2008
    Posts
    3,992
    Thanks given
    553
    Thanks received
    1,078
    Rep Power
    5000
    Quote Originally Posted by super_ View Post
    StringWriter out = new StringWriter();
    throwable.printStackTrace(new PrintWriter(out));
    String output = out.toString();
    I forgot about that, I made this a long time ago and just wanted to show it

    The people thanking you can stop being jerks
    Please don't add/pm me asking for RSPS help!

    Links:
    - Pastebin
    - Sleeksnap

    Reply With Quote  
     

  9. Thankful users:


  10. #6  
    Renown Programmer

    Nikki's Avatar
    Join Date
    Aug 2008
    Posts
    3,992
    Thanks given
    553
    Thanks received
    1,078
    Rep Power
    5000
    Quote Originally Posted by 'Mystic Flow View Post
    You didn't make this a long time ago, this is recent , you always said you removed all of your old work because it's a disgrace to what you can do now
    I never removed my old work? It's all on my hard drive, this was a few months+ lol.

    You would be amazed how much I LEARNED in a few months.


    BFMV, I knew there was a simple way to do it, I just didn't really care about it at the time.
    Please don't add/pm me asking for RSPS help!

    Links:
    - Pastebin
    - Sleeksnap

    Reply With Quote  
     

  11. Thankful user:


  12. #7  
    Hi.

    'Mystic Flow's Avatar
    Join Date
    Nov 2007
    Posts
    7,146
    Thanks given
    256
    Thanks received
    1,252
    Rep Power
    3714
    Quote Originally Posted by Nikki View Post
    I never removed my old work? It's all on my hard drive, this was a few months+ lol.

    You would be amazed how much I LEARNED in a few months.


    BFMV, I knew there was a simple way to do it, I just didn't really care about it at the time.
    Either way, this isn't you work, all you did was copy code off the Throwable class and replace System.print with a string builder lol



    Reply With Quote  
     

  13. #8  
    Renown Programmer

    Nikki's Avatar
    Join Date
    Aug 2008
    Posts
    3,992
    Thanks given
    553
    Thanks received
    1,078
    Rep Power
    5000
    Quote Originally Posted by 'Mystic Flow View Post
    Either way, this isn't you work, all you did was copy code off the Throwable class and replace System.print with a string builder lol
    Basically, lol.
    Please don't add/pm me asking for RSPS help!

    Links:
    - Pastebin
    - Sleeksnap

    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. [Java] String.contains(String)
    By Shoes in forum Application Development
    Replies: 9
    Last Post: 10-26-2010, 01:13 AM
  2. String dump
    By Sub in forum Requests
    Replies: 3
    Last Post: 08-22-2010, 04:27 PM
  3. NPC Dump and Object Dump
    By Dynamik in forum Requests
    Replies: 1
    Last Post: 08-20-2008, 08:20 PM
  4. String Bow and C Bow
    By Rune Arc in forum Configuration
    Replies: 1
    Last Post: 07-16-2008, 04:17 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •