Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
0
rated 0 times [  5] [ 5]  / answers: 1 / hits: 76203  / 13 Years ago, sat, july 16, 2011, 12:00:00

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 I also can not get their names with callee.caller.name.


More From » firefox

 Answers
50

I'm not sure about firefox, but in v8/chrome you can use a method on the Error constructor called captureStackTrace. (More info here)



So a hacky way to get it would be:



var getStackTrace = function() {
var obj = {};
Error.captureStackTrace(obj, getStackTrace);
return obj.stack;
};

console.log(getStackTrace());


Normally, getStackTrace would be on the stack when it's captured. The second argument there excludes getStackTrace from being included in the stack trace.


[#91158] Thursday, July 14, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaredsages

Total Points: 273
Total Questions: 97
Total Answers: 105

Location: French Southern and Antarctic Lands
Member since Fri, Jan 6, 2023
1 Year ago
jaredsages questions
;