Logging/src/types.ts

120 lines
2.1 KiB
TypeScript
Raw Normal View History

import { ObservableInterface } from "@hibas123/utils";
export enum LoggingTypes {
2020-04-09 15:50:30 +00:00
Debug,
Log,
Warning,
Error,
}
2019-04-05 02:22:52 +00:00
export const TerminalFormats = {
2020-04-09 15:50:30 +00:00
Reset: "\x1b[0m",
Bold: "\x1b[1m",
Underscore: "\x1b[4m",
Blink: "\x1b[5m",
Reverse: "\x1b[7m",
Hidden: "\x1b[8m",
FgBlack: "\x1b[30m",
FgRed: "\x1b[31m",
FgGreen: "\x1b[32m",
FgYellow: "\x1b[33m",
FgBlue: "\x1b[34m",
FgMagenta: "\x1b[35m",
FgCyan: "\x1b[36m",
FgWhite: "\x1b[37m",
BgBlack: "\x1b[40m",
BgRed: "\x1b[41m",
BgGreen: "\x1b[42m",
BgYellow: "\x1b[43m",
BgBlue: "\x1b[44m",
BgMagenta: "\x1b[45m",
BgCyan: "\x1b[46m",
BgWhite: "\x1b[47m",
};
2019-04-05 02:22:52 +00:00
export enum FormatTypes {
2020-04-09 15:50:30 +00:00
COLOR,
BOLD,
UNDERSCORE,
BLINK,
2019-04-05 02:22:52 +00:00
}
export enum Colors {
2020-04-09 15:50:30 +00:00
NONE,
RED,
GREEN,
YELLOW,
BLUE,
MAGENTA,
CYAN,
WHITE,
2019-04-05 02:22:52 +00:00
}
export interface FormatConfig {
2020-04-09 15:50:30 +00:00
error: Format[];
warning: Format[];
log: Format[];
debug: Format[];
2019-04-05 02:22:52 +00:00
2020-04-09 15:50:30 +00:00
date: Format[];
file: Format[];
2019-04-05 02:22:52 +00:00
}
function colorFormat(color: Colors) {
2020-04-09 15:50:30 +00:00
return {
type: FormatTypes.COLOR,
color,
};
2019-04-05 02:22:52 +00:00
}
const boldFormat = {
2020-04-09 15:50:30 +00:00
type: FormatTypes.BOLD,
2019-04-05 02:22:52 +00:00
};
export class DefaultFormatConfig implements FormatConfig {
2020-04-09 15:50:30 +00:00
error = [colorFormat(Colors.RED), boldFormat];
warning = [colorFormat(Colors.YELLOW), boldFormat];
log = [colorFormat(Colors.NONE), boldFormat];
debug = [colorFormat(Colors.CYAN), boldFormat];
2019-04-05 02:22:52 +00:00
2020-04-09 15:50:30 +00:00
date = [colorFormat(Colors.NONE)];
file = [colorFormat(Colors.NONE)];
2019-04-05 02:22:52 +00:00
}
export interface Format {
2020-04-09 15:50:30 +00:00
type: FormatTypes;
color?: Colors;
2019-04-05 02:22:52 +00:00
}
export interface FormattedText {
2020-04-09 15:50:30 +00:00
text: string;
formats: Format[];
2019-04-05 02:22:52 +00:00
}
export type FormattedLine = FormattedText[];
export interface Message {
2020-04-09 15:50:30 +00:00
type: LoggingTypes;
name?: string;
text: {
raw: string[];
formatted: FormattedLine[];
};
date: Date;
file: string;
}
export interface Adapter {
2020-04-09 15:50:30 +00:00
init(
observable: ObservableInterface<Message>,
name?: string
): void | Promise<void>;
2020-04-09 15:50:30 +00:00
flush(sync: true): void;
flush(sync: false): void | Promise<void>;
2020-04-09 15:50:30 +00:00
close?(): void;
}