import { useRef } from 'react' ; import { remote } from './redux/shell/remote' ; export type LogLevel = 'error' | 'warn' | 'info' | 'http' | 'verbose' | 'debug' | 'silly' ; export type Log = ( message : string , meta ? : { } ) => void ; export interface Logger { error : Log ; warn : Log ; info : Log ; http : Log ; verbose : Log ; debug : Log ; silly : Log ; } const ERROR = 'error' ; const WARN = 'warn' ; const INFO = 'info' ; const HTTP = 'http' ; const VERBOSE = 'verbose' ; const DEBUG = 'debug' ; const SILLY = 'silly' ; export function createLogger ( filename ) { const label = `app/${ filename }` ; return { [ ERROR ] : ( message , meta ) => log ( ERROR , message , label , meta ) , [ WARN ] : ( message , meta ) => log ( WARN , message , label , meta ) , [ INFO ] : ( message , meta ) => log ( INFO , message , label , meta ) , [ HTTP ] : ( message , meta ) => log ( HTTP , message , label , meta ) , [ VERBOSE ] : ( message , meta ) => log ( VERBOSE , message , label , meta ) , [ DEBUG ] : ( message , meta ) => log ( DEBUG , message , label , meta ) , [ SILLY ] : ( message , meta ) => log ( SILLY , message , label , meta ) , } ; } function log ( level , message , label , meta ? ) { const print = `[${ label }] ${ level }: ${ message }` ; if ( level === 'error' ) { console . error ( print ) ; } else if ( level === 'warn' ) { console . warn ( print ) ; } else if ( level === 'info' ) { console . info ( print ) ; } else { console . log ( print ) ; } if ( meta && Object . getOwnPropertyNames ( meta ) . length !== 0 ) { console . dir ( meta ) ; } remote . ipcRenderer . send ( 'log' , { ... meta , level , message , label } ) ; } export function useLogger ( filename ) { const loggerRef = useRef < Logger | null > ( null ) ; const getLogger = ( ) : Logger => { if ( loggerRef . current === null ) { loggerRef . current = createLogger ( filename ) ; } return loggerRef . current ; } ; return getLogger ( ) ; }