import pino from 'pino' ; import _ from 'lodash' ; import { LOG_DESTINATION , ADD_LOGS , JEST_WORKER_ID , LOG_LEVEL } from './config' ; const IS_BROWSER_CONTEXT = JEST_WORKER_ID === undefined ; const LOG_TO_CONSOLE = LOG_DESTINATION === 'console' ; const LOG_TO_FILE = ADD_LOGS && ! LOG_TO_CONSOLE ; const name = 'browser-wallet' ; const destination = LOG_TO_FILE && ! IS_BROWSER_CONTEXT ? pino . destination ( LOG_DESTINATION ) : undefined ; const postMessageAndCallConsoleFn = ( consoleFn ) => ( o ) => { const withName = JSON . stringify ( { ... o , name } ) ; window . parent . postMessage ( { type : 'PINO_LOG' , logEvent : JSON . parse ( withName ) } , '*' ) ; if ( LOG_TO_FILE ) consoleFn ( withName ) ; else consoleFn ( o . msg , _ . omit ( o , 'msg' ) ) ; } ; const browser = IS_BROWSER_CONTEXT ? { write : { error : postMessageAndCallConsoleFn ( console . error ) , warn : postMessageAndCallConsoleFn ( console . warn ) , info : postMessageAndCallConsoleFn ( console . info ) , debug : postMessageAndCallConsoleFn ( console . debug ) , trace : postMessageAndCallConsoleFn ( console . debug ) } } : undefined ; const prettyPrint = LOG_TO_CONSOLE ? { translateTime : true } : false ; const level = window . localStorage . LOG_LEVEL ?? LOG_LEVEL ; const opts = { name , prettyPrint , browser , level } ; const logger = destination ? pino ( opts , destination ) : pino ( opts ) ; logger . on ( 'level-change' , ( lvl , val , prevLvl , prevVal ) => { if ( lvl !== prevLvl ) console . log ( 'wallet: %s (%d) was changed to %s (%d)' , lvl , val , prevLvl , prevVal ) ; } ) ; window . addEventListener ( 'message' , event => { const key = 'LOG_LEVEL' ; if ( event . data . type === 'SET_LOG_LEVEL' ) { const { level } = event . data ; console . log ( `wallet: level CHANGED from ${ logger . level } to ${ level }` ) ; window . localStorage . setItem ( key , level ) ; logger . level = level ; } else if ( event . data . type === 'CLEAR_LOG_LEVEL' ) { console . log ( `wallet: level CLEARED from ${ logger . level } to ${ LOG_LEVEL }` ) ; window . localStorage . removeItem ( key ) ; logger . level = LOG_LEVEL ; } } ) ; export { logger } ;