mirror of
https://github.com/ClaytonWWilson/stokpile.git
synced 2025-12-16 09:08:46 +00:00
Organize types. Fix TS errors.
This commit is contained in:
parent
ace5a16f3a
commit
4229e1ee79
@ -1,57 +1,15 @@
|
|||||||
export enum LogLevel {
|
import type {
|
||||||
TRACE = 10,
|
LogOutputs,
|
||||||
DEBUG = 20,
|
LogContext,
|
||||||
INFO = 30,
|
BucketInfo,
|
||||||
WARN = 40,
|
LogMeta,
|
||||||
FATAL = 50,
|
LogConfig,
|
||||||
}
|
} from "./types/logger";
|
||||||
|
|
||||||
enum LogLabel {
|
import { LogLabel, LogLevel } from "./types/logger";
|
||||||
TRACE = "trace",
|
import type { DeepRequired } from "generic";
|
||||||
DEBUG = "debug",
|
|
||||||
INFO = "info",
|
|
||||||
WARN = "warn",
|
|
||||||
FATAL = "fatal",
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface TampermonkeyOutputOpts {
|
const DEFAULT_OUTPUTS: DeepRequired<LogOutputs> = {
|
||||||
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<LogOutputs> = {
|
|
||||||
console: { enabled: true },
|
console: { enabled: true },
|
||||||
tampermonkey: {
|
tampermonkey: {
|
||||||
enabled: false,
|
enabled: false,
|
||||||
@ -60,10 +18,12 @@ const DEFAULT_OUTPUTS: Required<LogOutputs> = {
|
|||||||
},
|
},
|
||||||
callback: undefined,
|
callback: undefined,
|
||||||
};
|
};
|
||||||
const DEFAULT_CONFIG: Required<LogConfig> = {
|
|
||||||
|
const DEFAULT_CONFIG: DeepRequired<LogConfig> = {
|
||||||
outputs: DEFAULT_OUTPUTS,
|
outputs: DEFAULT_OUTPUTS,
|
||||||
bufferCapacity: 100_000,
|
bufferCapacity: 100_000,
|
||||||
};
|
};
|
||||||
|
|
||||||
const MESSAGE_STYLE = "background: inherit; color: inherit;";
|
const MESSAGE_STYLE = "background: inherit; color: inherit;";
|
||||||
|
|
||||||
const STYLES = {
|
const STYLES = {
|
||||||
@ -148,7 +108,7 @@ export function stringifyInstance(instance: {}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function objectifyInstance(instance: any) {
|
export function objectifyInstance(instance: any) {
|
||||||
let ret = {};
|
let ret: Record<string, any> = {};
|
||||||
if (typeof instance !== "object") {
|
if (typeof instance !== "object") {
|
||||||
ret[`${typeof instance}`] = instance;
|
ret[`${typeof instance}`] = instance;
|
||||||
}
|
}
|
||||||
@ -181,7 +141,7 @@ export class Logger {
|
|||||||
private buffer: string[];
|
private buffer: string[];
|
||||||
private bufferLength: number;
|
private bufferLength: number;
|
||||||
private bucketIndex: BucketInfo[];
|
private bucketIndex: BucketInfo[];
|
||||||
private outputs: Required<LogOutputs>;
|
private outputs: DeepRequired<LogOutputs>;
|
||||||
private bufferCapacity: number;
|
private bufferCapacity: number;
|
||||||
|
|
||||||
constructor(config: LogConfig = JSON.parse(JSON.stringify(DEFAULT_CONFIG))) {
|
constructor(config: LogConfig = JSON.parse(JSON.stringify(DEFAULT_CONFIG))) {
|
||||||
@ -260,8 +220,9 @@ export class Logger {
|
|||||||
this.flush();
|
this.flush();
|
||||||
} else {
|
} else {
|
||||||
while (this.bufferLength >= this.bufferCapacity) {
|
while (this.bufferLength >= this.bufferCapacity) {
|
||||||
let stale = this.buffer.shift();
|
const stale = this.buffer.shift();
|
||||||
this.bufferLength -= stale.length;
|
const offset = stale ? stale.length : 0;
|
||||||
|
this.bufferLength -= offset;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -270,7 +231,7 @@ export class Logger {
|
|||||||
trace(message: string, context?: Object) {
|
trace(message: string, context?: Object) {
|
||||||
this.log(message, {
|
this.log(message, {
|
||||||
level: LogLevel.TRACE,
|
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,
|
...context,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
3
src/types/generic.ts
Normal file
3
src/types/generic.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export type DeepRequired<T> = {
|
||||||
|
[K in keyof T]: DeepRequired<T[K]>;
|
||||||
|
} & Required<T>;
|
||||||
52
src/types/logger.ts
Normal file
52
src/types/logger.ts
Normal file
@ -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;
|
||||||
|
}
|
||||||
@ -35,7 +35,7 @@
|
|||||||
} /* Specify a set of entries that re-map imports to additional lookup locations. */,
|
} /* 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. */
|
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
||||||
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
|
// "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. */
|
/* Specify type package names to be included without being referenced in a source file. */
|
||||||
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
||||||
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
|
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user