Skip to content

Instantly share code, notes, and snippets.

@samrocksc
Last active April 19, 2019 18:58
Show Gist options
  • Select an option

  • Save samrocksc/2b9639c99538848be7bb80f99ca63f83 to your computer and use it in GitHub Desktop.

Select an option

Save samrocksc/2b9639c99538848be7bb80f99ca63f83 to your computer and use it in GitHub Desktop.

Revisions

  1. samrocksc revised this gist Apr 19, 2019. 1 changed file with 15 additions and 1 deletion.
    16 changes: 15 additions & 1 deletion debug w stack
    Original file line number Diff line number Diff line change
    @@ -51,4 +51,18 @@ const debug = {
    warning: msg => w(msg, buildStack()),
    };

    module.exports = debug;
    function bar() {
    debug.verbose('name->bar');
    baz();
    }

    const foo = thing => {
    debug.verbose('name->foo')
    thing();
    }

    const baz = () => {
    debug.verbose('name->baz');
    }

    foo(bar);
  2. samrocksc revised this gist Apr 19, 2019. 1 changed file with 13 additions and 13 deletions.
    26 changes: 13 additions & 13 deletions debug w stack
    Original file line number Diff line number Diff line change
    @@ -4,41 +4,41 @@ const w = require('debug')('warning');

    const buildStack = () => {
    const depth = 3;
    if (!this.__stack) {
    Object.defineProperty(this, '__stack', {
    if (!this.stackIs) {
    Object.defineProperty(this, 'stackIs', {
    get: function() {
    var orig = Error.prepareStackTrace;
    const orig = Error.prepareStackTrace;
    Error.prepareStackTrace = function(_, stack) {
    return stack;
    };
    var err = new Error();
    const err = new Error();
    Error.captureStackTrace(err, arguments.callee);
    var stack = err.stack;
    const stack = err.stack;
    Error.prepareStackTrace = orig;
    return stack;
    },
    });
    }

    if (!this.__line) {
    Object.defineProperty(this, '__line', {
    if (!this.lineIs) {
    Object.defineProperty(this, 'lineIs', {
    get: function() {
    return this.__stack[depth].getLineNumber();
    return this.stackIs[depth].getLineNumber();
    },
    });
    }

    if (!this.__function) {
    Object.defineProperty(this, '__function', {
    if (!this.functionIs) {
    Object.defineProperty(this, 'functionIs', {
    get: function() {
    return this.__stack[depth].getFunctionName();
    return this.stackIs[depth].getFunctionName();
    },
    });
    }

    return {
    function: this.__function,
    line: this.__line,
    function: this.functionIs,
    line: this.lineIs,
    };
    };

  3. samrocksc created this gist Apr 19, 2019.
    54 changes: 54 additions & 0 deletions debug w stack
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    const v = require('debug')('verbose');
    const e = require('debug')('error');
    const w = require('debug')('warning');

    const buildStack = () => {
    const depth = 3;
    if (!this.__stack) {
    Object.defineProperty(this, '__stack', {
    get: function() {
    var orig = Error.prepareStackTrace;
    Error.prepareStackTrace = function(_, stack) {
    return stack;
    };
    var err = new Error();
    Error.captureStackTrace(err, arguments.callee);
    var stack = err.stack;
    Error.prepareStackTrace = orig;
    return stack;
    },
    });
    }

    if (!this.__line) {
    Object.defineProperty(this, '__line', {
    get: function() {
    return this.__stack[depth].getLineNumber();
    },
    });
    }

    if (!this.__function) {
    Object.defineProperty(this, '__function', {
    get: function() {
    return this.__stack[depth].getFunctionName();
    },
    });
    }

    return {
    function: this.__function,
    line: this.__line,
    };
    };

    /**
    * base the functions to apply debugging
    */
    const debug = {
    verbose: msg => v(msg, buildStack()),
    error: msg => e(msg, buildStack()),
    warning: msg => w(msg, buildStack()),
    };

    module.exports = debug;