Una buona (e semplice) soluzione come sottolineato nei commenti sulla domanda iniziale è quello di utilizzare la proprietà stack
di un oggetto Error
in questo modo:
function stackTrace() {
var err = new Error();
return err.stack;
}
Questa volontà uscita generare in questo modo:
[email protected]://localhost:49573/assets/js/scripts.js:44
[email protected]://localhost:49573/assets/js/scripts.js:9
[email protected]://localhost:49573/:462
x.Callbacks/[email protected]://localhost:49573/assets/js/jquery-1.10.2.min.js:4
x.Callbacks/[email protected]://localhost:49573/assets/js/jquery-1.10.2.min.js:4
[email protected]://localhost:49573/assets/js/jquery-1.10.2.min.js:6
.send/[email protected]://localhost:49573/assets/js/jquery-1.10.2.min.js:6
Dare il nome della funzione di chiamata con l'URL e numero di riga, la sua funzione di chiamata, e così sopra.
Ho una soluzione davvero elaborata e carina che ho ideato per un progetto al momento sto lavorando e l'ho estratta e rielaborata un po 'per essere generalizzata. Eccolo:
(function(context){
// Only global namespace.
var Console = {
//Settings
settings: {
debug: {
alwaysShowURL: false,
enabled: true,
showInfo: true
},
stackTrace: {
enabled: true,
collapsed: true,
ignoreDebugFuncs: true,
spacing: false
}
}
};
// String formatting prototype function.
if (!String.prototype.format) {
String.prototype.format = function() {
var s = this.toString(),
args = typeof arguments[0],
args = (("string" == args || "number" == args) ? arguments : arguments[0]);
if (!arguments.length)
return s;
for (arg in args)
s = s.replace(RegExp("\\{" + arg + "\\}", "gi"), args[arg]);
return s;
}
}
// String repeating prototype function.
if (!String.prototype.times) {
String.prototype.times = function() {
var s = this.toString(),
tempStr = "",
times = arguments[0];
if (!arguments.length)
return s;
for (var i = 0; i < times; i++)
tempStr += s;
return tempStr;
}
}
// Commonly used functions
Console.debug = function() {
if (Console.settings.debug.enabled) {
var args = ((typeof arguments !== 'undefined') ? Array.prototype.slice.call(arguments, 0) : []),
sUA = navigator.userAgent,
currentBrowser = {
firefox: /firefox/gi.test(sUA),
webkit: /webkit/gi.test(sUA),
},
aLines = Console.stackTrace().split("\n"),
aCurrentLine,
iCurrIndex = ((currentBrowser.webkit) ? 3 : 2),
sCssBlack = "color:black;",
sCssFormat = "color:{0}; font-weight:bold;",
sLines = "";
if (currentBrowser.firefox)
aCurrentLine = aLines[iCurrIndex].replace(/(.*):/, "[email protected]").split("@");
else if (currentBrowser.webkit)
aCurrentLine = aLines[iCurrIndex].replace("at ", "").replace(")", "").replace(/(\()/gi, "@").replace(/(.*):(\d*):(\d*)/, "[email protected][email protected]$3").split("@");
// Show info if the setting is true and there's no extra trace (would be kind of pointless).
if (Console.settings.debug.showInfo && !Console.settings.stackTrace.enabled) {
var sFunc = aCurrentLine[0].trim(),
sURL = aCurrentLine[1].trim(),
sURL = ((!Console.settings.debug.alwaysShowURL && context.location.href == sURL) ? "this page" : sURL),
sLine = aCurrentLine[2].trim(),
sCol;
if (currentBrowser.webkit)
sCol = aCurrentLine[3].trim();
console.info("%cOn line %c{0}%c{1}%c{2}%c of %c{3}%c inside the %c{4}%c function:".format(sLine, ((currentBrowser.webkit) ? ", column " : ""), ((currentBrowser.webkit) ? sCol : ""), sURL, sFunc),
sCssBlack, sCssFormat.format("red"),
sCssBlack, sCssFormat.format("purple"),
sCssBlack, sCssFormat.format("green"),
sCssBlack, sCssFormat.format("blue"),
sCssBlack);
}
// If the setting permits, get rid of the two obvious debug functions (Console.debug and Console.stackTrace).
if (Console.settings.stackTrace.ignoreDebugFuncs) {
// In WebKit (Chrome at least), there's an extra line at the top that says "Error" so adjust for this.
if (currentBrowser.webkit)
aLines.shift();
aLines.shift();
aLines.shift();
}
sLines = aLines.join(((Console.settings.stackTrace.spacing) ? "\n\n" : "\n")).trim();
trace = typeof trace !== 'undefined' ? trace : true;
if (typeof console !== "undefined") {
for (var arg in args)
console.debug(args[arg]);
if (Console.settings.stackTrace.enabled) {
var sCss = "color:red; font-weight: bold;",
sTitle = "%c Stack Trace" + " ".times(70);
if (Console.settings.stackTrace.collapsed)
console.groupCollapsed(sTitle, sCss);
else
console.group(sTitle, sCss);
console.debug("%c" + sLines, "color: #666666; font-style: italic;");
console.groupEnd();
}
}
}
}
Console.stackTrace = function() {
var err = new Error();
return err.stack;
}
context.Console = Console;
})(window);
Check it out sul GitHub (attualmente v1.2)! Puoi usarlo come Console.debug("Whatever");
e, a seconda delle impostazioni in Console
, stampare l'output e una traccia dello stack (o semplicemente informazioni semplici/niente di più). Ecco un esempio:
Assicurarsi di giocare con le impostazioni nell'oggetto Console
! È possibile aggiungere una spaziatura tra le linee della traccia e disattivarla completamente. Qui è con Console.trace
insieme a false
:
È anche possibile disattivare il primo bit di informazioni indicato (impostato Console.settings.debug.showInfo
-false
) o disattivare il debug del tutto (impostare Console.settings.debug.enabled
a false
) in modo da non dover commenta di nuovo una dichiarazione di debug! Lasciali entrare e questo non farà nulla.
Basta google per la traccia dello stack javascript. Riceverai la tua risposta! Eccone uno particolarmente interessante: http://helephant.com/2007/05/diy-javascript-stack-trace/ –
possibile duplicato di [trace di eccezioni JavaScript] (http://stackoverflow.com/questions/147891/javascript- exception-stack-trace) – ripper234
Bug è ancora aperto su Firebug bug tracker dal 2008: http://code.google.com/p/fbug/issues/detail?id=1260 - lo star! –