UNPKG

2.81 kBJavaScriptView Raw
1/*
2 * Nested color logging support for terminal
3 * @author: Hsiaoming Yang <lepture@me.com>
4 *
5 * Thanks to: https://github.com/lepture/terminal
6 *
7 */
8
9var util = require('util');
10var os = require('os');
11var EventEmitter = require('events').EventEmitter;
12var scheme = require('./scheme');
13var color = require('./color');
14var count = 0;
15
16function colorize(name, text) {
17 var func;
18 if (typeof name === 'string') {
19 func = color.color[name]
20 } else if (typeof name === 'function') {
21 func = name;
22 }
23 if (!func) return text;
24 return func(text);
25}
26
27
28var definedLevels = {
29 'debug': 1,
30 'info': 2,
31 'start': 2,
32 'end': 2,
33 'warn': 3,
34 'error': 4,
35 'silent': 5
36}
37
38function Logging(level) {
39 EventEmitter.call(this);
40 this.level = level || 'info';
41 this.indent = 0;
42 this.scheme = scheme.arrow;
43}
44util.inherits(Logging, EventEmitter);
45
46Logging.prototype.log = function() {
47 var level = arguments[0], msg, args;
48
49 if (arguments.length === 2 && definedLevels.hasOwnProperty(level)) {
50 args = Array.prototype.slice.call(arguments[1]);
51 } else if (definedLevels.hasOwnProperty(level)) {
52 args = Array.prototype.slice.call(arguments).slice(1);
53 } else {
54 level = 'info';
55 args = arguments;
56 }
57 msg = util.format.apply(this, args);
58
59 this.emit('logging-' + level, msg);
60 if (!definedLevels.hasOwnProperty(this.level)) return;
61 if (definedLevels[level] < definedLevels[this.level]) return;
62
63 var text = new Array(this.indent + 1).join(' ');
64 var levelScheme = this.scheme[level] || {};
65 text += (levelScheme.start || '');
66
67 if (levelScheme.color) {
68 text += colorize(levelScheme.color, msg);
69 } else {
70 text += msg;
71 }
72
73 text += (levelScheme.end || '');
74 if (level === 'error') {
75 process.stderr.write(text + os.EOL);
76 } else {
77 process.stdout.write(text + os.EOL);
78 }
79}
80Logging.prototype.start = function() {
81 this.log('start', arguments);
82 this.indent += 1;
83}
84Logging.prototype.end = function() {
85 this.log('end', arguments);
86 this.indent -= 1;
87}
88Logging.prototype.exit = function() {
89 this.indent = 1;
90 this.log('error', arguments);
91 process.exit(1);
92}
93Logging.prototype.debug = function() {
94 this.log('debug', arguments);
95}
96Logging.prototype.info = function() {
97 this.log('info', arguments);
98}
99Logging.prototype.warning = Logging.prototype.warn = function() {
100 this.log('warn', arguments);
101}
102Logging.prototype.error = function() {
103 this.log('error', arguments);
104}
105Logging.prototype.config = function(obj) {
106 if (obj.verbose) {
107 this.level = 'debug';
108 }
109 if (obj.quiet) {
110 this.level = 'warn';
111 }
112 if (obj.level && definedLevels.hasOwnProperty(obj.level)) {
113 this.level = obj.level;
114 }
115 if (definedLevels.hasOwnProperty(obj)) {
116 this.level = obj;
117 }
118};
119
120exports = module.exports = new Logging();
121exports.Logging = Logging