UNPKG

11.1 kBJavaScriptView Raw
1/**
2 * Isomorphic logging module with support for colors!
3 *
4 * @module logging
5 */
6
7import * as env from './environment.js'
8import * as symbol from './symbol.js'
9import * as pair from './pair.js'
10import * as dom from './dom.js'
11import * as json from './json.js'
12import * as map from './map.js'
13import * as eventloop from './eventloop.js'
14import * as math from './math.js'
15import * as time from './time.js'
16import * as func from './function.js'
17
18export const BOLD = symbol.create()
19export const UNBOLD = symbol.create()
20export const BLUE = symbol.create()
21export const GREY = symbol.create()
22export const GREEN = symbol.create()
23export const RED = symbol.create()
24export const PURPLE = symbol.create()
25export const ORANGE = symbol.create()
26export const UNCOLOR = symbol.create()
27
28/**
29 * @type {Object<Symbol,pair.Pair<string,string>>}
30 */
31const _browserStyleMap = {
32 [BOLD]: pair.create('font-weight', 'bold'),
33 [UNBOLD]: pair.create('font-weight', 'normal'),
34 [BLUE]: pair.create('color', 'blue'),
35 [GREEN]: pair.create('color', 'green'),
36 [GREY]: pair.create('color', 'grey'),
37 [RED]: pair.create('color', 'red'),
38 [PURPLE]: pair.create('color', 'purple'),
39 [ORANGE]: pair.create('color', 'orange'), // not well supported in chrome when debugging node with inspector - TODO: deprecate
40 [UNCOLOR]: pair.create('color', 'black')
41}
42
43const _nodeStyleMap = {
44 [BOLD]: '\u001b[1m',
45 [UNBOLD]: '\u001b[2m',
46 [BLUE]: '\x1b[34m',
47 [GREEN]: '\x1b[32m',
48 [GREY]: '\u001b[37m',
49 [RED]: '\x1b[31m',
50 [PURPLE]: '\x1b[35m',
51 [ORANGE]: '\x1b[38;5;208m',
52 [UNCOLOR]: '\x1b[0m'
53}
54
55/* istanbul ignore next */
56/**
57 * @param {Array<string|Symbol|Object|number>} args
58 * @return {Array<string|object|number>}
59 */
60const computeBrowserLoggingArgs = args => {
61 const strBuilder = []
62 const styles = []
63 const currentStyle = map.create()
64 /**
65 * @type {Array<string|Object|number>}
66 */
67 let logArgs = []
68 // try with formatting until we find something unsupported
69 let i = 0
70
71 for (; i < args.length; i++) {
72 const arg = args[i]
73 // @ts-ignore
74 const style = _browserStyleMap[arg]
75 if (style !== undefined) {
76 currentStyle.set(style.left, style.right)
77 } else {
78 if (arg.constructor === String || arg.constructor === Number) {
79 const style = dom.mapToStyleString(currentStyle)
80 if (i > 0 || style.length > 0) {
81 strBuilder.push('%c' + arg)
82 styles.push(style)
83 } else {
84 strBuilder.push(arg)
85 }
86 } else {
87 break
88 }
89 }
90 }
91
92 if (i > 0) {
93 // create logArgs with what we have so far
94 logArgs = styles
95 logArgs.unshift(strBuilder.join(''))
96 }
97 // append the rest
98 for (; i < args.length; i++) {
99 const arg = args[i]
100 if (!(arg instanceof Symbol)) {
101 logArgs.push(arg)
102 }
103 }
104 return logArgs
105}
106
107/**
108 * @param {Array<string|Symbol|Object|number>} args
109 * @return {Array<string|object|number>}
110 */
111const computeNodeLoggingArgs = args => {
112 const strBuilder = []
113 const logArgs = []
114
115 // try with formatting until we find something unsupported
116 let i = 0
117
118 for (; i < args.length; i++) {
119 const arg = args[i]
120 // @ts-ignore
121 const style = _nodeStyleMap[arg]
122 if (style !== undefined) {
123 strBuilder.push(style)
124 } else {
125 if (arg.constructor === String || arg.constructor === Number) {
126 strBuilder.push(arg)
127 } else {
128 break
129 }
130 }
131 }
132 if (i > 0) {
133 // create logArgs with what we have so far
134 strBuilder.push('\x1b[0m')
135 logArgs.push(strBuilder.join(''))
136 }
137 // append the rest
138 for (; i < args.length; i++) {
139 const arg = args[i]
140 /* istanbul ignore else */
141 if (!(arg instanceof Symbol)) {
142 logArgs.push(arg)
143 }
144 }
145 return logArgs
146}
147
148/* istanbul ignore next */
149const computeLoggingArgs = env.isNode ? computeNodeLoggingArgs : computeBrowserLoggingArgs
150
151/**
152 * @param {Array<string|Symbol|Object|number>} args
153 */
154export const print = (...args) => {
155 console.log(...computeLoggingArgs(args))
156 /* istanbul ignore next */
157 vconsoles.forEach(vc => vc.print(args))
158}
159
160/* istanbul ignore next */
161/**
162 * @param {Array<string|Symbol|Object|number>} args
163 */
164export const warn = (...args) => {
165 console.warn(...computeLoggingArgs(args))
166 args.unshift(ORANGE)
167 vconsoles.forEach(vc => vc.print(args))
168}
169
170/* istanbul ignore next */
171/**
172 * @param {Error} err
173 */
174export const printError = err => {
175 console.error(err)
176 vconsoles.forEach(vc => vc.printError(err))
177}
178
179/* istanbul ignore next */
180/**
181 * @param {string} url image location
182 * @param {number} height height of the image in pixel
183 */
184export const printImg = (url, height) => {
185 if (env.isBrowser) {
186 console.log('%c ', `font-size: ${height}px; background-size: contain; background-repeat: no-repeat; background-image: url(${url})`)
187 // console.log('%c ', `font-size: ${height}x; background: url(${url}) no-repeat;`)
188 }
189 vconsoles.forEach(vc => vc.printImg(url, height))
190}
191
192/* istanbul ignore next */
193/**
194 * @param {string} base64
195 * @param {number} height
196 */
197export const printImgBase64 = (base64, height) => printImg(`data:image/gif;base64,${base64}`, height)
198
199/**
200 * @param {Array<string|Symbol|Object|number>} args
201 */
202export const group = (...args) => {
203 console.group(...computeLoggingArgs(args))
204 /* istanbul ignore next */
205 vconsoles.forEach(vc => vc.group(args))
206}
207
208/**
209 * @param {Array<string|Symbol|Object|number>} args
210 */
211export const groupCollapsed = (...args) => {
212 console.groupCollapsed(...computeLoggingArgs(args))
213 /* istanbul ignore next */
214 vconsoles.forEach(vc => vc.groupCollapsed(args))
215}
216
217export const groupEnd = () => {
218 console.groupEnd()
219 /* istanbul ignore next */
220 vconsoles.forEach(vc => vc.groupEnd())
221}
222
223/* istanbul ignore next */
224/**
225 * @param {function():Node} createNode
226 */
227export const printDom = createNode =>
228 vconsoles.forEach(vc => vc.printDom(createNode()))
229
230/* istanbul ignore next */
231/**
232 * @param {HTMLCanvasElement} canvas
233 * @param {number} height
234 */
235export const printCanvas = (canvas, height) => printImg(canvas.toDataURL(), height)
236
237export const vconsoles = new Set()
238
239/* istanbul ignore next */
240/**
241 * @param {Array<string|Symbol|Object|number>} args
242 * @return {Array<Element>}
243 */
244const _computeLineSpans = args => {
245 const spans = []
246 const currentStyle = new Map()
247 // try with formatting until we find something unsupported
248 let i = 0
249 for (; i < args.length; i++) {
250 const arg = args[i]
251 // @ts-ignore
252 const style = _browserStyleMap[arg]
253 if (style !== undefined) {
254 currentStyle.set(style.left, style.right)
255 } else {
256 if (arg.constructor === String || arg.constructor === Number) {
257 // @ts-ignore
258 const span = dom.element('span', [pair.create('style', dom.mapToStyleString(currentStyle))], [dom.text(arg)])
259 if (span.innerHTML === '') {
260 span.innerHTML = '&nbsp;'
261 }
262 spans.push(span)
263 } else {
264 break
265 }
266 }
267 }
268 // append the rest
269 for (; i < args.length; i++) {
270 let content = args[i]
271 if (!(content instanceof Symbol)) {
272 if (content.constructor !== String && content.constructor !== Number) {
273 content = ' ' + json.stringify(content) + ' '
274 }
275 spans.push(dom.element('span', [], [dom.text(/** @type {string} */ (content))]))
276 }
277 }
278 return spans
279}
280
281const lineStyle = 'font-family:monospace;border-bottom:1px solid #e2e2e2;padding:2px;'
282
283/* istanbul ignore next */
284export class VConsole {
285 /**
286 * @param {Element} dom
287 */
288 constructor (dom) {
289 this.dom = dom
290 /**
291 * @type {Element}
292 */
293 this.ccontainer = this.dom
294 this.depth = 0
295 vconsoles.add(this)
296 }
297
298 /**
299 * @param {Array<string|Symbol|Object|number>} args
300 * @param {boolean} collapsed
301 */
302 group (args, collapsed = false) {
303 eventloop.enqueue(() => {
304 const triangleDown = dom.element('span', [pair.create('hidden', collapsed), pair.create('style', 'color:grey;font-size:120%;')], [dom.text('▼')])
305 const triangleRight = dom.element('span', [pair.create('hidden', !collapsed), pair.create('style', 'color:grey;font-size:125%;')], [dom.text('▶')])
306 const content = dom.element('div', [pair.create('style', `${lineStyle};padding-left:${this.depth * 10}px`)], [triangleDown, triangleRight, dom.text(' ')].concat(_computeLineSpans(args)))
307 const nextContainer = dom.element('div', [pair.create('hidden', collapsed)])
308 const nextLine = dom.element('div', [], [content, nextContainer])
309 dom.append(this.ccontainer, [nextLine])
310 this.ccontainer = nextContainer
311 this.depth++
312 // when header is clicked, collapse/uncollapse container
313 dom.addEventListener(content, 'click', event => {
314 nextContainer.toggleAttribute('hidden')
315 triangleDown.toggleAttribute('hidden')
316 triangleRight.toggleAttribute('hidden')
317 })
318 })
319 }
320
321 /**
322 * @param {Array<string|Symbol|Object|number>} args
323 */
324 groupCollapsed (args) {
325 this.group(args, true)
326 }
327
328 groupEnd () {
329 eventloop.enqueue(() => {
330 if (this.depth > 0) {
331 this.depth--
332 // @ts-ignore
333 this.ccontainer = this.ccontainer.parentElement.parentElement
334 }
335 })
336 }
337
338 /**
339 * @param {Array<string|Symbol|Object|number>} args
340 */
341 print (args) {
342 eventloop.enqueue(() => {
343 dom.append(this.ccontainer, [dom.element('div', [pair.create('style', `${lineStyle};padding-left:${this.depth * 10}px`)], _computeLineSpans(args))])
344 })
345 }
346
347 /**
348 * @param {Error} err
349 */
350 printError (err) {
351 this.print([RED, BOLD, err.toString()])
352 }
353
354 /**
355 * @param {string} url
356 * @param {number} height
357 */
358 printImg (url, height) {
359 eventloop.enqueue(() => {
360 dom.append(this.ccontainer, [dom.element('img', [pair.create('src', url), pair.create('height', `${math.round(height * 1.5)}px`)])])
361 })
362 }
363
364 /**
365 * @param {Node} node
366 */
367 printDom (node) {
368 eventloop.enqueue(() => {
369 dom.append(this.ccontainer, [node])
370 })
371 }
372
373 destroy () {
374 eventloop.enqueue(() => {
375 vconsoles.delete(this)
376 })
377 }
378}
379
380/* istanbul ignore next */
381/**
382 * @param {Element} dom
383 */
384export const createVConsole = dom => new VConsole(dom)
385
386const loggingColors = [GREEN, PURPLE, ORANGE, BLUE]
387let nextColor = 0
388let lastLoggingTime = time.getUnixTime()
389
390/**
391 * @param {string} moduleName
392 * @return {function(...any):void}
393 */
394export const createModuleLogger = moduleName => {
395 const color = loggingColors[nextColor]
396 const debugRegexVar = env.getVariable('log')
397 const doLogging = debugRegexVar !== null && (debugRegexVar === '*' || debugRegexVar === 'true' || new RegExp(debugRegexVar, 'gi').test(moduleName))
398 nextColor = (nextColor + 1) % loggingColors.length
399 moduleName += ': '
400
401 return !doLogging ? func.nop : (...args) => {
402 const timeNow = time.getUnixTime()
403 const timeDiff = timeNow - lastLoggingTime
404 lastLoggingTime = timeNow
405 print(color, moduleName, UNCOLOR, ...args.map(arg => (typeof arg === 'string' || typeof arg === 'symbol') ? arg : JSON.stringify(arg)), color, ' +' + timeDiff + 'ms')
406 }
407}