This commit is contained in:
Fabian
2019-11-17 16:44:57 +01:00
6 changed files with 129 additions and 94 deletions

View File

@ -157,26 +157,13 @@ export class LoggingBase {
this.message(LoggingTypes.Error, message);
}
private message(type: LoggingTypes, message: any[] | string, caller?: { file: string, line: number }) {
private message(type: LoggingTypes, message: any[], caller?: { file: string, line: number }) {
let date = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
let file_raw = caller || getCallerFile();
let file = `${file_raw.file}:${String(file_raw.line).padEnd(3, " ")}`;
let mb = "";
if (typeof message === "string") {
mb = message;
} else {
message.forEach((e, i) => {
if (typeof e !== "string") e = inspect(e, { colors: true, showHidden: true, depth: 3 });
if (e.endsWith("\n") || i === message.length - 1) {
mb += e;
} else {
mb += e + " ";
}
});
}
let lines = removeColors(mb).split("\n");
let type_str = LoggingTypes[type].toUpperCase().padEnd(5, " ");
let type_format: Format[] = [];
switch (type) {
case LoggingTypes.Log:
@ -193,12 +180,7 @@ export class LoggingBase {
break;
}
let date = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
let type_str = LoggingTypes[type].toUpperCase().padEnd(5, " ");
const prefix: FormattedText[] = [];
const a = (text: string, formats: Format[] = []) => {
prefix.push({
text,
@ -216,18 +198,56 @@ export class LoggingBase {
}
a("]: ");
let formatted: FormattedLine[] = lines.map<FormattedText[]>(line => [...prefix, {
text: line,
formats: []
}]);
let raw: string[] = [];
const formatted: FormattedLine[] = [];
let line: FormattedLine;
const newLine = () => {
if (line && line.length > 0) {
formatted.push(line);
raw.push(line.map(e => e.text).join(""));
}
line = [...prefix];
}
newLine();
message.forEach((e, i) => {
let formats: Format[] = [];
if (typeof e !== "string") {
if (typeof e === "object") {
if (e[colorSymbol]) {
formats.push({
type: FormatTypes.COLOR,
color: e[colorSymbol]
})
e = e.value;
}
}
if (typeof e !== "string")
e = inspect(e, { colors: true, showHidden: true, depth: 3 }) as string;
}
removeColors(e).split("\n").map((text, index, { length }) => {
line.push({ text, formats });
if (index < length - 1) {
newLine();
}
})
if (!e.endsWith("\n") && i < message.length - 1) {
line.push({ text: " ", formats: [] });
}
});
newLine();
let msg: Message = {
date: new Date(),
file,
name: this._name,
text: {
raw: lines,
raw,
formatted
},
type
@ -237,6 +257,20 @@ export class LoggingBase {
}
}
const colorSymbol = Symbol("color");
export interface ColorFormat {
[colorSymbol]: Colors;
value: any;
}
export function withColor(color: Colors, value: any): ColorFormat {
return {
[colorSymbol]: color,
value
}
}
function getStack() {
// Save original Error.prepareStackTrace
let origPrepareStackTrace = (<any>Error).prepareStackTrace;

View File

@ -1,6 +1,6 @@
import { LoggingBase } from "./base";
export { ConsoleAdapter } from "./consolewriter";
export { LoggingBase, LoggingBaseOptions, removeColors } from "./base";
export { LoggingBase, LoggingBaseOptions, removeColors, withColor } from "./base";
export {
Adapter,
LoggingTypes,

View File

@ -1,5 +1,4 @@
import { Logging, LoggingBase } from ".";
import { LoggingTypes } from "./types";
import { Logging, LoggingBase, LoggingTypes, Colors, withColor } from ".";
Logging.log("test")
Logging.log("i", "am", { a: "an" }, 1000);
@ -9,6 +8,9 @@ Logging.errorMessage("i", "am", "an", "error");
Logging.log("\x1b[31m\x1b[31m\x1b[31m\x1b[31m\x1b[31m\x1b[31m TEST \x1b[31m\x1b[31m\x1b[31m")
Logging.log(withColor(Colors.MAGENTA, "This text should be magenta!"), "This not!")
Logging.log(withColor(Colors.MAGENTA, { somekey: "Some value" }))
let err = new Error()
if (typeof err.stack !== "string") console.log("Stacktrace invalid", err.stack)

View File

@ -43,7 +43,6 @@ export enum FormatTypes {
BLINK
}
export enum Colors {
NONE,
RED,