Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn a stack trace into a string?

Tags:

gwt

Is it possible to print a stack trace to a string in GWT? The usual methods of using the classes in java.io won't work I think, because the java.io package is not available clientside (and Writer, PrintWriter, etc are in that package)

Thank you

like image 306
user291701 Avatar asked Dec 01 '12 21:12

user291701


People also ask

How do you analyze a stack trace?

Analyze external stack tracesFrom the main menu, select Code | Analyze Stack Trace or Thread Dump. In the Analyze Stack Trace dialog that opens, paste the external stack trace or thread dump into the Put a stacktrace here: text area. Click OK. The stack trace is displayed in the Run tool window.

How do I print stack trace logs?

The short answer is that all you have to do to print the stack trace of an exception using Java and Log4J (or the Apache Commons Logging project) is this: log. error("Your description here", exception);

How do you trace a string in Java?

Using StringWriter and PrintWriter in the catch block, and the purpose behind it is to print the given output in the form of a string. Now print the stack trace using the printStackTrace() method of the exception and after that write it in the writer. And finally, convert it into a string using the toString() method.


2 Answers

I'm not sure if StackTraceElement is emulated, but if it is you can run something like

for (StackTraceElement element : exception.getStackTrace()) {
    string += element + "\n";
}
like image 136
Riley Lark Avatar answered Nov 08 '22 05:11

Riley Lark


Here is the method I'm using to retrieve a full stack trace as a String in GWT :

private static String getMessage (Throwable throwable) {
    String ret="";
    while (throwable!=null) {
            if (throwable instanceof com.google.gwt.event.shared.UmbrellaException){
                    for (Throwable thr2 :((com.google.gwt.event.shared.UmbrellaException)throwable).getCauses()){
                            if (ret != "")
                                    ret += "\nCaused by: ";
                            ret += thr2.toString();
                            ret += "\n  at "+getMessage(thr2);
                    }
            } else if (throwable instanceof com.google.web.bindery.event.shared.UmbrellaException){
                    for (Throwable thr2 :((com.google.web.bindery.event.shared.UmbrellaException)throwable).getCauses()){
                            if (ret != "")
                                    ret += "\nCaused by: ";
                            ret += thr2.toString();
                            ret += "\n  at "+getMessage(thr2);
                    }
            } else {
                    if (ret != "")
                            ret += "\nCaused by: ";
                    ret += throwable.toString();
                    for (StackTraceElement sTE : throwable.getStackTrace())
                            ret += "\n  at "+sTE;
            }
            throwable = throwable.getCause();
    }

    return ret;
}
like image 21
Florent Bayle Avatar answered Nov 08 '22 06:11

Florent Bayle