123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
-
-
- 'use strict';
-
-
- var util = require('util');
-
-
- var hooker = require('hooker');
-
- var colors = require('colors');
-
- var _ = require('lodash');
- _.str = require('underscore.string');
- _.mixin(_.str.exports());
-
-
- var logUtils = require('grunt-legacy-log-utils');
-
- function Log(options) {
-
- this.always = this;
-
- this.options = _.extend({}, {
-
- color: true,
-
- verbose: false,
-
- debug: false,
-
- outStream: process.stdout,
-
-
-
- grunt: null,
-
- maxCols: null,
-
- muted: false,
- }, options);
-
- this.hasLogged = false;
-
-
- this.verbose = new VerboseLog(this, true);
- this.notverbose = new VerboseLog(this, false);
- this.verbose.or = this.notverbose;
- this.notverbose.or = this.verbose;
-
-
-
- if (this.options.grunt) {
- _.bindAll(this);
- _.bindAll(this.verbose);
- _.bindAll(this.notverbose);
- }
- }
- exports.Log = Log;
-
-
- function VerboseLog(parentLog, verbose) {
-
- this.always = parentLog;
-
- this._isVerbose = verbose;
- }
- util.inherits(VerboseLog, Log);
-
- VerboseLog.prototype._write = function() {
-
- if (Boolean(this.option('verbose')) !== this._isVerbose) { return; }
-
- return VerboseLog.super_.prototype._write.apply(this, arguments);
- };
-
-
-
- function makeSmartAccessor(name, isOption) {
- Object.defineProperty(Log.prototype, name, {
- enumerable: true,
- configurable: true,
- get: function() {
- return isOption ? this.always._options[name] : this.always['_' + name];
- },
- set: function(value) {
- if (isOption) {
- this.always._options[name] = value;
- } else {
- this.always['_' + name] = value;
- }
- },
- });
- }
- makeSmartAccessor('options');
- makeSmartAccessor('hasLogged');
- makeSmartAccessor('muted', true);
-
-
- Log.prototype.initColors = function() {
- if (this.option('no-color')) {
-
- colors.mode = 'none';
-
- hooker.hook(console, 'log', function() {
- var args = _.toArray(arguments);
- return hooker.filter(this, args.map(function(arg) {
- return typeof arg === 'string' ? colors.stripColors(arg) : arg;
- }));
- });
- }
- };
-
-
-
- Log.prototype.option = function(name) {
- if (this.options.grunt && this.options.grunt.option) {
- return this.options.grunt.option(name);
- }
- var no = name.match(/^no-(.+)$/);
- return no ? !this.options[no[1]] : this.options[name];
- };
-
-
- Log.prototype._markup = function(str) {
- str = str || '';
-
- str = str.replace(/(\s|^)_(\S|\S[\s\S]+?\S)_(?=[\s,.!?]|$)/g, '$1' + '$2'.underline);
-
- str = str.replace(/(\s|^)\*(\S|\S[\s\S]+?\S)\*(?=[\s,.!?]|$)/g, '$1' + '$2'.bold);
- return str;
- };
-
-
-
- Log.prototype._format = function(args) {
- args = _.toArray(args);
- if (args.length > 0) {
- args[0] = String(args[0]);
- }
- return util.format.apply(util, args);
- };
-
- Log.prototype._write = function(msg) {
-
- if (this.muted) { return; }
-
- this.hasLogged = true;
- msg = msg || '';
-
-
- if (this.option('no-color')) { msg = colors.stripColors(msg); }
-
- this.options.outStream.write(this._markup(msg));
- };
-
- Log.prototype._writeln = function(msg) {
-
- this._write((msg || '') + '\n');
- };
-
-
- Log.prototype.write = function() {
- this._write(this._format(arguments));
- return this;
- };
-
-
- Log.prototype.writeln = function() {
- this._writeln(this._format(arguments));
- return this;
- };
-
- Log.prototype.warn = function() {
- var msg = this._format(arguments);
- if (arguments.length > 0) {
- this._writeln('>> '.red + _.trim(msg).replace(/\n/g, '\n>> '.red));
- } else {
- this._writeln('ERROR'.red);
- }
- return this;
- };
- Log.prototype.error = function() {
- if (this.options.grunt && this.options.grunt.fail) {
- this.options.grunt.fail.errorcount++;
- }
- this.warn.apply(this, arguments);
- return this;
- };
- Log.prototype.ok = function() {
- var msg = this._format(arguments);
- if (arguments.length > 0) {
- this._writeln('>> '.green + _.trim(msg).replace(/\n/g, '\n>> '.green));
- } else {
- this._writeln('OK'.green);
- }
- return this;
- };
- Log.prototype.errorlns = function() {
- var msg = this._format(arguments);
- this.error(this.wraptext(this.options.maxCols || 77, msg));
- return this;
- };
- Log.prototype.oklns = function() {
- var msg = this._format(arguments);
- this.ok(this.wraptext(this.options.maxCols || 77, msg));
- return this;
- };
- Log.prototype.success = function() {
- var msg = this._format(arguments);
- this._writeln(msg.green);
- return this;
- };
- Log.prototype.fail = function() {
- var msg = this._format(arguments);
- this._writeln(msg.red);
- return this;
- };
- Log.prototype.header = function() {
- var msg = this._format(arguments);
-
- if (this.hasLogged) { this._writeln(); }
- this._writeln(msg.underline);
- return this;
- };
- Log.prototype.subhead = function() {
- var msg = this._format(arguments);
-
- if (this.hasLogged) { this._writeln(); }
- this._writeln(msg.bold);
- return this;
- };
-
- Log.prototype.debug = function() {
- var msg = this._format(arguments);
- if (this.option('debug')) {
- this._writeln('[D] ' + msg.magenta);
- }
- return this;
- };
-
-
- Log.prototype.writetableln = function(widths, texts) {
- this._writeln(this.table(widths, texts));
- return this;
- };
-
-
- Log.prototype.writelns = function() {
- var msg = this._format(arguments);
- this._writeln(this.wraptext(this.options.maxCols || 80, msg));
- return this;
- };
-
-
- Log.prototype.writeflags = function(obj, prefix) {
- var wordlist;
- if (Array.isArray(obj)) {
- wordlist = this.wordlist(obj);
- } else if (typeof obj === 'object' && obj) {
- wordlist = this.wordlist(Object.keys(obj).map(function(key) {
- var val = obj[key];
- return key + (val === true ? '' : '=' + JSON.stringify(val));
- }));
- }
- this._writeln((prefix || 'Flags') + ': ' + (wordlist || '(none)'.cyan));
- return this;
- };
-
-
- [
- 'wordlist',
- 'uncolor',
- 'wraptext',
- 'table',
- ].forEach(function(prop) {
- Log.prototype[prop] = exports[prop] = logUtils[prop];
- });
|