UNPKG

yadsil

Version:

Yet Another Dead-SImple Log on stderr for CLI tools.

91 lines (74 loc) 1.94 kB
'use strict'; var Log, colors, util; require('source-map-support').install(); util = require('util'); colors = require('colors'); Log = (function() { function Log(appName) { this.appName = appName; this.isTTY = process.stderr.isTTY; this.stderr = process.stderr; this.exitFunc = process.exit; this.haveColor = this.isTTY; this.isVerbose = false; } Log.prototype.verbose = function(value) { if (value == null) { return this.isVerbose; } return this.isVerbose = value; }; Log.prototype.color = function(value) { if (value == null) { return this.haveColor; } return this.haveColor = value; }; Log.prototype.any = function(args, level, color) { var message; message = util.format.apply(null, args); if (!this.isTTY) { this.stderr.write("" + this.appName + ": "); } if (level) { if (color && this.haveColor) { this.stderr.write("" + level[color] + ": "); } else { this.stderr.write("" + level + ": "); } } return this.stderr.write("" + message + "\n"); }; Log.prototype.fatal = function(errorCode) { this.any([].slice.apply(arguments).slice(1), 'fatal error', 'red'); return this.exitFunc(errorCode); }; Log.prototype.error = function() { return this.any(arguments, 'error', 'red'); }; Log.prototype.warning = function() { return this.any(arguments, 'warning', 'yellow'); }; Log.prototype.notice = function() { return this.any(arguments, 'notice', 'green'); }; Log.prototype.info = function() { if (!this.isVerbose) { return; } return this.any(arguments); }; Log.prototype.debug = function() { if (!this.isVerbose) { return; } return this.any(arguments, 'debug', 'magenta'); }; return Log; })(); module.exports = function(appName) { return new Log(appName); }; /* //@ sourceMappingURL=yadsil.js.map */