/
githubmirror
/
client
Обзор
Документация
Войти
/
githubmirror
/
client
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
shared/logger/ring-logger.tsx
57 строк
1 KB
chrisnojima
move to vite (#29386)
09 июл 2026, 00:46
Не верифицирован
09 июл 2026, 00:46
bcd1945
Код
Авторство
О чём код?
import type * as Types from '.' import {toStringForLog} from '@/util/string' import {registerDebugClear} from '@/util/debug-registry' const levelToFunction = { Action: 'log', Debug: 'log', Error: 'error', Info: 'info', Warn: 'warn', } as const // Simple in memory ring Logger class RingLogger { private ringSize: number private currentWriteIdx: number = 0 private ringBuffer: Array<Types.LogLine | undefined> = [] private logLevel: Types.LogLevel private consoleLog: (...s: Array<unknown>) => void constructor(logLevel: Types.LogLevel, ringSize: number) { this.logLevel = logLevel this.ringSize = ringSize this.consoleLog = console[levelToFunction[logLevel]].bind(console) registerDebugClear(() => { this.ringBuffer.length = 0 }) } log = (...s: Array<unknown>) => { const singleString = s.map(toStringForLog).join(' ') if (__DEV__) { this.consoleLog(this.logLevel, ...s) } if (this.ringSize) { this.ringBuffer[this.currentWriteIdx] = [Date.now(), singleString] this.currentWriteIdx = (this.currentWriteIdx + 1) % this.ringSize } } dump = () => { const toDump: Array<Types.LogLineWithLevel> = [] for (let i = 0; i < this.ringSize; i++) { const idxWrapped = (this.currentWriteIdx + i) % this.ringSize const s = this.ringBuffer[idxWrapped] if (s) { this.ringBuffer[idxWrapped] = undefined toDump.push([this.logLevel, s[0], s[1]]) } } return toDump } } export default RingLogger