File tree 1 file changed +46
-20
lines changed
1 file changed +46
-20
lines changed Original file line number Diff line number Diff line change 3
3
const originalConsoleLog = console . log ;
4
4
const originalConsoleError = console . error ;
5
5
6
- function ensureSerializable ( arg : any ) {
7
- if ( arg === undefined ) return "undefined" ;
8
- if ( arg === null ) return null ;
9
-
10
- if ( typeof arg === "object" ) {
11
- try {
12
- JSON . stringify ( arg ) ;
13
- return arg ;
14
- } catch ( e ) {
15
- return String ( arg ) ;
16
- }
17
- }
18
-
19
- return arg ;
20
- }
21
-
22
6
export function setupJsonConsole ( ) {
23
7
console . log = function ( ...args ) {
24
- const serializedArgs = args . map ( ensureSerializable ) ;
25
- originalConsoleLog ( JSON . stringify ( serializedArgs ) ) ;
8
+ const message = args
9
+ . map ( ( arg ) => {
10
+ if ( typeof arg === "object" || Array . isArray ( arg ) ) {
11
+ try {
12
+ return JSON . stringify ( arg ) ;
13
+ } catch ( e ) {
14
+ return String ( arg ) ;
15
+ }
16
+ }
17
+ return String ( arg ) ;
18
+ } )
19
+ . join ( " " ) ;
20
+
21
+ originalConsoleLog (
22
+ JSON . stringify ( {
23
+ jsonrpc : "2.0" ,
24
+ method : "window/logMessage" ,
25
+ params : {
26
+ type : 3 ,
27
+ message : message ,
28
+ } ,
29
+ } )
30
+ ) ;
26
31
} ;
27
32
28
33
console . error = function ( ...args ) {
29
- const serializedArgs = args . map ( ensureSerializable ) ;
30
- originalConsoleError ( JSON . stringify ( serializedArgs ) ) ;
34
+ const message = args
35
+ . map ( ( arg ) => {
36
+ if ( typeof arg === "object" || Array . isArray ( arg ) ) {
37
+ try {
38
+ return JSON . stringify ( arg ) ;
39
+ } catch ( e ) {
40
+ return String ( arg ) ;
41
+ }
42
+ }
43
+ return String ( arg ) ;
44
+ } )
45
+ . join ( " " ) ;
46
+
47
+ originalConsoleError (
48
+ JSON . stringify ( {
49
+ jsonrpc : "2.0" ,
50
+ method : "window/logMessage" ,
51
+ params : {
52
+ type : 1 ,
53
+ message : message ,
54
+ } ,
55
+ } )
56
+ ) ;
31
57
} ;
32
58
}
You can’t perform that action at this time.
0 commit comments