Add option to print stack with debug messages
This commit is contained in:
parent
15d10d18a7
commit
c5a532c789
2 changed files with 49 additions and 13 deletions
|
@ -41,7 +41,7 @@ Zotero.Debug = new function () {
|
||||||
this.enabled = _console || _store;
|
this.enabled = _console || _store;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.log = function (message, level) {
|
this.log = function (message, level, stack) {
|
||||||
if (!_console && !_store) {
|
if (!_console && !_store) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -92,15 +92,24 @@ Zotero.Debug = new function () {
|
||||||
deltaStr = '(+' + delta + ')';
|
deltaStr = '(+' + delta + ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_stackTrace) {
|
if (stack === true) {
|
||||||
var stack = (new Error()).stack;
|
// Display stack starting from where this was called
|
||||||
var nl1Index = stack.indexOf("\n")
|
stack = Components.stack.caller;
|
||||||
var nl2Index = stack.indexOf("\n", nl1Index+1);
|
} else if (stack >= 0) {
|
||||||
var line2 = stack.substring(nl1Index+2, nl2Index-1);
|
let i = stack;
|
||||||
var debugLine = line2.substr(line2.indexOf("@"));
|
stack = Components.stack.caller;
|
||||||
|
while(stack && i--) {
|
||||||
|
stack = stack.caller;
|
||||||
|
}
|
||||||
|
} else if (_stackTrace) {
|
||||||
|
// Stack trace enabled globally
|
||||||
|
stack = Components.stack.caller;
|
||||||
|
} else {
|
||||||
|
stack = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
stack = stack.substring(nl2Index, stack.length-1);
|
if (stack) {
|
||||||
message += "\n"+debugLine+stack;
|
message += '\n' + Zotero.Debug.stackToString(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_console) {
|
if (_console) {
|
||||||
|
@ -194,4 +203,20 @@ Zotero.Debug = new function () {
|
||||||
this.clear = function () {
|
this.clear = function () {
|
||||||
_output = [];
|
_output = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Format a stack trace for output in the same way that Error.stack does
|
||||||
|
* @param {Components.stack} stack
|
||||||
|
* @param {Integer} [lines=5] Number of lines to format
|
||||||
|
*/
|
||||||
|
this.stackToString = function (stack, lines) {
|
||||||
|
if (!lines) lines = 5;
|
||||||
|
var str = '';
|
||||||
|
while(stack && lines--) {
|
||||||
|
str += '\n ' + (stack.name || '') + '@' + stack.filename
|
||||||
|
+ ':' + stack.lineNumber;
|
||||||
|
stack = stack.caller;
|
||||||
|
}
|
||||||
|
return str.substr(1);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1217,10 +1217,21 @@ Components.utils.import("resource://gre/modules/osfile.jsm");
|
||||||
*
|
*
|
||||||
* Uses prefs e.z.debug.log and e.z.debug.level (restart required)
|
* Uses prefs e.z.debug.log and e.z.debug.level (restart required)
|
||||||
*
|
*
|
||||||
* Defaults to log level 3 if level not provided
|
* @param {} message
|
||||||
|
* @param {Integer} [level=3]
|
||||||
|
* @param {Boolean|Integer} [stack] Whether to display the calling stack.
|
||||||
|
* If true, stack is displayed starting from the caller. If an integer,
|
||||||
|
* that many stack levels will be omitted starting from the caller.
|
||||||
*/
|
*/
|
||||||
function debug(message, level) {
|
function debug(message, level, stack) {
|
||||||
Zotero.Debug.log(message, level);
|
// Account for this alias
|
||||||
|
if (stack === true) {
|
||||||
|
stack = 1;
|
||||||
|
} else if (stack >= 0) {
|
||||||
|
stack++;
|
||||||
|
}
|
||||||
|
|
||||||
|
Zotero.Debug.log(message, level, stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue