1 | 'use strict'
|
2 |
|
3 | chalk = require 'chalk'
|
4 | format = require './Format'
|
5 | ms = require 'pretty-ms'
|
6 | CONST = require './Constants'
|
7 |
|
8 | module.exports =
|
9 | print: ->
|
10 | for type of @types
|
11 | @transport @generateMessage type, message for message in @messages[type]
|
12 |
|
13 | decorateCounter: (counter) ->
|
14 | str = '' + counter
|
15 | n = CONST.DECORATE_COUNTER_ZERO_N - str.length
|
16 | return '0'.repeat(n) + str if n > 0
|
17 | str
|
18 |
|
19 | outputMessage: (message) -> message
|
20 |
|
21 | outputType: (type) ->
|
22 | if @keyword
|
23 | if @keyword is CONST.SYMBOL_KEYWORD
|
24 | type = then @types[type].symbol
|
25 | else
|
26 | type = @keyword
|
27 | type = type.toUpperCase() if @upperCase
|
28 | type
|
29 |
|
30 | outputAlign: ->
|
31 | return ' ' unless @align
|
32 | @align
|
33 |
|
34 | outputCounter: ->
|
35 | return '' unless @timestamp
|
36 |
|
37 | @_counterTimestamp ||= 0
|
38 | @_lastTimestamp ||= null
|
39 |
|
40 | diff = Date.now() - @_lastTimestamp
|
41 |
|
42 | if diff >= @timestamp
|
43 | ++@_counterTimestamp
|
44 | @_lastTimestamp = Date.now()
|
45 |
|
46 | " [#{@decorateCounter(@_counterTimestamp)}]"
|
47 |
|
48 | outputSeparator: (type) ->
|
49 | return '' if @keyword
|
50 | @types[type].separator or ''
|
51 |
|
52 | outputContext: ->
|
53 | unless @context then '' else " #{@context}"
|
54 |
|
55 | transport: console.log
|
56 |
|
57 | generateMessage: (type, message) ->
|
58 | return unless @isPrintable type
|
59 | colorType = @types[type].color
|
60 | message = @outputMessage message
|
61 | message = @colorizeMessage type, message
|
62 | diff = null
|
63 |
|
64 | if @diff
|
65 | if @diff[type]
|
66 | diff = ms(Date.now() - @diff[type])
|
67 | diff = " +#{diff}"
|
68 | @diff[type] = Date.now()
|
69 | else
|
70 | @diff[type] = Date.now()
|
71 | diff = " +0ms"
|
72 |
|
73 | messageType = @outputType type
|
74 | messageType = @colorize colorType, messageType
|
75 |
|
76 | separator = @outputSeparator(type)
|
77 |
|
78 | messageCounter = @outputCounter()
|
79 | messageCounter = @colorize CONST.LINE_COLOR, messageCounter
|
80 |
|
81 | messageContext = @outputContext()
|
82 | messageContext = @colorize CONST.LINE_COLOR, messageContext
|
83 |
|
84 | align = @outputAlign()
|
85 |
|
86 | output = "#{separator}#{messageType}#{messageCounter}#{messageContext}#{align}#{message}"
|
87 | output += @colorize colorType, diff if diff
|
88 | output
|
89 |
|
90 | generateTypeMessage: (type) ->
|
91 | (message...) ->
|
92 | color = @types[type].color
|
93 | message = @format message, color
|
94 | message = @generateMessage type, message
|
95 | @transport message if message
|
96 | this
|
97 |
|
98 | colorizeMessage: (type, message) ->
|
99 | return message unless @color
|
100 | lineColor = CONST.LINE_COLOR
|
101 | return @colorize lineColor, message if message.indexOf '=' is -1
|
102 | typeColor = @types[type].color
|
103 |
|
104 | message.toString().split(' ').map((msg) =>
|
105 | msg = msg.split '='
|
106 | if msg.length > 1
|
107 | msg[0] = @colorize typeColor, msg[0]
|
108 | msg[1] = @colorize lineColor, msg[1]
|
109 | msg.join @colorize lineColor, '='
|
110 | else
|
111 | @colorize lineColor, msg
|
112 | ).join(' ')
|
113 |
|
114 | colorize: (colors, message) ->
|
115 | return message unless @color
|
116 | colors = colors.split ' '
|
117 | stylize = chalk
|
118 | stylize = stylize[color] for color in colors
|
119 | stylize message
|
120 |
|
121 | isPrintable: (type) ->
|
122 | return true if @level is CONST.UNMUTED
|
123 | return false if @level is CONST.MUTED
|
124 | @types[type].level <= @types[@level].level
|
125 |
|
126 | format: (messages, color) ->
|
127 | opts = {@offset, @depth}
|
128 | applyFormatter = format opts
|
129 |
|
130 | messages.push color
|
131 | applyFormatter messages...
|
132 |
|
133 | align: " "
|
134 | color: true
|
135 | timestamp: 0
|
136 | offset: 2
|
137 | depth: Infinity
|
138 |
|
139 | level: CONST.UNMUTED
|
140 |
|
141 | types:
|
142 | debug:
|
143 | level : 4
|
144 | color : 'white'
|
145 | symbol: CONST.FIGURE.info
|
146 |
|
147 | info:
|
148 | level : 3
|
149 | color : 'blue'
|
150 | separator : ' '
|
151 | symbol : CONST.FIGURE.info
|
152 |
|
153 | warn:
|
154 | level : 2
|
155 | color : 'yellow'
|
156 | separator : ' '
|
157 | symbol : CONST.FIGURE.warning
|
158 |
|
159 | error:
|
160 | level : 1
|
161 | color : 'red'
|
162 | symbol : CONST.FIGURE.error
|
163 |
|
164 | fatal:
|
165 | level : 0
|
166 | color : 'red'
|
167 | symbol : CONST.FIGURE.error
|