Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce debug.unique() #544

Closed
wants to merge 1 commit into from
Closed

Commits on Jan 23, 2018

  1. introduce debug.unique()

    When debugging the applications that work with pools of streams
    or sockets, the debug stream often comes out interleaved making it
    impossible to distinguish between streams. It is a common knowledge
    that unique ids could be assigned to each stream and be used in
    logging to solve this. However, this solution is rather ad-hoc and
    usually applied only during manual debugging.
    
    Introduce `debug.unique([ format ])` method that returns function with
    the same signature as `debug`. This function however prepends either
    `'%d '` or `format + ' '` (depending on the presence of `format`) to the
    first argument of original `debug` function, and supplies unique
    integer as a second argument, shifting the rest to the right.
    
    Example:
    
    ```
    const debug = require('debug')('ns');
    
    class Instance {
      constructor() {
        this.debug = debug.unique('id=%d ');
      }
    
      method() {
        this.debug('method data=%j', {});
        // "id=123 method data={}"
      }
    
      attach(other) {
        this.debug('attach to=%d', other.debug.id());
        // "id=123 attach to=456"
      }
    }
    ```
    indutny committed Jan 23, 2018
    Configuration menu
    Copy the full SHA
    469e961 View commit details
    Browse the repository at this point in the history