import { Injectable } from '@angular/core' ; declare let amplitude ; @ Injectable ( ) export class DebugService { constructor ( ) { const debugMode = process . env . NODE_ENV === 'production' || process . env . LOCAL_TEST != null ; console . log = this . override ( console . log , debugMode ) ; console . warn = this . override ( console . warn , debugMode ) ; console . error = this . overrideError ( console . error , console . warn , debugMode ) ; if ( debugMode ) { console . debug = ( message ? , ... optionalParams ) => { } ; } } private override ( oldConsoleLogFunc , debugMode ) { if ( debugMode ) { return function ( ) { let argsString = '' ; const shouldFormat = arguments . length > 0 && arguments [ 0 ] !== 'no-format' ; for ( let i = 0 ; i < arguments . length ; i ++ ) { let cache = [ ] ; const argAsString = JSON . stringify ( arguments [ i ] , function ( key , value ) { if ( typeof value === 'object' && value !== null ) { if ( cache . indexOf ( value ) !== - 1 ) { return ; } cache . push ( value ) ; } return value ; } ) || '' ; argsString += ( shouldFormat ? argAsString . substring ( 0 , 1000 ) : argAsString ) + ' | ' ; cache = null ; } oldConsoleLogFunc . apply ( console , [ argsString ] ) ; } ; } return oldConsoleLogFunc ; } private overrideError ( oldConsoleLogFunc , oldWarnFunc , debugMode ) { if ( debugMode ) { return function ( ) { if ( Math . random ( ) < 0.001 ) { amplitude . getInstance ( ) . logEvent ( 'error-logged' ) ; } const stack = new Error ( ) . stack ; let argsString = stack + '\n|' ; for ( let i = 0 ; i < arguments . length ; i ++ ) { let cache = [ ] ; argsString += ( JSON . stringify ( arguments [ i ] , function ( key , value ) { if ( typeof value === 'object' && value !== null ) { if ( cache . indexOf ( value ) !== - 1 ) { return ; } cache . push ( value ) ; } return value ; } ) || '' ) . substring ( 0 , 1000 ) + ' | ' ; cache = null ; } oldWarnFunc . apply ( console , [ '(ERROR)' , argsString ] ) ; oldConsoleLogFunc . apply ( console , arguments ) ; } ; } return oldConsoleLogFunc ; } }