import { ILogger } from 'polar-shared/src/logger/ILogger' ; import { FixedBuffer } from '../util/FixedBuffer' ; import { LogLevelName , LogMessage } from './Logging' ; import { EventListener , Releaseable } from '../reactor/EventListener' ; import { ISODateTimeStrings } from 'polar-shared/src/metadata/ISODateTimeStrings' ; import { Strings } from "polar-shared/src/util/Strings" ; const capacity = 250 ; let IDX_GENERATOR = 0 ; const buffer = new FixedBuffer < LogMessage > ( capacity ) ; /**
 * Write log messages to an internal buffer for testing log output of
 * components.
 */ export class MemoryLogger implements ILogger { public readonly name : string = 'memory-logger' ; public notice ( msg , ... args ) { buffer . write ( createLogMessage ( 'notice' , msg , args ) ) ; } public info ( msg , ... args ) { buffer . write ( createLogMessage ( 'info' , msg , args ) ) ; } public warn ( msg , ... args ) { buffer . write ( createLogMessage ( 'warn' , msg , args ) ) ; } public error ( msg , ... args ) { buffer . write ( createLogMessage ( 'error' , msg , args ) ) ; } public verbose ( msg , ... args ) { buffer . write ( createLogMessage ( 'verbose' , msg , args ) ) ; } public debug ( msg , ... args ) { buffer . write ( createLogMessage ( 'debug' , msg , args ) ) ; } public getOutput ( ) { return buffer . toView ( ) . join ( "\n" ) ; } public toJSON ( ) { return JSON . stringify ( buffer . toView ( ) , null , "  " ) ; } public async sync ( ) { } public static addEventListener ( eventListener ) { return buffer . addEventListener ( eventListener ) ; } public static toView ( ) { return buffer . toView ( ) ; } public static clear ( ) { buffer . clear ( ) ; buffer . write ( createLogMessage ( 'info' , "Log messages cleared" , [ ] ) ) ; } } function createLogMessage ( level , msg , args ) { return { timestamp : ISODateTimeStrings . create ( ) , idx : IDX_GENERATOR ++ , level , msg , args } ; }