Comments (27)
👍 if we can get correct line numbers with debug
in the browser log ;)
from debug.
I started on a possible fix for this #273.
from debug.
You mean in the web browser?
from debug.
yes
from debug.
Ya... I'm not sure if there's a way around this. We may be able to fix it in V8 using the stack trace API, but don't expect anything cross-browser.
from debug.
well, I only care about Android WebView (2.2 ~ 4.4+)
from debug.
I found angular.$log
got the same problem, angular/angular.js#2975
is there any workaround like that ? bind
from debug.
I tried out the bind
technique but the problem is that we do our own processing of the given arguments before calling the bound log
function, so even still, it just points to the debug code rather than the user code that called the function.
I'm still pondering this but I'm not sure if there's a fix still...
from debug.
Yes I noticed that too.
I've used bows before, and it seemed to show correct line numbers in the browser. I'm not sure what they are doing differently?
from debug.
@nicolashery Thanks for pointing to bows! It is logging correct line numbers. But I m not sure it's features complements 'debug.js' features, though.
from debug.
The issue is that logs are deferred, do they really need to be? Can we just make deferring an optional argument flag?
from debug.
The issue is that logs are deferred, do they really need to be? Can we just make deferring an optional argument flag?
What makes you think that?
from debug.
Isn't that what the stack traces shows? If I could just jump halfway up the stack trace and figure out where I called the log it would be fine. Or is it wrapped in a try catch? I don't remember, I created this issue #2975 over a year ago.
from debug.
It really just wraps the console.log
call. It's not deferred using setTimeout
or anything like that.
The problem is that the console will only display the call-site of the console.log() call, which always happens in the same place in debug, as opposed to the call-site of the debug()
call, which is what we're actually interested in. Unfortunately I haven't come up with a solution for that.
from debug.
@TooTallNate I meant they used to be deferred and executed in batch (like part of the $scope.$apply() stuff) This may have changed or I may have been confused from poking around the src code/stack trace, etc.
Can't we just move up the stack trace to see where the debug() was called? I mean if that worked it wouldn't be an issue, but from my recollection that just isn't the case.
from debug.
Ya AFAIK they've never been deferred.
Can't we just move up the stack trace to see where the debug() was called? I mean if that worked it wouldn't be an issue, but from my recollection that just isn't the case.
console.log()
doesn't give you a trace though. You could switch to console.error()
to get a trace, but then all the logs calls are red.
from debug.
There is console.trace
and it produces debug formatted messages (with colors and so on) and trace stack. Works in chrome, firefox and ie 11. Here is demo:
The one bad thing here — stack traces are not collapsed by default and log window is harder to read.
from debug.
I use a browserify transform that overrides the console.log() and inserts the line number. Works pretty well for my purposes, except my regex doesn't handle all cases correctly.
This also automates enabling of each file included in my browserify bundle (related to #154).
var through = require('through2');
var _ = require('underscore');
module.exports = function(file) {
return through(function(buf, enc, next) {
var parts = file.split('/');
var filename = parts[parts.length - 1];
if (filename === 'index.js') {
filename = parts[parts.length - 2] + '-dir';
}
var bufString = buf.toString('utf8');
var lines = bufString.split(/\r\n|\r|\n/);
_.each(lines, function(line, i) {
if (line.match('console') && file.match('app')) {
var regExp = /\((.*)\)/;
var insideParens = regExp.exec(line)[1];
var lineNumber = '"Line ' + String(i + 1) + '"';
lines[i] = line.replace('(' + insideParens + ')', '(' + insideParens + ',' + lineNumber + ')');
}
});
if (file.match('app')) {
var string = "var console = {log: require('debug')('" + filename + "')}\n\n" + lines.join("\r\n");
this.push(string);
}
else {
this.push(lines.join("\r\n"));
}
next();
});
}
from debug.
That's pretty cool Nate :)
On Fri, Feb 20, 2015 at 10:27 AM, Truong Nguyen [email protected]
wrote:
[image: 👍] if we can get correct line numbers with debug in the
browser log ;)—
Reply to this email directly or view it on GitHub
#105 (comment).
from debug.
In Chrome you can now have the correct file and line number by blackboxing 'debug.js' script in the DevTool;
https://gist.github.com/paulirish/c307a5a585ddbcc17242
Only (quite annoying) problem is that it doesn't work if the script you blackbox is bundled together in a single file with your code. I hope chrome improves this behaviour in the future.
Meanwhile it might be a good ides to expose a standalone build, since this solution won't work if you require 'debug.js' in your code.
from debug.
@wildermuthn interesting code of yours. care to make it a npm package?
@TooTallNate yeah, this is tricky, tough problem to crack. any success at all yet?
from debug.
correction: bows does not seem to preserve line number
from debug.
that's a compromise i can live with for now:
// workaround: since we cannot overwrite console.log without having the correct file and line number
// we'll use groupCollapsed() and trace() instead to get these.
function debug() {
console.groupCollapsed(arguments)
console.trace('Trace')
console.groupEnd()
}
from debug.
I will have to correct @binarykitchen, we just switched to bows because it indeed does indeed show the line number.
We'd actually prefer debug, but alas, for now we'll stay with bows.
👍 for this issue.
from debug.
We have an internal module we created to log stuff, it reveals the line in the source code it logged from.
Policy prevents me from dumping the source code, but I can clue you into how it's done for webkit/blink and ie:
isFunction (func)->
return typeof func is 'function'
class Logger
methods: [
'log', 'error', 'debug', 'trace'
]
constructor: ->
(@bind(method) for method in @methods)
bind: (method)->
target = window.console[method]
if isFunction(target) && isFunction(target.bind)
@[method] = target.bind console
else
@[method] = Function.prototype.bind.call target, console
debug = new Logger().log
debug 'module start'
So the reason why debug will never show the source line number is because the exported function we all use after setting a namespace isn't a rebound function of console.log
. It is instead a wrapper around some other complex logic wherein console.log
is called with your arguments (which is why you see debug.js:117
).
Showing line numbers from source, will require a complete rewrite.
from debug.
I just tried #273. It works.
from debug.
feel free to discuss further here. I think this should be something we handle with V3 #370
from debug.
Related Issues (20)
- debug with dotenv HOT 3
- INEFFICIENT REGULAR EXPRESSION COMPLEXITY IN DEBUG HOT 1
- [Question] How do I enable millisecond diff?
- Issue in Next.js HOT 5
- Redirect logs to file doesnt capture time HOT 2
- Different formatting for namespaces on TTY and files
- Specify peer dependency version
- Specify namespace with url in ESM HOT 5
- npm ERR! code ENOENT npm ERR! syscall open npm ERR! path C:\Users\sahar\Desktop\myCode\jonas_React_course\React_Jonas_20_10_2023\project_pizza-menu\package.json npm ERR! errno -4058 npm ERR! enoent Could not read package.json: Error: ENOENT: no such file or directory, open 'C:\Users\sahar\Desktop\myCode\jonas_React_course\React_Jonas_20_10_2023\project_pizza-menu\package.json' npm ERR! enoent This is related to npm not being able to find a file. npm ERR! enoent HOT 1
- Debug mixed with console.log HOT 6
- Debug Checkmarx Vulnerability HOT 1
- Avoid throwing errors
- useColors return false after modify user-agent in tauri app HOT 1
- Colour lost when using Node native test running (Node 18+) HOT 2
- Alternate solution to CWE-1333 | Inefficient Regular Expression Complexity
- Display full error object all the time regardless of argument position
- DEBUG_HIDE_DATE doesn't exist / do anything
- Property 'type' does not exist on type 'Process'.
- How to set max array length (`maxArrayLength`)? HOT 1
- update colors for darkmode in devtools
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from debug.