UNPKG

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