Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Clojure Stacktrace

Tags:

clojure

I am aware of http://richhickey.github.com/clojure/clojure.stacktrace-api.html .

Is there a way to get the current stacktrace w/o throwing an exception and catching it?

(I'm debugging a piece of code, and want to capture stacktraces at certain points so I can analyze what's going on.)

Thanks!

like image 273
user1383359 Avatar asked May 10 '12 07:05

user1383359


People also ask

How do you get a Stacktrace?

You can obtain a stack trace from a thread – by calling the getStackTrace method on that Thread instance. This invocation returns an array of StackTraceElement, from which details about stack frames of the thread can be extracted.

How do I get the string Stacktrace as an exception?

The function printStackTrace() of the Exception class can take one parameter, either a PrintStream or a PrintWriter. Thus, it is possible, using a StringWriter, to print the stack trace into a String: StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e. printStackTrace(pw);

Should Stacktrace be logged?

Therefore, you should log a stacktrace if, and only if, and always if, the exception indicates a bug in the program. However, that does not always indicate that a method you write should catch and log the exception.

What is full Stacktrace?

A full stack trace allows you to see the chain of files from when your custom tag reached your set breakpoint. If you click on any of the pages in the caller chain then FusionDebug will show you that page and will highlight the line at which the next page in the chain was called.


2 Answers

use clojure.repl.pst

user=> (try (/ 1 0) (catch Exception e (pst e)))
ArithmeticException Divide by zero
    clojure.lang.Numbers.divide (Numbers.java:156)
    clojure.lang.Numbers.divide (Numbers.java:3691)
    user/eval28 (NO_SOURCE_FILE:8)
    clojure.lang.Compiler.eval (Compiler.java:6511)
    clojure.lang.Compiler.eval (Compiler.java:6477)
    clojure.core/eval (core.clj:2797)
    clojure.main/repl/read-eval-print--6569 (main.clj:245)
    clojure.main/repl/fn--6574 (main.clj:266)
    clojure.main/repl (main.clj:266)
    clojure.main/repl-opt (main.clj:332)
    clojure.main/main (main.clj:427)
    clojure.lang.Var.invoke (Var.java:423)
like image 87
number23_cn Avatar answered Nov 15 '22 10:11

number23_cn


This code return StackTraceElement array which is not so hard to turn in readable form:

(.getStackTrace (Thread/currentThread))
like image 25
om-nom-nom Avatar answered Nov 15 '22 11:11

om-nom-nom