1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | import * as env from './environment.js'
|
8 | import * as set from './set.js'
|
9 | import * as pair from './pair.js'
|
10 | import * as dom from './dom.js'
|
11 | import * as json from './json.js'
|
12 | import * as map from './map.js'
|
13 | import * as eventloop from './eventloop.js'
|
14 | import * as math from './math.js'
|
15 | import * as common from './logging.common.js'
|
16 |
|
17 | export { BOLD, UNBOLD, BLUE, GREY, GREEN, RED, PURPLE, ORANGE, UNCOLOR } from './logging.common.js'
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | const _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'),
|
31 | [common.UNCOLOR]: pair.create('color', 'black')
|
32 | }
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | const computeBrowserLoggingArgs = (args) => {
|
40 | const strBuilder = []
|
41 | const styles = []
|
42 | const currentStyle = map.create()
|
43 | |
44 |
|
45 |
|
46 | let logArgs = []
|
47 |
|
48 | let i = 0
|
49 | for (; i < args.length; i++) {
|
50 | const arg = args[i]
|
51 |
|
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 |
|
71 | logArgs = styles
|
72 | logArgs.unshift(strBuilder.join(''))
|
73 | }
|
74 |
|
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 |
|
84 |
|
85 |
|
86 | const computeLoggingArgs = env.supportsColor
|
87 | ? computeBrowserLoggingArgs
|
88 | : common.computeNoColorLoggingArgs
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | export const print = (...args) => {
|
95 | console.log(...computeLoggingArgs(args))
|
96 |
|
97 | vconsoles.forEach((vc) => vc.print(args))
|
98 | }
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | export const warn = (...args) => {
|
105 | console.warn(...computeLoggingArgs(args))
|
106 | args.unshift(common.ORANGE)
|
107 | vconsoles.forEach((vc) => vc.print(args))
|
108 | }
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | export const printError = (err) => {
|
116 | console.error(err)
|
117 | vconsoles.forEach((vc) => vc.printError(err))
|
118 | }
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | export 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 |
|
133 | }
|
134 | vconsoles.forEach((vc) => vc.printImg(url, height))
|
135 | }
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | export const printImgBase64 = (base64, height) =>
|
144 | printImg(`data:image/gif;base64,${base64}`, height)
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | export const group = (...args) => {
|
150 | console.group(...computeLoggingArgs(args))
|
151 |
|
152 | vconsoles.forEach((vc) => vc.group(args))
|
153 | }
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | export const groupCollapsed = (...args) => {
|
159 | console.groupCollapsed(...computeLoggingArgs(args))
|
160 |
|
161 | vconsoles.forEach((vc) => vc.groupCollapsed(args))
|
162 | }
|
163 |
|
164 | export const groupEnd = () => {
|
165 | console.groupEnd()
|
166 |
|
167 | vconsoles.forEach((vc) => vc.groupEnd())
|
168 | }
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 | export const printDom = (createNode) =>
|
175 | vconsoles.forEach((vc) => vc.printDom(createNode()))
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 | export const printCanvas = (canvas, height) =>
|
183 | printImg(canvas.toDataURL(), height)
|
184 |
|
185 | export const vconsoles = set.create()
|
186 |
|
187 | /**
|
188 | * @param {Array<string|Symbol|Object|number>} args
|
189 | * @return {Array<Element>}
|
190 | */
|
191 |
|
192 | const _computeLineSpans = (args) => {
|
193 | const spans = []
|
194 | const currentStyle = new Map()
|
195 |
|
196 | let i = 0
|
197 | for (; i < args.length; i++) {
|
198 | const arg = args[i]
|
199 |
|
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 |
|
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 = ' '
|
211 | }
|
212 | spans.push(span)
|
213 | } else {
|
214 | break
|
215 | }
|
216 | }
|
217 | }
|
218 |
|
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( (content))])
|
227 | )
|
228 | }
|
229 | }
|
230 | return spans
|
231 | }
|
232 |
|
233 |
|
234 | const lineStyle =
|
235 | 'font-family:monospace;border-bottom:1px solid #e2e2e2;padding:2px;'
|
236 |
|
237 |
|
238 | export class VConsole {
|
239 | |
240 |
|
241 |
|
242 | constructor (dom) {
|
243 | this.dom = dom
|
244 | |
245 |
|
246 |
|
247 | this.ccontainer = this.dom
|
248 | this.depth = 0
|
249 | vconsoles.add(this)
|
250 | }
|
251 |
|
252 | |
253 |
|
254 |
|
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 |
|
284 | dom.addEventListener(content, 'click', (_event) => {
|
285 | nextContainer.toggleAttribute('hidden')
|
286 | triangleDown.toggleAttribute('hidden')
|
287 | triangleRight.toggleAttribute('hidden')
|
288 | })
|
289 | })
|
290 | }
|
291 |
|
292 | |
293 |
|
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 |
|
304 | this.ccontainer = this.ccontainer.parentElement.parentElement
|
305 | }
|
306 | })
|
307 | }
|
308 |
|
309 | |
310 |
|
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 |
|
327 |
|
328 | printError (err) {
|
329 | this.print([common.RED, common.BOLD, err.toString()])
|
330 | }
|
331 |
|
332 | |
333 |
|
334 |
|
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 |
|
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 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 | export const createVConsole = (dom) => new VConsole(dom)
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 | export const createModuleLogger = (moduleName) => common.createModuleLogger(print, moduleName)
|