node-debug-tool
Advanced tools
Comparing version 0.1.4 to 0.1.6
@@ -5,3 +5,10 @@ 'use strict'; | ||
var worker2 = require('../lib/node-debug-tool')('worker 2'); | ||
var ab = require('../lib/node-debug-tool')('Foo:bar:x'); | ||
var abc = require('../lib/node-debug-tool')('foo:bar:y'); | ||
var abcd = require('../lib/node-debug-tool')('foo:abc'); | ||
ab.log('Some foo bar x'); | ||
abc.log('Some foo bar y'); | ||
abcd.log('Some foo abc'); | ||
worker1.error('Some Error'); | ||
@@ -8,0 +15,0 @@ worker1.warning('Some warning'); |
@@ -8,80 +8,123 @@ /** | ||
function NodeDebug (workerName) { | ||
var colors = { red: 1, green: 2, yellow: 3 }; | ||
var printPrefix = { error: '[Error]', warning: '[Warning]', debug: '[Debug]' }; | ||
var allowedNamespace = process.env.DEBUG_MODE; | ||
var isTTY = process.stdout.isTTY, | ||
currentTick = false , | ||
tickBetween = 0, | ||
colors = { red: 1, green: 2, yellow: 3 }, | ||
printPrefix = { error: '[Error]', warning: '[Warning]', debug: '[Debug]' }; | ||
/** | ||
* Namespaces to show | ||
*/ | ||
var matchNamespaces = function(name) { | ||
if (typeof process.env.DEBUG_MODE === 'undefined') { | ||
return false; | ||
} | ||
// Format worker name | ||
workerName = workerName.slice(0, 1).toUpperCase() + | ||
workerName.slice(1).toLowerCase(); | ||
if (allowedNamespace == '*') { | ||
return true; | ||
} | ||
/* Logging methods */ | ||
var error = function () { | ||
generateOutput(arguments, printPrefix.error, colors.red); | ||
}; | ||
if (name.length < allowedNamespace.length) { | ||
return false; | ||
} | ||
var warning = function () { | ||
generateOutput(arguments, printPrefix.warning, colors.yellow); | ||
}; | ||
var checkedNamespace = name.toLowerCase().split(':'); | ||
var namespaces = allowedNamespace.toLowerCase().split(':'); | ||
var debug = function () { | ||
generateOutput(arguments, printPrefix.debug, colors.green); | ||
}; | ||
for (var j= 0,l=namespaces.length; j<l; ++j) { | ||
if (j == namespaces.length || namespaces[j] != checkedNamespace[j]) { | ||
break; | ||
} | ||
} | ||
/* Ticking timer */ | ||
var tickTimer = function () { | ||
var curr = +new Date(); | ||
tickBetween = (currentTick === false) | ||
? 0 : | ||
curr - currentTick; | ||
currentTick = curr; | ||
}; | ||
if (checkedNamespace.length == j && j == namespaces.length) { | ||
return true; | ||
} | ||
/* Output generation */ | ||
var generateOutput = function (args, prefix, color) { | ||
if (process.env.DEBUG_MODE != true) { | ||
return; | ||
if (checkedNamespace.length >= namespaces.length) { | ||
if (namespaces[j] == '*') { | ||
return true; | ||
} | ||
} | ||
var time = '', | ||
prefixColor = '', | ||
resetColor = '', | ||
date = new Date(); | ||
return false; | ||
}; | ||
function NodeDebug (workerName) { | ||
if (!(this instanceof NodeDebug)) { | ||
return new NodeDebug(workerName); | ||
} | ||
// using color on TTY, otherwise using UTCString prefix | ||
if (isTTY) { | ||
prefixColor = '\u001b[3' + color + 'm'; | ||
resetColor = '\u001b[0m'; | ||
} else { | ||
time = '(' + date.toUTCString() + ')' + ' '; | ||
this.isTTY = process.stdout.isTTY; | ||
this.currentTick = false; | ||
this.tickBetween = 0; | ||
this.workerName = ''; | ||
var splitName = workerName.split(':'); | ||
splitName.forEach(function(e, i) { | ||
this.workerName += e.slice(0, 1).toUpperCase() | ||
+ e.slice(1).toLowerCase(); | ||
if (i < splitName.length-1) { | ||
this.workerName += ':'; | ||
} | ||
}.bind(this)); | ||
} | ||
tickTimer(); | ||
/* Logging methods */ | ||
NodeDebug.prototype.error = function () { | ||
this.generateOutput(arguments, printPrefix.error, colors.red); | ||
}; | ||
var string = ''; | ||
for (var i= 0,l=args.length; i<l; ++i) { | ||
if (args[i] instanceof Error === false) { | ||
string += JSON.stringify(args[i], null, 4) + ' '; | ||
} | ||
NodeDebug.prototype.warning = function () { | ||
this.generateOutput(arguments, printPrefix.warning, colors.yellow); | ||
}; | ||
NodeDebug.prototype.log = function () { | ||
this.generateOutput(arguments, printPrefix.debug, colors.green); | ||
}; | ||
/* Ticking timer */ | ||
NodeDebug.prototype.tickTimer = function () { | ||
var curr = +new Date(); | ||
this.tickBetween = (this.currentTick === false) | ||
? 0 : | ||
curr - this.currentTick; | ||
this.currentTick = curr; | ||
}; | ||
/* Output generation */ | ||
NodeDebug.prototype.generateOutput = function (args, prefix, color) { | ||
if (! matchNamespaces(this.workerName)) { | ||
return; | ||
} | ||
var time = '', | ||
prefixColor = '', | ||
resetColor = '', | ||
date = new Date(); | ||
// using color on TTY, otherwise using UTCString prefix | ||
if (this.isTTY) { | ||
prefixColor = '\u001b[3' + color + 'm'; | ||
resetColor = '\u001b[0m'; | ||
} else { | ||
time = '(' + date.toUTCString() + ')' + ' '; | ||
} | ||
this.tickTimer(); | ||
var string = ''; | ||
for (var i= 0,l=args.length; i<l; ++i) { | ||
if (!(args[i] instanceof Error)) { | ||
string += JSON.stringify(args[i], null, 4) + ' '; | ||
} | ||
} | ||
console.log( | ||
time + '['+workerName+'] ' + | ||
console.log( | ||
time + '['+this.workerName+'] ' + | ||
prefixColor + prefix + ' ' + | ||
string + resetColor + ' ' + | ||
'+' + tickBetween + 'ms' | ||
); | ||
}; | ||
'+' + this.tickBetween + 'ms' | ||
); | ||
}; | ||
return { | ||
error: error, | ||
warning: warning, | ||
log: debug | ||
}; | ||
} | ||
module.exports = NodeDebug; | ||
})(); |
{ | ||
"name": "node-debug-tool", | ||
"version": "0.1.4", | ||
"version": "0.1.6", | ||
"description": "Node debug tool", | ||
@@ -5,0 +5,0 @@ "main": "./lib/node-debug-tool.js", |
@@ -18,3 +18,3 @@ # Node Debug Tool | ||
## Usage | ||
The `DEBUG_MODE` environment variable, must be set to 1 in order to see the deubbing information. | ||
The `DEBUG_MODE` environment variable, must be set to a namespace in order to see the debugging information. | ||
@@ -24,21 +24,40 @@ *Code example:* | ||
var worker1 = require('../lib/node-debug-tool')('worker 1'); | ||
var worker2 = require('../lib/node-debug-tool')('worker 2'); | ||
worker1.error('Some Error'); | ||
worker1.warning('Some warning'); | ||
worker1.log('Some logging', 'some more args', 'unlimited'); | ||
worker2.error('Foo'); | ||
worker2.warning('Bar'); | ||
worker2.log('Foo bar'); | ||
setInterval(function () { | ||
worker2.log('Some Foo Bar Log'); | ||
}, 2000); | ||
var ab = require('../lib/node-debug-tool')('Foo:bar:x'); | ||
var abc = require('../lib/node-debug-tool')('foo:bar:y'); | ||
var abcd = require('../lib/node-debug-tool')('foo:abc'); | ||
var abcde = require('../lib/node-debug-tool')('other'); | ||
ab.log('Some foo bar x'); | ||
abc.log('Some foo bar y'); | ||
abcd.log('Some foo abc'); | ||
other.log('Some other'); | ||
``` | ||
*Usage & Output:* | ||
The code above possible outputs: | ||
1. in case of running with `DEBUG_MODE=*`: | ||
``` | ||
[Foo:Bar:X] [Debug] "Some foo bar x" +0ms | ||
[Foo:Bar:Y] [Debug] "Some foo bar y" +0ms | ||
[Foo:Abc] [Debug] "Some foo abc" +0ms | ||
[Other] [Debug] "Some foo abc" +0ms | ||
``` | ||
![alt tag](http://i57.tinypic.com/2ez3brl.jpg) | ||
2. in case of running with `DEBUG_MODE=foo:*`: | ||
``` | ||
[Foo:Bar:X] [Debug] "Some foo bar x" +0ms | ||
[Foo:Bar:Y] [Debug] "Some foo bar y" +0ms | ||
[Foo:Abc] [Debug] "Some foo abc" +0ms | ||
``` | ||
3. in case of running with `DEBUG_MODE=foo:bar:*`: | ||
``` | ||
[Foo:Bar:X] [Debug] "Some foo bar x" +0ms | ||
[Foo:Bar:Y] [Debug] "Some foo bar y" +0ms | ||
``` | ||
4. in case of running with `DEBUG_MODE=foo:bar:x`: | ||
``` | ||
[Foo:Bar:X] [Debug] "Some foo bar x" +0ms | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
29580
157
62