console.trace()
outputs its result on console.
I want to get the results as string and save them to a file.
I don\'t define names for functions and
Error.stack is what you need. It works in Chrome and Firefox. For example
try { var a = {}; a.debug(); } catch(ex) {console.log(ex.stack)}
will give in Chrome:
TypeError: Object #<Object> has no method 'debug'
at eval at <anonymous> (unknown source)
at eval (native)
at Object._evaluateOn (unknown source)
at Object._evaluateAndWrap (unknown source)
at Object.evaluate (unknown source)
and in Firefox:
@http://www.google.com.ua/:87 _firebugInjectedEvaluate("with(_FirebugCommandLine){try { var a = {}; a.debug() } catch(ex) {console.log(ex.stack)}\n};")
@http://www.google.com.ua/:87 _firebugEvalEvent([object Event])
@http://www.google.com.ua/:67
There is a library called stacktrace.js that gives you cross browser stack traces. You can use it simply by including the script and calling at any point:
var trace = printStackTrace();