开发者

Getting firebug console to display different line numbers

I have wrapped calls to firebugs console.log in a logging function (that checks for existance of cons开发者_如何学Goole along with other flags)

for example:

Log(string) { if (console && DEBUG) console.log(string); }

my issue is that the firebug console shows the line number of the console.log function call rather then the Log function call.

Is there any way to change what line numbers firebug shows?


Firebug does not allow you to change the line number on the console via code.


console.trace() will give you the call stack.

See http://getfirebug.com/logging for more info.


I took a little different approach and just defined a property that sets up a __stack__ variable by intentionally throwing an error, from this we can get the filename, line number (and many others such as callers and their line numbers if you choose to implement them).

Also rather than setting up a log function, I set it up as a variable also, but one that gets logged when set. This will be the location where LOG is set, not where it was defined.

Its as simple as LOG="my message". Then you can use it later as a variable to get the location of your last debug with alert(LOG)

/*@const*/ //for closure-compiler
DEBUG=2 // 0=off, 1=msg:file:line:column, 2=msg:stack-trace
if(DEBUG){

/*@const @constructor*/
Object.defineProperty(window,'__stack__',{get:function(){
    try{_ფ_()}catch(e){return e.stack.split(":")}
}})

/*@const @constructor*/
Object.defineProperty(window,'__file__',{get:function(){
    var s=__stack__,l=s.length
    return (isNaN(s[l-2]))?s[l-2]:s[l-3]
}})

/*@const @constructor*/
Object.defineProperty(window,'__line__',{get:function(){
    var s=__stack__,l=s.length
    return (isNaN(s[l-2]))?s[l-1]:s[l-2]
}})

/*@const @constructor*/
Object.defineProperty(window,'__col__',{get:function(){
    var s=__stack__,l=s.length
    return (isNaN(s[l-2]))?"NA":s[l-1]
}})

/*@const @constructor*/
Object.defineProperty(window,'LOG',{
    get:function(){return out},
    set:function(msg){if(DEBUG>1)out=msg+"\t-\t"+__stack__
        else out=msg+" in file:"+__file__+" @ Line:"+__line__+", Column:"+__col__
        console.log(out)}
})
}//end if(DEBUG)


There doesn't seem to be a way to do that, so I have been using console.group

console.group(file +' (line '+ line +')');
console.log(data);
console.groupEnd();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜