Nav apraksta

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * grunt
  3. * http://gruntjs.com/
  4. *
  5. * Copyright (c) 2014 "Cowboy" Ben Alman
  6. * Licensed under the MIT license.
  7. * https://github.com/gruntjs/grunt/blob/master/LICENSE-MIT
  8. */
  9. 'use strict';
  10. // Nodejs libs.
  11. var util = require('util');
  12. // External libs.
  13. var hooker = require('hooker');
  14. // Requiring this here modifies the String prototype!
  15. var colors = require('colors');
  16. // The upcoming lodash 2.5+ should remove the need for underscore.string.
  17. var _ = require('lodash');
  18. _.str = require('underscore.string');
  19. _.mixin(_.str.exports());
  20. // TODO: ADD CHALK
  21. var logUtils = require('grunt-legacy-log-utils');
  22. function Log(options) {
  23. // This property always refers to the "base" logger.
  24. this.always = this;
  25. // Extend options.
  26. this.options = _.extend({}, {
  27. // Show colors in output?
  28. color: true,
  29. // Enable verbose-mode logging?
  30. verbose: false,
  31. // Enable debug logging statement?
  32. debug: false,
  33. // Where should messages be output?
  34. outStream: process.stdout,
  35. // NOTE: the color, verbose, debug options will be ignored if the
  36. // "grunt" option is specified! See the Log.prototype.option and
  37. // the Log.prototype.error methods for more info.
  38. grunt: null,
  39. // Where should output wrap? If null, use legacy Grunt defaults.
  40. maxCols: null,
  41. // Should logger start muted?
  42. muted: false,
  43. }, options);
  44. // True once anything has actually been logged.
  45. this.hasLogged = false;
  46. // Related verbose / notverbose loggers.
  47. this.verbose = new VerboseLog(this, true);
  48. this.notverbose = new VerboseLog(this, false);
  49. this.verbose.or = this.notverbose;
  50. this.notverbose.or = this.verbose;
  51. // Apparently, people have using grunt.log in interesting ways. Just bind
  52. // all methods so that "this" is irrelevant.
  53. if (this.options.grunt) {
  54. _.bindAll(this);
  55. _.bindAll(this.verbose);
  56. _.bindAll(this.notverbose);
  57. }
  58. }
  59. exports.Log = Log;
  60. // Am I doing it wrong? :P
  61. function VerboseLog(parentLog, verbose) {
  62. // Keep track of the original, base "Log" instance.
  63. this.always = parentLog;
  64. // This logger is either verbose (true) or notverbose (false).
  65. this._isVerbose = verbose;
  66. }
  67. util.inherits(VerboseLog, Log);
  68. VerboseLog.prototype._write = function() {
  69. // Abort if not in correct verbose mode.
  70. if (Boolean(this.option('verbose')) !== this._isVerbose) { return; }
  71. // Otherwise... log!
  72. return VerboseLog.super_.prototype._write.apply(this, arguments);
  73. };
  74. // Create read/write accessors that prefer the parent log's properties (in
  75. // the case of verbose/notverbose) to the current log's properties.
  76. function makeSmartAccessor(name, isOption) {
  77. Object.defineProperty(Log.prototype, name, {
  78. enumerable: true,
  79. configurable: true,
  80. get: function() {
  81. return isOption ? this.always._options[name] : this.always['_' + name];
  82. },
  83. set: function(value) {
  84. if (isOption) {
  85. this.always._options[name] = value;
  86. } else {
  87. this.always['_' + name] = value;
  88. }
  89. },
  90. });
  91. }
  92. makeSmartAccessor('options');
  93. makeSmartAccessor('hasLogged');
  94. makeSmartAccessor('muted', true);
  95. // Disable colors if --no-colors was passed.
  96. Log.prototype.initColors = function() {
  97. if (this.option('no-color')) {
  98. // String color getters should just return the string.
  99. colors.mode = 'none';
  100. // Strip colors from strings passed to console.log.
  101. hooker.hook(console, 'log', function() {
  102. var args = _.toArray(arguments);
  103. return hooker.filter(this, args.map(function(arg) {
  104. return typeof arg === 'string' ? colors.stripColors(arg) : arg;
  105. }));
  106. });
  107. }
  108. };
  109. // Check for color, verbose, debug options through Grunt if specified,
  110. // otherwise defer to options object properties.
  111. Log.prototype.option = function(name) {
  112. if (this.options.grunt && this.options.grunt.option) {
  113. return this.options.grunt.option(name);
  114. }
  115. var no = name.match(/^no-(.+)$/);
  116. return no ? !this.options[no[1]] : this.options[name];
  117. };
  118. // Parse certain markup in strings to be logged.
  119. Log.prototype._markup = function(str) {
  120. str = str || '';
  121. // Make _foo_ underline.
  122. str = str.replace(/(\s|^)_(\S|\S[\s\S]+?\S)_(?=[\s,.!?]|$)/g, '$1' + '$2'.underline);
  123. // Make *foo* bold.
  124. str = str.replace(/(\s|^)\*(\S|\S[\s\S]+?\S)\*(?=[\s,.!?]|$)/g, '$1' + '$2'.bold);
  125. return str;
  126. };
  127. // Similar to util.format in the standard library, however it'll always
  128. // convert the first argument to a string and treat it as the format string.
  129. Log.prototype._format = function(args) {
  130. args = _.toArray(args);
  131. if (args.length > 0) {
  132. args[0] = String(args[0]);
  133. }
  134. return util.format.apply(util, args);
  135. };
  136. Log.prototype._write = function(msg) {
  137. // Abort if muted.
  138. if (this.muted) { return; }
  139. // Actually write output.
  140. this.hasLogged = true;
  141. msg = msg || '';
  142. // Users should probably use the colors-provided methods, but if they
  143. // don't, this should strip extraneous color codes.
  144. if (this.option('no-color')) { msg = colors.stripColors(msg); }
  145. // Actually write to stdout.
  146. this.options.outStream.write(this._markup(msg));
  147. };
  148. Log.prototype._writeln = function(msg) {
  149. // Write blank line if no msg is passed in.
  150. this._write((msg || '') + '\n');
  151. };
  152. // Write output.
  153. Log.prototype.write = function() {
  154. this._write(this._format(arguments));
  155. return this;
  156. };
  157. // Write a line of output.
  158. Log.prototype.writeln = function() {
  159. this._writeln(this._format(arguments));
  160. return this;
  161. };
  162. Log.prototype.warn = function() {
  163. var msg = this._format(arguments);
  164. if (arguments.length > 0) {
  165. this._writeln('>> '.red + _.trim(msg).replace(/\n/g, '\n>> '.red));
  166. } else {
  167. this._writeln('ERROR'.red);
  168. }
  169. return this;
  170. };
  171. Log.prototype.error = function() {
  172. if (this.options.grunt && this.options.grunt.fail) {
  173. this.options.grunt.fail.errorcount++;
  174. }
  175. this.warn.apply(this, arguments);
  176. return this;
  177. };
  178. Log.prototype.ok = function() {
  179. var msg = this._format(arguments);
  180. if (arguments.length > 0) {
  181. this._writeln('>> '.green + _.trim(msg).replace(/\n/g, '\n>> '.green));
  182. } else {
  183. this._writeln('OK'.green);
  184. }
  185. return this;
  186. };
  187. Log.prototype.errorlns = function() {
  188. var msg = this._format(arguments);
  189. this.error(this.wraptext(this.options.maxCols || 77, msg));
  190. return this;
  191. };
  192. Log.prototype.oklns = function() {
  193. var msg = this._format(arguments);
  194. this.ok(this.wraptext(this.options.maxCols || 77, msg));
  195. return this;
  196. };
  197. Log.prototype.success = function() {
  198. var msg = this._format(arguments);
  199. this._writeln(msg.green);
  200. return this;
  201. };
  202. Log.prototype.fail = function() {
  203. var msg = this._format(arguments);
  204. this._writeln(msg.red);
  205. return this;
  206. };
  207. Log.prototype.header = function() {
  208. var msg = this._format(arguments);
  209. // Skip line before header, but not if header is the very first line output.
  210. if (this.hasLogged) { this._writeln(); }
  211. this._writeln(msg.underline);
  212. return this;
  213. };
  214. Log.prototype.subhead = function() {
  215. var msg = this._format(arguments);
  216. // Skip line before subhead, but not if subhead is the very first line output.
  217. if (this.hasLogged) { this._writeln(); }
  218. this._writeln(msg.bold);
  219. return this;
  220. };
  221. // For debugging.
  222. Log.prototype.debug = function() {
  223. var msg = this._format(arguments);
  224. if (this.option('debug')) {
  225. this._writeln('[D] ' + msg.magenta);
  226. }
  227. return this;
  228. };
  229. // Write a line of a table.
  230. Log.prototype.writetableln = function(widths, texts) {
  231. this._writeln(this.table(widths, texts));
  232. return this;
  233. };
  234. // Wrap a long line of text.
  235. Log.prototype.writelns = function() {
  236. var msg = this._format(arguments);
  237. this._writeln(this.wraptext(this.options.maxCols || 80, msg));
  238. return this;
  239. };
  240. // Display flags in verbose mode.
  241. Log.prototype.writeflags = function(obj, prefix) {
  242. var wordlist;
  243. if (Array.isArray(obj)) {
  244. wordlist = this.wordlist(obj);
  245. } else if (typeof obj === 'object' && obj) {
  246. wordlist = this.wordlist(Object.keys(obj).map(function(key) {
  247. var val = obj[key];
  248. return key + (val === true ? '' : '=' + JSON.stringify(val));
  249. }));
  250. }
  251. this._writeln((prefix || 'Flags') + ': ' + (wordlist || '(none)'.cyan));
  252. return this;
  253. };
  254. // Add static methods.
  255. [
  256. 'wordlist',
  257. 'uncolor',
  258. 'wraptext',
  259. 'table',
  260. ].forEach(function(prop) {
  261. Log.prototype[prop] = exports[prop] = logUtils[prop];
  262. });