UNPKG

3.9 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 common from './logging.common.js'
9
10export { BOLD, UNBOLD, BLUE, GREY, GREEN, RED, PURPLE, ORANGE, UNCOLOR } from './logging.common.js'
11
12const _nodeStyleMap = {
13 [common.BOLD]: '\u001b[1m',
14 [common.UNBOLD]: '\u001b[2m',
15 [common.BLUE]: '\x1b[34m',
16 [common.GREEN]: '\x1b[32m',
17 [common.GREY]: '\u001b[37m',
18 [common.RED]: '\x1b[31m',
19 [common.PURPLE]: '\x1b[35m',
20 [common.ORANGE]: '\x1b[38;5;208m',
21 [common.UNCOLOR]: '\x1b[0m'
22}
23
24/* c8 ignore start */
25/**
26 * @param {Array<string|undefined|Symbol|Object|number|function():Array<any>>} args
27 * @return {Array<string|object|number|undefined>}
28 */
29const computeNodeLoggingArgs = (args) => {
30 if (args.length === 1 && args[0]?.constructor === Function) {
31 args = /** @type {Array<string|Symbol|Object|number>} */ (/** @type {[function]} */ (args)[0]())
32 }
33 const strBuilder = []
34 const logArgs = []
35 // try with formatting until we find something unsupported
36 let i = 0
37 for (; i < args.length; i++) {
38 const arg = args[i]
39 // @ts-ignore
40 const style = _nodeStyleMap[arg]
41 if (style !== undefined) {
42 strBuilder.push(style)
43 } else {
44 if (arg === undefined) {
45 break
46 } else if (arg.constructor === String || arg.constructor === Number) {
47 strBuilder.push(arg)
48 } else {
49 break
50 }
51 }
52 }
53 if (i > 0) {
54 // create logArgs with what we have so far
55 strBuilder.push('\x1b[0m')
56 logArgs.push(strBuilder.join(''))
57 }
58 // append the rest
59 for (; i < args.length; i++) {
60 const arg = args[i]
61 if (!(arg instanceof Symbol)) {
62 logArgs.push(arg)
63 }
64 }
65 return logArgs
66}
67/* c8 ignore stop */
68
69/* c8 ignore start */
70const computeLoggingArgs = env.supportsColor
71 ? computeNodeLoggingArgs
72 : common.computeNoColorLoggingArgs
73/* c8 ignore stop */
74
75/**
76 * @param {Array<string|Symbol|Object|number|undefined>} args
77 */
78export const print = (...args) => {
79 console.log(...computeLoggingArgs(args))
80}
81
82/* c8 ignore start */
83/**
84 * @param {Array<string|Symbol|Object|number>} args
85 */
86export const warn = (...args) => {
87 console.warn(...computeLoggingArgs(args))
88}
89/* c8 ignore stop */
90
91/**
92 * @param {Error} err
93 */
94/* c8 ignore start */
95export const printError = (err) => {
96 console.error(err)
97}
98/* c8 ignore stop */
99
100/**
101 * @param {string} _url image location
102 * @param {number} _height height of the image in pixel
103 */
104/* c8 ignore start */
105export const printImg = (_url, _height) => {
106 // console.log('%c ', `font-size: ${height}x; background: url(${url}) no-repeat;`)
107}
108/* c8 ignore stop */
109
110/**
111 * @param {string} base64
112 * @param {number} height
113 */
114/* c8 ignore next 2 */
115export const printImgBase64 = (base64, height) =>
116 printImg(`data:image/gif;base64,${base64}`, height)
117
118/**
119 * @param {Array<string|Symbol|Object|number>} args
120 */
121/* c8 ignore next 3 */
122export const group = (...args) => {
123 console.group(...computeLoggingArgs(args))
124}
125
126/**
127 * @param {Array<string|Symbol|Object|number>} args
128 */
129/* c8 ignore next 3 */
130export const groupCollapsed = (...args) => {
131 console.groupCollapsed(...computeLoggingArgs(args))
132}
133
134/* c8 ignore next 3 */
135export const groupEnd = () => {
136 console.groupEnd()
137}
138
139/**
140 * @param {function():Node} _createNode
141 */
142/* c8 ignore next 2 */
143export const printDom = (_createNode) => {}
144
145/**
146 * @param {HTMLCanvasElement} canvas
147 * @param {number} height
148 */
149/* c8 ignore next 2 */
150export const printCanvas = (canvas, height) =>
151 printImg(canvas.toDataURL(), height)
152
153/**
154 * @param {Element} _dom
155 */
156/* c8 ignore next */
157export const createVConsole = (_dom) => {}
158
159/**
160 * @param {string} moduleName
161 * @return {function(...any):void}
162 */
163/* c8 ignore next */
164export const createModuleLogger = (moduleName) => common.createModuleLogger(print, moduleName)