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 color = require('./color')
|
13 | var count = 0
|
14 |
|
15 | var levels = {
|
16 | 'debug': 10,
|
17 | 'info': 20,
|
18 | 'warn': 30,
|
19 | 'error': 40,
|
20 | 'log': 50
|
21 | }
|
22 |
|
23 | var colors = {
|
24 | debug: color.grey,
|
25 | info: color.green,
|
26 | warn: color.yellow,
|
27 | error:color.red
|
28 | }
|
29 | var icons = {
|
30 | logIcon: color.cyan('➠ '),
|
31 | startIcon: color.bold(color.magenta('⚑ ')),
|
32 | endIcon: color.cyan('➥ ')
|
33 | }
|
34 |
|
35 | function log(context, level, args) {
|
36 | if (levels[context.level] > levels[level]) return
|
37 |
|
38 | var text = ''
|
39 | var stream = process.stdout
|
40 | text += Array(count + 1).join(' ')
|
41 | if (count) {
|
42 | text += icons.logIcon
|
43 | }
|
44 | if (level === 'warn') {
|
45 | text += color.yellow_bg(' WARN ') + ' '
|
46 | } else if (level === 'error') {
|
47 | text += color.red_bg(' ERROR ') + ' '
|
48 | stream = process.stderr
|
49 | }
|
50 | var colorize = colors[level]
|
51 | if (colorize) {
|
52 | text += colorize(util.format.apply(context, args)) + os.EOL
|
53 | } else {
|
54 | text += util.format.apply(context, args) + os.EOL
|
55 | }
|
56 | stream.write(text)
|
57 | }
|
58 |
|
59 | function Logging(level) {
|
60 | this.level = level || 'info'
|
61 | }
|
62 | Logging.prototype.__proto__ = EventEmitter.prototype
|
63 | Logging.prototype.start = function() {
|
64 | if (levels[this.level] > 25) return;
|
65 |
|
66 | var text = Array(count + 1).join(' ')
|
67 | text += icons.startIcon
|
68 | text += color.bold(util.format.apply(this, arguments)) + os.EOL
|
69 | process.stdout.write(text)
|
70 | count += 1
|
71 | this.emit('logging-start')
|
72 | }
|
73 | Logging.prototype.end = function() {
|
74 | if (levels[this.level] > 25) return;
|
75 | var text = ''
|
76 | text += Array(count + 1).join(' ')
|
77 | if (count) {
|
78 | text += icons.endIcon
|
79 | }
|
80 | text += util.format.apply(this, arguments) + os.EOL
|
81 | process.stdout.write(text)
|
82 | count -= 1
|
83 | this.emit('logging-end')
|
84 | }
|
85 | Logging.prototype.log = function() {
|
86 | log(this, 'log', arguments)
|
87 | }
|
88 | Logging.prototype.debug = function() {
|
89 | log(this, 'debug', arguments)
|
90 | }
|
91 | Logging.prototype.info = function() {
|
92 | log(this, 'info', arguments)
|
93 | }
|
94 | Logging.prototype.warn = function() {
|
95 | log(this, 'warn', arguments)
|
96 | this.emit('logging-warn')
|
97 | }
|
98 | Logging.prototype.error = function() {
|
99 | log(this, 'error', arguments)
|
100 | this.emit('logging-error')
|
101 | }
|
102 | Logging.prototype.config = function(obj) {
|
103 | if (obj.verbose) {
|
104 | this.level = 'debug'
|
105 | }
|
106 | if (obj.quiet) {
|
107 | this.level = 'warn'
|
108 | }
|
109 | if (obj.level) {
|
110 | this.level = obj.level
|
111 | }
|
112 | if (obj in colors) {
|
113 | this.level = obj
|
114 | }
|
115 | if (obj.colors) {
|
116 | for (var key in obj.colors) {
|
117 | (function(key) {
|
118 | colors[key] = obj.colors[key]
|
119 | })(key)
|
120 | }
|
121 | }
|
122 | if (obj.icons) {
|
123 | for (var key in obj.icons) {
|
124 | (function(key) {
|
125 | icons[key] = obj.icons[key]
|
126 | })(key)
|
127 | }
|
128 | }
|
129 | }
|
130 |
|
131 | module.exports = Logging
|