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
.
trace()在控制台输出结果。我想把结果作为字符串保存到文件中。我没有为函数定义名称,也不能用call .call .name获取它们的名称。
5 个解决方案
#1
75
I'm not sure about firefox, but in v8/chrome you can use a method on the Error constructor called captureStackTrace
. (More info here)
我对firefox不太确定,但是在v8/chrome中,您可以在错误构造函数captureStackTrace上使用一个方法。(更多信息)
So a hacky way to get it would be:
所以用一种hacky的方法来得到它:
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.
通常,getStackTrace在捕获时将位于堆栈上。第二个参数将getStackTrace排除在堆栈跟踪中。
#2
27
Error.stack is what you need. It works in Chrome and Firefox. For example
错误。堆栈是您需要的。它可以在Chrome和Firefox中工作。例如
try { var a = {}; a.debug(); } catch(ex) {console.log(ex.stack)}
will give in Chrome:
在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:
在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
#3
14
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:
有一个叫stacktrace的库。这将给您跨浏览器堆栈跟踪。您可以简单地使用它,包括脚本和调用在任何时候:
var trace = printStackTrace();
#4
11
This will give a stack trace (as array of strings) for modern Chrome, Firefox, Opera and IE10+
这将为现代Chrome、Firefox、Opera和IE10+提供堆栈跟踪(作为字符串数组)
function getStackTrace () {
var stack;
try {
throw new Error('');
}
catch (error) {
stack = error.stack || '';
}
stack = stack.split('\n').map(function (line) { return line.trim(); });
return stack.splice(stack[0] == 'Error' ? 2 : 1);
}
Usage:
用法:
console.log(getStackTrace().join('\n'));
It excludes from the stack its own call as well as title "Error" that is used by Chrome and Firefox (but not IE).
它将自己的调用以及Chrome和Firefox(但不包括IE)使用的标题“错误”排除在堆栈之外。
It shouldn't crash on older browsers but just return empty array. If you need more universal solution look at stacktrace.js. Its list of supported browsers is really impressive but to my mind it is very big for that small task it is intended for: 37Kb of minified text including all dependencies.
它不应该在旧浏览器上崩溃,而应该返回空数组。如果需要更通用的解决方案,请查看stacktrace.js。它支持的浏览器列表确实令人印象深刻,但是我认为它对于这个小任务非常重要,它的目标是:37Kb的缩小文本,包括所有依赖项。
#5
5
This is only a minor enhancement to Konstantin's excellent code. It cuts a bit on the expense of throwing-catching and just instantiates the Error stack:
这只是康斯坦丁优秀代码的一个小小的改进。它减少了抛出捕获的费用,并且只实例化了错误堆栈:
function getStackTrace () {
let stack = new Error().stack || '';
stack = stack.split('\n').map(function (line) { return line.trim(); });
return stack.splice(stack[0] == 'Error' ? 2 : 1);
}
I usually want a specific level of stack trace (for my custom logger) so this is also possible when calling:
我通常需要一个特定级别的堆栈跟踪(对于我的自定义日志记录器),因此在调用时也可以这样做:
getStackTrace()[2]; // get stack trace info 2 levels-deep
#1
75
I'm not sure about firefox, but in v8/chrome you can use a method on the Error constructor called captureStackTrace
. (More info here)
我对firefox不太确定,但是在v8/chrome中,您可以在错误构造函数captureStackTrace上使用一个方法。(更多信息)
So a hacky way to get it would be:
所以用一种hacky的方法来得到它:
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.
通常,getStackTrace在捕获时将位于堆栈上。第二个参数将getStackTrace排除在堆栈跟踪中。
#2
27
Error.stack is what you need. It works in Chrome and Firefox. For example
错误。堆栈是您需要的。它可以在Chrome和Firefox中工作。例如
try { var a = {}; a.debug(); } catch(ex) {console.log(ex.stack)}
will give in Chrome:
在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:
在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
#3
14
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:
有一个叫stacktrace的库。这将给您跨浏览器堆栈跟踪。您可以简单地使用它,包括脚本和调用在任何时候:
var trace = printStackTrace();
#4
11
This will give a stack trace (as array of strings) for modern Chrome, Firefox, Opera and IE10+
这将为现代Chrome、Firefox、Opera和IE10+提供堆栈跟踪(作为字符串数组)
function getStackTrace () {
var stack;
try {
throw new Error('');
}
catch (error) {
stack = error.stack || '';
}
stack = stack.split('\n').map(function (line) { return line.trim(); });
return stack.splice(stack[0] == 'Error' ? 2 : 1);
}
Usage:
用法:
console.log(getStackTrace().join('\n'));
It excludes from the stack its own call as well as title "Error" that is used by Chrome and Firefox (but not IE).
它将自己的调用以及Chrome和Firefox(但不包括IE)使用的标题“错误”排除在堆栈之外。
It shouldn't crash on older browsers but just return empty array. If you need more universal solution look at stacktrace.js. Its list of supported browsers is really impressive but to my mind it is very big for that small task it is intended for: 37Kb of minified text including all dependencies.
它不应该在旧浏览器上崩溃,而应该返回空数组。如果需要更通用的解决方案,请查看stacktrace.js。它支持的浏览器列表确实令人印象深刻,但是我认为它对于这个小任务非常重要,它的目标是:37Kb的缩小文本,包括所有依赖项。
#5
5
This is only a minor enhancement to Konstantin's excellent code. It cuts a bit on the expense of throwing-catching and just instantiates the Error stack:
这只是康斯坦丁优秀代码的一个小小的改进。它减少了抛出捕获的费用,并且只实例化了错误堆栈:
function getStackTrace () {
let stack = new Error().stack || '';
stack = stack.split('\n').map(function (line) { return line.trim(); });
return stack.splice(stack[0] == 'Error' ? 2 : 1);
}
I usually want a specific level of stack trace (for my custom logger) so this is also possible when calling:
我通常需要一个特定级别的堆栈跟踪(对于我的自定义日志记录器),因此在调用时也可以这样做:
getStackTrace()[2]; // get stack trace info 2 levels-deep