Skip to content

Commit

Permalink
introduce debug.unique()
Browse files Browse the repository at this point in the history
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"
  }
}
```
  • Loading branch information
indutny committed Jan 23, 2018
1 parent 22f9932 commit 469e961
Showing 1 changed file with 53 additions and 12 deletions.
65 changes: 53 additions & 12 deletions src/common.js
Expand Up @@ -65,6 +65,24 @@ module.exports = function setup(env) {

function createDebug(namespace) {
var prevTime;
var lastId = 0;

function prepareArgs() {
// turn the `arguments` into a proper Array
var args = new Array(arguments.length);
for (var i = 0; i < args.length; i++) {
args[i] = arguments[i];
}

args[0] = createDebug.coerce(args[0]);

if ('string' !== typeof args[0]) {
// anything else let's inspect with %O
args.unshift('%O');
}

return args;
}

function debug() {
// disabled?
Expand All @@ -80,18 +98,7 @@ module.exports = function setup(env) {
self.curr = curr;
prevTime = curr;

// turn the `arguments` into a proper Array
var args = new Array(arguments.length);
for (var i = 0; i < args.length; i++) {
args[i] = arguments[i];
}

args[0] = createDebug.coerce(args[0]);

if ('string' !== typeof args[0]) {
// anything else let's inspect with %O
args.unshift('%O');
}
var args = prepareArgs.apply(self, arguments);

// apply any `formatters` transformations
var index = 0;
Expand All @@ -118,11 +125,45 @@ module.exports = function setup(env) {
logFn.apply(self, args);
}

function unique (format) {
var uniqueId = null;

format = format ? format + ' ' : '%d ';

function subdebug () {
// disabled?
if (!debug.enabled) return;

var self = debug;

var args = prepareArgs.apply(self, arguments);

args[0] = format + args[0];
args.splice(1, 0, id());

return debug.apply(self, args);
}

function id () {
if (!debug.enabled) return;

if (uniqueId === null)
uniqueId = lastId++;

return uniqueId;
}

subdebug.id = id;

return subdebug;
}

debug.namespace = namespace;
debug.enabled = createDebug.enabled(namespace);
debug.useColors = createDebug.useColors();
debug.color = selectColor(namespace);
debug.destroy = destroy;
debug.unique = unique;
//debug.formatArgs = formatArgs;
//debug.rawLog = rawLog;

Expand Down

0 comments on commit 469e961

Please sign in to comment.