From b767bd4484d0bad7d23469ad88da1c8bcb370547 Mon Sep 17 00:00:00 2001 From: Ib Green Date: Wed, 16 Oct 2024 22:06:10 -0400 Subject: [PATCH] feat(log): Add MemoryLog class --- modules/log/src/index.ts | 11 +++-- .../src/{log.ts => loggers/console-log.ts} | 33 +++++++------- modules/log/src/loggers/logger.ts | 29 ++++++++++++ modules/log/src/loggers/memory-log.ts | 45 +++++++++++++++++++ .../log/test/lib/normalize-arguments.spec.js | 2 +- .../src/components/bench-results.tsx | 2 +- 6 files changed, 100 insertions(+), 22 deletions(-) rename modules/log/src/{log.ts => loggers/console-log.ts} (93%) create mode 100644 modules/log/src/loggers/logger.ts create mode 100644 modules/log/src/loggers/memory-log.ts diff --git a/modules/log/src/index.ts b/modules/log/src/index.ts index 8f22708..06d6988 100644 --- a/modules/log/src/index.ts +++ b/modules/log/src/index.ts @@ -1,13 +1,16 @@ -import {Log} from './log'; +import {ConsoleLog} from './loggers/console-log'; // DEFAULT EXPORT IS A LOG INSTANCE -export default new Log({id: '@probe.gl/log'}); +export default new ConsoleLog({id: '@probe.gl/log'}); // LOGGING -export {Log} from './log'; -export {COLOR} from './utils/color'; +export type {Logger} from './loggers/logger'; +export {ConsoleLog, ConsoleLog as Log} from './loggers/console-log'; +export type {MemoryLogMessage} from './loggers/memory-log'; +export {MemoryLog} from './loggers/memory-log'; // UTILITIES +export {COLOR} from './utils/color'; export {addColor} from './utils/color'; export {leftPad, rightPad} from './utils/formatters'; export {autobind} from './utils/autobind'; diff --git a/modules/log/src/log.ts b/modules/log/src/loggers/console-log.ts similarity index 93% rename from modules/log/src/log.ts rename to modules/log/src/loggers/console-log.ts index 414204c..a0c7464 100644 --- a/modules/log/src/log.ts +++ b/modules/log/src/loggers/console-log.ts @@ -1,23 +1,26 @@ -// probe.gl, MIT license +// probe.gl +// SPDX-License-Identifier: MIT +// Copyright (c) vis.gl contributors /* eslint-disable no-console,prefer-rest-params */ import {VERSION, isBrowser} from '@probe.gl/env'; -import {LocalStorage} from './utils/local-storage'; -import {formatTime, leftPad} from './utils/formatters'; -import {addColor} from './utils/color'; -import {autobind} from './utils/autobind'; -import assert from './utils/assert'; -import {getHiResTimestamp} from './utils/hi-res-timestamp'; +import {Logger, LogFunction} from './logger'; +import {LocalStorage} from '../utils/local-storage'; +import {formatTime, leftPad} from '../utils/formatters'; +import {addColor} from '../utils/color'; +import {autobind} from '../utils/autobind'; +import assert from '../utils/assert'; +import {getHiResTimestamp} from '../utils/hi-res-timestamp'; /** "Global" log configuration settings */ -type LogConfiguration = { +type ConsoleLogConfiguration = { enabled?: boolean; level?: number; [key: string]: unknown; }; /** Options when logging a message */ -type LogOptions = { +type ConsoleLogOptions = { method?: Function; time?: boolean; total?: number; @@ -29,8 +32,6 @@ type LogOptions = { args?: any; }; -type LogFunction = () => void; - type Table = Record; // Instrumentation in other packages may override console methods, so preserve them here @@ -42,7 +43,7 @@ const originalConsole = { error: console.error }; -const DEFAULT_LOG_CONFIGURATION: Required = { +const DEFAULT_LOG_CONFIGURATION: Required = { enabled: true, level: 0 }; @@ -54,14 +55,14 @@ const ONCE = {once: true}; /** A console wrapper */ -export class Log { +export class ConsoleLog implements Logger { static VERSION = VERSION; id: string; VERSION: string = VERSION; _startTs: number = getHiResTimestamp(); _deltaTs: number = getHiResTimestamp(); - _storage: LocalStorage; + _storage: LocalStorage; userData = {}; // TODO - fix support from throttling groups @@ -70,7 +71,7 @@ export class Log { constructor({id} = {id: ''}) { this.id = id; this.userData = {}; - this._storage = new LocalStorage( + this._storage = new LocalStorage( `__probe-${this.id}__`, DEFAULT_LOG_CONFIGURATION ); @@ -298,7 +299,7 @@ in a later version. Use \`${newUsage}\` instead`); message?: unknown, method?: Function, args?: IArguments, - opts?: LogOptions + opts?: ConsoleLogOptions ): LogFunction { if (this._shouldLog(logLevel)) { // normalized opts + timings diff --git a/modules/log/src/loggers/logger.ts b/modules/log/src/loggers/logger.ts new file mode 100644 index 0000000..f4384e4 --- /dev/null +++ b/modules/log/src/loggers/logger.ts @@ -0,0 +1,29 @@ +// probe.gl +// SPDX-License-Identifier: MIT +// Copyright (c) vis.gl contributors + +/* eslint-disable no-console,prefer-rest-params */ +export type LogFunction = () => void; + +/** A common interface for loggers */ + +export interface Logger { + // Unconditional logging + + /** Warn, but only once, no console flooding */ + warn(message: string, ...args: unknown[]): LogFunction; + + /** Print an error */ + error(message: string, ...args: unknown[]): LogFunction; + + // Conditional logging + + /** Log a debug message */ + log(logLevel, message?, ...args: unknown[]): LogFunction; + + /** Log a normal message */ + info(logLevel, message?, ...args: unknown[]): LogFunction; + + /** Log a normal message, but only once, no console flooding */ + once(logLevel, message?, ...args: unknown[]): LogFunction; +} diff --git a/modules/log/src/loggers/memory-log.ts b/modules/log/src/loggers/memory-log.ts new file mode 100644 index 0000000..fc1ac86 --- /dev/null +++ b/modules/log/src/loggers/memory-log.ts @@ -0,0 +1,45 @@ +// probe.gl +// SPDX-License-Identifier: MIT +// Copyright (c) vis.gl contributors + +import {Logger, LogFunction} from './logger'; + +export type MemoryLogMessage = { + level: number; + type: 'warning' | 'error' | 'log' | 'info' | 'once' | 'table'; + message: string; + args: unknown[]; +}; + +export class MemoryLog implements Logger { + userData: Record = {}; + + messages: MemoryLogMessage[] = []; + + /** Warn, but only once, no console flooding */ + warn(message: string, ...args: unknown[]): LogFunction { + return () => this.messages.push({type: 'warning', level: 0, message, args}); + } + + /** Print an error */ + error(message: string, ...args: unknown[]): LogFunction { + return () => this.messages.push({type: 'error', level: 0, message, args}); + } + + // Conditional logging + + /** Log a debug message */ + log(logLevel, message?, ...args: unknown[]): LogFunction { + return () => this.messages.push({type: 'log', level: logLevel, message, args}); + } + + /** Log a normal message */ + info(logLevel, message?, ...args: unknown[]): LogFunction { + return () => this.messages.push({type: 'info', level: logLevel, message, args}); + } + + /** Log a normal message, but only once, no console flooding */ + once(logLevel, message?, ...args: unknown[]): LogFunction { + return () => this.messages.push({type: 'once', level: logLevel, message, args}); + } +} diff --git a/modules/log/test/lib/normalize-arguments.spec.js b/modules/log/test/lib/normalize-arguments.spec.js index bce7fb2..5667bcf 100644 --- a/modules/log/test/lib/normalize-arguments.spec.js +++ b/modules/log/test/lib/normalize-arguments.spec.js @@ -1,4 +1,4 @@ -import {normalizeArguments} from '@probe.gl/log/log'; +import {normalizeArguments} from '@probe.gl/log/loggers/console-log'; import test from 'tape-promise/tape'; function makeOpts(logLevel, message, ...args) { diff --git a/modules/react-bench/src/components/bench-results.tsx b/modules/react-bench/src/components/bench-results.tsx index 323006b..3cdc484 100644 --- a/modules/react-bench/src/components/bench-results.tsx +++ b/modules/react-bench/src/components/bench-results.tsx @@ -2,7 +2,7 @@ import React, {Component, FC, PropsWithChildren} from 'react'; import ReactTable from 'react-table'; -import '../react-table.css'; +// import '../react-table.css'; function getPercent(score: number): number { // Log scale between 100K - 100M, 0-3