22 * @file Module for intercepting console logs with stack trace capture
33 */
44
5- import type { ConsoleLogEvent } from " @hawk.so/types" ;
5+ import type { ConsoleLogEvent } from ' @hawk.so/types' ;
66
77const createConsoleCatcher = ( ) : {
88 initConsoleCatcher : ( ) => void ;
@@ -18,13 +18,15 @@ const createConsoleCatcher = (): {
1818 */
1919 const safeStringify = ( obj : unknown ) : string => {
2020 const seen = new WeakSet ( ) ;
21+
2122 return JSON . stringify ( obj , ( key , value ) => {
22- if ( typeof value === " object" && value !== null ) {
23+ if ( typeof value === ' object' && value !== null ) {
2324 if ( seen . has ( value ) ) {
24- return " [Circular]" ;
25+ return ' [Circular]' ;
2526 }
2627 seen . add ( value ) ;
2728 }
29+
2830 return value ;
2931 } ) ;
3032 } ;
@@ -38,11 +40,12 @@ const createConsoleCatcher = (): {
3840 if ( args . length === 0 ) return { message : "" , styles : [ ] } ;
3941
4042 const firstArg = args [ 0 ] ;
41- if ( typeof firstArg !== "string" || ! firstArg . includes ( "%c" ) ) {
43+ if ( typeof firstArg !== 'string' || ! firstArg . includes ( '%c' ) ) {
44+
4245 return {
4346 message : args
44- . map ( ( arg ) => ( typeof arg === " string" ? arg : safeStringify ( arg ) ) )
45- . join ( " " ) ,
47+ . map ( ( arg ) => ( typeof arg === ' string' ? arg : safeStringify ( arg ) ) )
48+ . join ( ' ' ) ,
4649 styles : [ ] ,
4750 } ;
4851 }
@@ -55,20 +58,20 @@ const createConsoleCatcher = (): {
5558 let styleIndex = 0 ;
5659 for ( let i = 1 ; i < args . length ; i ++ ) {
5760 const arg = args [ i ] ;
58- if ( typeof arg === " string" && message . indexOf ( "%c" , styleIndex ) !== - 1 ) {
61+ if ( typeof arg === ' string' && message . indexOf ( '%c' , styleIndex ) !== - 1 ) {
5962 styles . push ( arg ) ;
60- styleIndex = message . indexOf ( "%c" , styleIndex ) + 2 ;
63+ styleIndex = message . indexOf ( '%c' , styleIndex ) + 2 ;
6164 }
6265 }
6366
6467 // Add remaining arguments that aren't styles
6568 const remainingArgs = args
6669 . slice ( styles . length + 1 )
67- . map ( ( arg ) => ( typeof arg === " string" ? arg : safeStringify ( arg ) ) )
68- . join ( " " ) ;
70+ . map ( ( arg ) => ( typeof arg === ' string' ? arg : safeStringify ( arg ) ) )
71+ . join ( ' ' ) ;
6972
7073 return {
71- message : message + ( remainingArgs ? " " + remainingArgs : "" ) ,
74+ message : message + ( remainingArgs ? ' ' + remainingArgs : '' ) ,
7275 styles,
7376 } ;
7477 } ;
@@ -84,25 +87,26 @@ const createConsoleCatcher = (): {
8487 event : ErrorEvent | PromiseRejectionEvent
8588 ) : ConsoleLogEvent => {
8689 if ( event instanceof ErrorEvent ) {
90+
8791 return {
88- method : " error" ,
92+ method : ' error' ,
8993 timestamp : new Date ( ) ,
90- type : event . error ?. name || " Error" ,
94+ type : event . error ?. name || ' Error' ,
9195 message : event . error ?. message || event . message ,
92- stack : event . error ?. stack || "" ,
96+ stack : event . error ?. stack || '' ,
9397 fileLine : event . filename
9498 ? `${ event . filename } :${ event . lineno } :${ event . colno } `
95- : "" ,
99+ : '' ,
96100 } ;
97101 }
98102
99103 return {
100- method : " error" ,
104+ method : ' error' ,
101105 timestamp : new Date ( ) ,
102- type : " UnhandledRejection" ,
106+ type : ' UnhandledRejection' ,
103107 message : event . reason ?. message || String ( event . reason ) ,
104- stack : event . reason ?. stack || "" ,
105- fileLine : "" ,
108+ stack : event . reason ?. stack || '' ,
109+ fileLine : '' ,
106110 } ;
107111 } ;
108112
@@ -114,23 +118,23 @@ const createConsoleCatcher = (): {
114118
115119 isInitialized = true ;
116120 const consoleMethods : string [ ] = [
117- " log" ,
118- " warn" ,
119- " error" ,
120- " info" ,
121- " debug" ,
121+ ' log' ,
122+ ' warn' ,
123+ ' error' ,
124+ ' info' ,
125+ ' debug' ,
122126 ] ;
123127
124128 consoleMethods . forEach ( ( method ) => {
125- if ( typeof window . console [ method ] !== " function" ) {
129+ if ( typeof window . console [ method ] !== ' function' ) {
126130 return ;
127131 }
128132
129133 const oldFunction = window . console [ method ] . bind ( window . console ) ;
130134
131135 window . console [ method ] = function ( ...args : unknown [ ] ) : void {
132136 const stack =
133- new Error ( ) . stack ?. split ( "\n" ) . slice ( 2 ) . join ( "\n" ) || "" ;
137+ new Error ( ) . stack ?. split ( '\n' ) . slice ( 2 ) . join ( '\n' ) || '' ;
134138 const { message, styles } = formatConsoleArgs ( args ) ;
135139
136140 const logEvent : ConsoleLogEvent = {
@@ -139,7 +143,7 @@ const createConsoleCatcher = (): {
139143 type : method ,
140144 message,
141145 stack,
142- fileLine : stack . split ( "\n" ) [ 0 ] ?. trim ( ) ,
146+ fileLine : stack . split ( '\n' ) [ 0 ] ?. trim ( ) ,
143147 styles,
144148 } ;
145149
@@ -156,6 +160,7 @@ const createConsoleCatcher = (): {
156160 } ,
157161
158162 getConsoleLogStack ( ) : ConsoleLogEvent [ ] {
163+
159164 return [ ...consoleOutput ] ;
160165 } ,
161166 } ;
0 commit comments