UNPKG

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