diff --git a/src/Logger.ts b/src/Logger.ts index 5d8ba5a..61ea738 100644 --- a/src/Logger.ts +++ b/src/Logger.ts @@ -1,57 +1,15 @@ -export enum LogLevel { - TRACE = 10, - DEBUG = 20, - INFO = 30, - WARN = 40, - FATAL = 50, -} +import type { + LogOutputs, + LogContext, + BucketInfo, + LogMeta, + LogConfig, +} from "./types/logger"; -enum LogLabel { - TRACE = "trace", - DEBUG = "debug", - INFO = "info", - WARN = "warn", - FATAL = "fatal", -} +import { LogLabel, LogLevel } from "./types/logger"; +import type { DeepRequired } from "generic"; -export interface TampermonkeyOutputOpts { - enabled: boolean; - maxBuckets?: number; - bucketIndexKey?: string; -} - -export interface ConsoleOutputOpts { - enabled: boolean; -} - -export interface LogOutputs { - console?: ConsoleOutputOpts; - tampermonkey?: TampermonkeyOutputOpts; - callback: ((message: string) => any) | undefined; -} - -export interface LogConfig { - outputs?: LogOutputs; - bufferCapacity?: number; -} - -export interface LogContext { - level?: number; - [key: string]: any; -} - -interface LogMeta { - context: LogContext; - time: number; -} - -interface BucketInfo { - name: string; - size: number; - createdAt: number; -} - -const DEFAULT_OUTPUTS: Required = { +const DEFAULT_OUTPUTS: DeepRequired = { console: { enabled: true }, tampermonkey: { enabled: false, @@ -60,10 +18,12 @@ const DEFAULT_OUTPUTS: Required = { }, callback: undefined, }; -const DEFAULT_CONFIG: Required = { + +const DEFAULT_CONFIG: DeepRequired = { outputs: DEFAULT_OUTPUTS, bufferCapacity: 100_000, }; + const MESSAGE_STYLE = "background: inherit; color: inherit;"; const STYLES = { @@ -148,7 +108,7 @@ export function stringifyInstance(instance: {}) { } export function objectifyInstance(instance: any) { - let ret = {}; + let ret: Record = {}; if (typeof instance !== "object") { ret[`${typeof instance}`] = instance; } @@ -181,7 +141,7 @@ export class Logger { private buffer: string[]; private bufferLength: number; private bucketIndex: BucketInfo[]; - private outputs: Required; + private outputs: DeepRequired; private bufferCapacity: number; constructor(config: LogConfig = JSON.parse(JSON.stringify(DEFAULT_CONFIG))) { @@ -260,8 +220,9 @@ export class Logger { this.flush(); } else { while (this.bufferLength >= this.bufferCapacity) { - let stale = this.buffer.shift(); - this.bufferLength -= stale.length; + const stale = this.buffer.shift(); + const offset = stale ? stale.length : 0; + this.bufferLength -= offset; } } } @@ -270,7 +231,7 @@ export class Logger { trace(message: string, context?: Object) { this.log(message, { level: LogLevel.TRACE, - stacktrace: new Error().stack.slice(13), // Remove the "Error\n at " + stacktrace: new Error().stack?.slice(13), // Remove the "Error\n at " ...context, }); } diff --git a/src/types/generic.ts b/src/types/generic.ts new file mode 100644 index 0000000..5e83392 --- /dev/null +++ b/src/types/generic.ts @@ -0,0 +1,3 @@ +export type DeepRequired = { + [K in keyof T]: DeepRequired; +} & Required; diff --git a/src/types/logger.ts b/src/types/logger.ts new file mode 100644 index 0000000..9ae1e06 --- /dev/null +++ b/src/types/logger.ts @@ -0,0 +1,52 @@ +export enum LogLevel { + TRACE = 10, + DEBUG = 20, + INFO = 30, + WARN = 40, + FATAL = 50, +} + +export enum LogLabel { + TRACE = "trace", + DEBUG = "debug", + INFO = "info", + WARN = "warn", + FATAL = "fatal", +} + +export interface TampermonkeyOutputOpts { + enabled: boolean; + maxBuckets?: number; + bucketIndexKey?: string; +} + +export interface ConsoleOutputOpts { + enabled: boolean; +} + +export interface LogOutputs { + console?: ConsoleOutputOpts; + tampermonkey?: TampermonkeyOutputOpts; + callback: ((message: string) => any) | undefined; +} + +export interface LogConfig { + outputs?: LogOutputs; + bufferCapacity?: number; +} + +export interface LogContext { + level?: number; + [key: string]: any; +} + +export interface LogMeta { + context: LogContext; + time: number; +} + +export interface BucketInfo { + name: string; + size: number; + createdAt: number; +} diff --git a/tsconfig.json b/tsconfig.json index c46ae8f..184fe06 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -35,7 +35,7 @@ } /* Specify a set of entries that re-map imports to additional lookup locations. */, // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */ - "types": ["tampermonkey"], + // "types": ["tampermonkey"], /* Specify type package names to be included without being referenced in a source file. */ // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */