GithubHelp home page GithubHelp logo

Comments (27)

constantx avatar constantx commented on May 17, 2024 1

👍 if we can get correct line numbers with debug in the browser log ;)

from debug.

fijiwebdesign avatar fijiwebdesign commented on May 17, 2024 1

I started on a possible fix for this #273.

from debug.

TooTallNate avatar TooTallNate commented on May 17, 2024

You mean in the web browser?

from debug.

atian25 avatar atian25 commented on May 17, 2024

yes

from debug.

TooTallNate avatar TooTallNate commented on May 17, 2024

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.

atian25 avatar atian25 commented on May 17, 2024

well, I only care about Android WebView (2.2 ~ 4.4+)

from debug.

atian25 avatar atian25 commented on May 17, 2024

I found angular.$log got the same problem, angular/angular.js#2975
is there any workaround like that ? bind

from debug.

TooTallNate avatar TooTallNate commented on May 17, 2024

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.

nicolashery avatar nicolashery commented on May 17, 2024

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.

manikantag avatar manikantag commented on May 17, 2024

@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.

ProLoser avatar ProLoser commented on May 17, 2024

The issue is that logs are deferred, do they really need to be? Can we just make deferring an optional argument flag?

from debug.

TooTallNate avatar TooTallNate commented on May 17, 2024

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.

ProLoser avatar ProLoser commented on May 17, 2024

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.

TooTallNate avatar TooTallNate commented on May 17, 2024

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.

ProLoser avatar ProLoser commented on May 17, 2024

@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.

TooTallNate avatar TooTallNate commented on May 17, 2024

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.

b1rdex avatar b1rdex commented on May 17, 2024

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:
image
The one bad thing here — stack traces are not collapsed by default and log window is harder to read.

from debug.

wildermuthn avatar wildermuthn commented on May 17, 2024

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();
  });
}

screenshot

from debug.

TooTallNate avatar TooTallNate commented on May 17, 2024

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.

AxelDelmas avatar AxelDelmas commented on May 17, 2024

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.

binarykitchen avatar binarykitchen commented on May 17, 2024

@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.

binarykitchen avatar binarykitchen commented on May 17, 2024

correction: bows does not seem to preserve line number

from debug.

binarykitchen avatar binarykitchen commented on May 17, 2024

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.

d-simon avatar d-simon commented on May 17, 2024

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.

airtonix avatar airtonix commented on May 17, 2024

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.

neojp avatar neojp commented on May 17, 2024

I just tried #273. It works.

from debug.

thebigredgeek avatar thebigredgeek commented on May 17, 2024

feel free to discuss further here. I think this should be something we handle with V3 #370

from debug.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.