You need to do something like console.log()
in chrome, when the row number is displayed next to where this method was called.
|
3 answers
If I understand you correctly, try this (chrome only):
console.line = function (offset){ var halt; try { halt(); } catch( error ){ var info = error.stack.split('\n')[2 + ~~offset].match(/at\s+([^\s]+)\s+\((.+):(\d+):/); console.info('"'+info[1]+'": line '+info[3]+' in '+info[2]); } }; console.spy = function (ctx, name){ var fn = ctx[name]; ctx[name] = function (){ console.line(1); return fn.apply(ctx, arguments); }; }; // Π½Π°Π·Π²Π°Π½ΠΈΠ΅ ΠΌΠ΅ΡΠΎΠ΄Π°, Π½ΠΎΠΌΠ΅Ρ ΡΡΡΠΎΠΊΠΈ ΠΈ ΠΈΠΌΡ ΡΠ°ΠΉΠ»Π° console.line(); // Π½Π°Π·Π²Π°Π½ΠΈΠ΅ ΠΈ Π½ΠΎΠΌΠ΅Ρ ΡΡΡΠΎΠΊΠΈ ΠΌΠ΅ΡΠΎΠ΄Π°, ΠΊΠΎΡΠΎΡΡΠΉ Π²ΡΠ·Π²Π°Π» ΡΡΡ ΡΡΠ½ΠΊΡΠΈΡ console.spy(document, 'addEventListener');
Example - http://jsfiddle.net/qpqF9/
|
I think the easiest option is to throw an exception, and then parse the stacktrace:
console.getLine = function() { function getError() { try { throw new Error() } catch(err) { return err } } return parseInt(getError().stack.replace(/[\(|\)]/g, '').match(/:(\d+)(:\d+)?[\n]?$/g)[0].substr(1)); } alert(console.getLine());
Demo: http://jsfiddle.net/kLgMC/4/ (works in Chrome, Opera, Firefox and IE)
- bug jsfiddle.net/oceog/kLgMC/5 - zb '
- yes, indeed the output goes wrong when called in a separate method - Pavel Azanov
|
myLog((new Error).stack); function myLog(stack){ // ..parse Stack // return parse Stack }
|