From 469e961dc8ae72032acd96f45ef7e59a09e78a8f Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Tue, 23 Jan 2018 14:39:20 -0500 Subject: [PATCH] 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" } } ``` --- src/common.js | 65 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 12 deletions(-) diff --git a/src/common.js b/src/common.js index 61eaf893..b246f258 100644 --- a/src/common.js +++ b/src/common.js @@ -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? @@ -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; @@ -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;