node-debug-tool
Advanced tools
Comparing version 0.1.8 to 0.1.9
@@ -0,119 +1,114 @@ | ||
'use strict'; | ||
var colors = { red: 1, green: 2, yellow: 3 }; | ||
var printPrefix = { error: '[Error]', warning: '[Warning]', debug: '[Debug]' }; | ||
var allowedNamespace = process.env.DEBUG_MODE; | ||
/** | ||
* NodeDebug Tool | ||
* Node package for debugging | ||
* Visible namespaces, using the DEBUG_MODE environment variable | ||
* in order to determine which namespace should be visible | ||
*/ | ||
(function() { | ||
'use strict'; | ||
var matchNamespaces = function(name) { | ||
if (typeof process.env.DEBUG_MODE === 'undefined' || | ||
name.length < allowedNamespace.length) { | ||
return false; | ||
} | ||
var colors = { red: 1, green: 2, yellow: 3 }; | ||
var printPrefix = { error: '[Error]', warning: '[Warning]', debug: '[Debug]' }; | ||
var allowedNamespace = process.env.DEBUG_MODE; | ||
if (allowedNamespace === '*') { | ||
return true; | ||
} | ||
/** | ||
* Namespaces to show | ||
*/ | ||
var matchNamespaces = function(name) { | ||
if (typeof process.env.DEBUG_MODE === 'undefined' || | ||
name.length < allowedNamespace.length) { | ||
return false; | ||
} | ||
var checkedNamespace = name.toLowerCase().split(':'); | ||
var namespaces = allowedNamespace.toLowerCase().split(':'); | ||
if (allowedNamespace === '*') { | ||
return true; | ||
for (var j= 0,l=namespaces.length; j<l; ++j) { | ||
if (j == namespaces.length || | ||
namespaces[j] != checkedNamespace[j]) { | ||
break; | ||
} | ||
} | ||
var checkedNamespace = name.toLowerCase().split(':'); | ||
var namespaces = allowedNamespace.toLowerCase().split(':'); | ||
return (checkedNamespace.length == j && j == namespaces.length || | ||
(checkedNamespace.length >= namespaces.length && namespaces[j] === '*')); | ||
}; | ||
for (var j= 0,l=namespaces.length; j<l; ++j) { | ||
if (j == namespaces.length || | ||
namespaces[j] != checkedNamespace[j]) { | ||
break; | ||
} | ||
} | ||
function NodeDebug (workerName) { | ||
if (!(this instanceof NodeDebug)) { | ||
return new NodeDebug(workerName); | ||
} | ||
return (checkedNamespace.length == j && j == namespaces.length || | ||
(checkedNamespace.length >= namespaces.length && namespaces[j] === '*')); | ||
}; | ||
function NodeDebug (workerName) { | ||
if (!(this instanceof NodeDebug)) { | ||
return new NodeDebug(workerName); | ||
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)); | ||
this.isTTY = process.stdout.isTTY; | ||
this.currentTick = false; | ||
this.tickBetween = 0; | ||
this.matchNamespace = matchNamespaces(this.workerName); | ||
} | ||
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)); | ||
/* Logging methods */ | ||
NodeDebug.prototype.error = function () { | ||
this.generateOutput(arguments, printPrefix.error, colors.red); | ||
}; | ||
this.matchNamespace = matchNamespaces(this.workerName); | ||
} | ||
NodeDebug.prototype.warning = function () { | ||
this.generateOutput(arguments, printPrefix.warning, colors.yellow); | ||
}; | ||
/* Logging methods */ | ||
NodeDebug.prototype.error = function () { | ||
this.generateOutput(arguments, printPrefix.error, colors.red); | ||
}; | ||
NodeDebug.prototype.log = function () { | ||
this.generateOutput(arguments, printPrefix.debug, colors.green); | ||
}; | ||
NodeDebug.prototype.warning = function () { | ||
this.generateOutput(arguments, printPrefix.warning, colors.yellow); | ||
}; | ||
/* Ticking timer */ | ||
NodeDebug.prototype.tickTimer = function () { | ||
var curr = +new Date(); | ||
this.tickBetween = (this.currentTick === false) | ||
? 0 : | ||
curr - this.currentTick; | ||
this.currentTick = curr; | ||
}; | ||
NodeDebug.prototype.log = function () { | ||
this.generateOutput(arguments, printPrefix.debug, colors.green); | ||
}; | ||
/* Output generation */ | ||
NodeDebug.prototype.generateOutput = function (args, prefix, color) { | ||
if (! this.matchNamespace) { | ||
return; | ||
} | ||
var time = '', | ||
prefixColor = '', | ||
resetColor = '', | ||
date = new Date(); | ||
/* Ticking timer */ | ||
NodeDebug.prototype.tickTimer = function () { | ||
var curr = +new Date(); | ||
this.tickBetween = (this.currentTick === false) | ||
? 0 : | ||
curr - this.currentTick; | ||
this.currentTick = curr; | ||
}; | ||
// using color on TTY, otherwise using UTCString prefix | ||
if (this.isTTY) { | ||
prefixColor = '\u001b[3' + color + 'm'; | ||
resetColor = '\u001b[0m'; | ||
} else { | ||
time = '(' + date.toUTCString() + ')' + ' '; | ||
} | ||
/* Output generation */ | ||
NodeDebug.prototype.generateOutput = function (args, prefix, color) { | ||
if (! this.matchNamespace) { | ||
return; | ||
} | ||
var time = '', | ||
prefixColor = '', | ||
resetColor = '', | ||
date = new Date(); | ||
this.tickTimer(); | ||
// using color on TTY, otherwise using UTCString prefix | ||
if (this.isTTY) { | ||
prefixColor = '\u001b[3' + color + 'm'; | ||
resetColor = '\u001b[0m'; | ||
} else { | ||
time = '(' + date.toUTCString() + ')' + ' '; | ||
var string = ''; | ||
for (var i= 0,l=args.length; i<l; ++i) { | ||
if (!(args[i] instanceof Error)) { | ||
string += JSON.stringify(args[i], null, 4) + ' '; | ||
} | ||
} | ||
this.tickTimer(); | ||
console.log( | ||
time + '['+this.workerName+'] ' + | ||
prefixColor + prefix + ' ' + | ||
string + resetColor + ' ' + | ||
'+' + this.tickBetween + 'ms' | ||
); | ||
}; | ||
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 + '['+this.workerName+'] ' + | ||
prefixColor + prefix + ' ' + | ||
string + resetColor + ' ' + | ||
'+' + this.tickBetween + 'ms' | ||
); | ||
}; | ||
module.exports = NodeDebug; | ||
})(); | ||
module.exports = NodeDebug; |
{ | ||
"name": "node-debug-tool", | ||
"version": "0.1.8", | ||
"version": "0.1.9", | ||
"description": "Node debug tool", | ||
@@ -5,0 +5,0 @@ "main": "./lib/node-debug-tool.js", |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30868
145