1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | var util = require('util');
|
10 | var os = require('os');
|
11 | var EventEmitter = require('events').EventEmitter;
|
12 | var scheme = require('./scheme');
|
13 | var color = require('./color');
|
14 | var count = 0;
|
15 |
|
16 | function 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 |
|
28 | var definedLevels = {
|
29 | 'debug': 1,
|
30 | 'info': 2,
|
31 | 'start': 2,
|
32 | 'end': 2,
|
33 | 'warn': 3,
|
34 | 'error': 4,
|
35 | 'silent': 5
|
36 | }
|
37 |
|
38 | function Logging(level) {
|
39 | EventEmitter.call(this);
|
40 | this.level = level || 'info';
|
41 | this.indent = 0;
|
42 | this.scheme = scheme.arrow;
|
43 | }
|
44 | util.inherits(Logging, EventEmitter);
|
45 |
|
46 | Logging.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 | }
|
80 | Logging.prototype.start = function() {
|
81 | this.log('start', arguments);
|
82 | this.indent += 1;
|
83 | }
|
84 | Logging.prototype.end = function() {
|
85 | this.log('end', arguments);
|
86 | this.indent -= 1;
|
87 | }
|
88 | Logging.prototype.debug = function() {
|
89 | this.log('debug', arguments);
|
90 | }
|
91 | Logging.prototype.info = function() {
|
92 | this.log('info', arguments);
|
93 | }
|
94 | Logging.prototype.warn = function() {
|
95 | this.log('warn', arguments);
|
96 | }
|
97 | Logging.prototype.error = function() {
|
98 | this.log('error', arguments);
|
99 | }
|
100 | Logging.prototype.config = function(obj) {
|
101 | if (obj.verbose) {
|
102 | this.level = 'debug';
|
103 | }
|
104 | if (obj.quiet) {
|
105 | this.level = 'warn';
|
106 | }
|
107 | if (obj.level && definedLevels.hasOwnProperty(obj.level)) {
|
108 | this.level = obj.level;
|
109 | }
|
110 | if (definedLevels.hasOwnProperty(obj)) {
|
111 | this.level = obj;
|
112 | }
|
113 | };
|
114 |
|
115 | exports = module.exports = new Logging();
|
116 | exports.Logging = Logging
|