Skip to content

Commit 3ba8b4c

Browse files
committed
refactor(src/vm.js): improve logging
1 parent a653cf5 commit 3ba8b4c

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/vm.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export async function init (options) {
1919
port.start()
2020
}
2121

22+
const { log, info, warn, debug, error } = globalThis.console
23+
2224
Object.assign(globalThis.console, {
2325
log (...args) {
2426
if (!port) {
@@ -29,9 +31,23 @@ export async function init (options) {
2931
.map(getTransferables)
3032
.reduce((array, transfer) => array.concat(transfer), [])
3133

34+
log.call(globalThis, ...args)
3235
port.postMessage({ method: 'console.log', args }, { transfer })
3336
},
3437

38+
info (...args) {
39+
if (!port) {
40+
return
41+
}
42+
43+
const transfer = args
44+
.map(getTransferables)
45+
.reduce((array, transfer) => array.concat(transfer), [])
46+
47+
info.call(globalThis, ...args)
48+
port.postMessage({ method: 'console.info', args }, { transfer })
49+
},
50+
3551
error (...args) {
3652
if (!port) {
3753
return
@@ -41,9 +57,23 @@ export async function init (options) {
4157
.map(getTransferables)
4258
.reduce((array, transfer) => array.concat(transfer), [])
4359

60+
error.call(globalThis, ...args)
4461
port.postMessage({ method: 'console.error', args }, { transfer })
4562
},
4663

64+
warn (...args) {
65+
if (!port) {
66+
return
67+
}
68+
69+
const transfer = args
70+
.map(getTransferables)
71+
.reduce((array, transfer) => array.concat(transfer), [])
72+
73+
warn.call(globalThis, ...args)
74+
port.postMessage({ method: 'console.warn', args }, { transfer })
75+
},
76+
4777
debug (...args) {
4878
if (!port) {
4979
return
@@ -53,6 +83,7 @@ export async function init (options) {
5383
.map(getTransferables)
5484
.reduce((array, transfer) => array.concat(transfer), [])
5585

86+
debug.call(globalThis, ...args)
5687
port.postMessage({ method: 'console.debug', args }, { transfer })
5788
}
5889
})

0 commit comments

Comments
 (0)