Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(log): Add MemoryLog class #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions modules/log/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
33 changes: 17 additions & 16 deletions modules/log/src/log.ts → modules/log/src/loggers/console-log.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -29,8 +32,6 @@ type LogOptions = {
args?: any;
};

type LogFunction = () => void;

type Table = Record<string, any>;

// Instrumentation in other packages may override console methods, so preserve them here
Expand All @@ -42,7 +43,7 @@ const originalConsole = {
error: console.error
};

const DEFAULT_LOG_CONFIGURATION: Required<LogConfiguration> = {
const DEFAULT_LOG_CONFIGURATION: Required<ConsoleLogConfiguration> = {
enabled: true,
level: 0
};
Expand All @@ -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<LogConfiguration>;
_storage: LocalStorage<ConsoleLogConfiguration>;
userData = {};

// TODO - fix support from throttling groups
Expand All @@ -70,7 +71,7 @@ export class Log {
constructor({id} = {id: ''}) {
this.id = id;
this.userData = {};
this._storage = new LocalStorage<LogConfiguration>(
this._storage = new LocalStorage<ConsoleLogConfiguration>(
`__probe-${this.id}__`,
DEFAULT_LOG_CONFIGURATION
);
Expand Down Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions modules/log/src/loggers/logger.ts
Original file line number Diff line number Diff line change
@@ -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;
}
45 changes: 45 additions & 0 deletions modules/log/src/loggers/memory-log.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown> = {};

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});
}
}
2 changes: 1 addition & 1 deletion modules/log/test/lib/normalize-arguments.spec.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion modules/react-bench/src/components/bench-results.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading