Compare commits

..

2 Commits

Author SHA1 Message Date
Fabian Stamm
b647b8fae6 Merge branch 'master' of https://git.stamm.me/OpenServer/Logging 2020-04-09 17:47:43 +02:00
Fabian Stamm
ce9742a20e Also adding prefix to browser console output 2020-03-21 21:13:46 +01:00
2 changed files with 146 additions and 134 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "@hibas123/logging", "name": "@hibas123/logging",
"version": "2.1.4", "version": "2.1.5",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -1,154 +1,166 @@
import { ObservableInterface } from "@hibas123/utils"; import { ObservableInterface } from "@hibas123/utils";
import { Colors } from "./index"; import { Colors } from "./index";
import { import {
<<<<<<< HEAD
Adapter,
Message,
FormattedLine,
TerminalFormats,
FormatTypes
=======
Adapter, Adapter,
Message, Message,
FormattedLine, FormattedLine,
TerminalFormats, TerminalFormats,
FormatTypes, FormatTypes,
>>>>>>> facb7e7b403e1dfaa8790426259e6effb0618efb
} from "./types"; } from "./types";
const browser = typeof window !== "undefined"; const browser = typeof window !== "undefined";
export class ConsoleAdapter implements Adapter { export class ConsoleAdapter implements Adapter {
init(observable: ObservableInterface<Message>) { init(observable: ObservableInterface<Message>) {
observable.subscribe(this.onMessage.bind(this)); observable.subscribe(this.onMessage.bind(this));
} }
<<<<<<< HEAD
flush() {}
=======
flush() {} flush() {}
>>>>>>> facb7e7b403e1dfaa8790426259e6effb0618efb
// TODO: Check if required! // TODO: Check if required!
// private escape(text: string): string { // private escape(text: string): string {
// return text // return text
// .replace(/%s/g, "%%s") // .replace(/%s/g, "%%s")
// .replace(/%c/g, "%%c") // .replace(/%c/g, "%%c")
// } // }
private formatLine(line: FormattedLine): [string, string[] | undefined] { private formatLine(line: FormattedLine): [string, string[] | undefined] {
let text = ""; let text = "";
let style_formats: string[] = []; let style_formats: string[] = [];
if (!browser) { if (!browser) {
for (let part of line) { for (let part of line) {
let formats = ""; let formats = "";
for (let format of part.formats) { for (let format of part.formats) {
switch (format.type) { switch (format.type) {
case FormatTypes.BOLD: case FormatTypes.BOLD:
formats += TerminalFormats.Bold; formats += TerminalFormats.Bold;
break; break;
case FormatTypes.UNDERSCORE: case FormatTypes.UNDERSCORE:
formats += TerminalFormats.Underscore; formats += TerminalFormats.Underscore;
break; break;
case FormatTypes.BLINK: case FormatTypes.BLINK:
formats += TerminalFormats.Blink; formats += TerminalFormats.Blink;
break; break;
case FormatTypes.COLOR: case FormatTypes.COLOR:
switch (format.color) { switch (format.color) {
case Colors.RED: case Colors.RED:
formats += TerminalFormats.FgRed; formats += TerminalFormats.FgRed;
break; break;
case Colors.GREEN: case Colors.GREEN:
formats += TerminalFormats.FgGreen; formats += TerminalFormats.FgGreen;
break; break;
case Colors.YELLOW: case Colors.YELLOW:
formats += TerminalFormats.FgYellow; formats += TerminalFormats.FgYellow;
break; break;
case Colors.BLUE: case Colors.BLUE:
formats += TerminalFormats.FgBlue; formats += TerminalFormats.FgBlue;
break; break;
case Colors.MAGENTA: case Colors.MAGENTA:
formats += TerminalFormats.FgMagenta; formats += TerminalFormats.FgMagenta;
break; break;
case Colors.CYAN: case Colors.CYAN:
formats += TerminalFormats.FgCyan; formats += TerminalFormats.FgCyan;
break; break;
case Colors.WHITE: case Colors.WHITE:
formats += TerminalFormats.FgWhite; formats += TerminalFormats.FgWhite;
break; break;
} }
break; break;
} }
} }
text += formats + part.text + TerminalFormats.Reset; text += formats + part.text + TerminalFormats.Reset;
}
} else {
for (let part of line) {
let styles: string[] = [];
let resetStyles: string[] = [];
for (let format of part.formats) {
switch (format.type) {
case FormatTypes.BOLD:
styles.push("font-weight: bold;");
resetStyles.push("font-weight: unset");
break;
case FormatTypes.UNDERSCORE:
styles.push("text-decoration: underline");
resetStyles.push("text-decoration: unset");
break;
case FormatTypes.BLINK:
styles.push("text-decoration: blink");
resetStyles.push("text-decoration: unset");
break;
case FormatTypes.COLOR:
let color = "";
switch (format.color) {
case Colors.RED:
color = "red";
break;
case Colors.GREEN:
color = "green";
break;
case Colors.YELLOW:
color = "gold";
break;
case Colors.BLUE:
color = "blue";
break;
case Colors.MAGENTA:
color = "magenta";
break;
case Colors.CYAN:
color = "cyan";
break;
case Colors.WHITE:
color = "white";
break;
}
styles.push("color: " + color);
resetStyles.push("color: unset");
break;
}
}
text += "%c" + part.text.replace(/%c/g, "%%c") + "%c";
style_formats.push(styles.join(";"), resetStyles.join(";"));
}
} }
} else {
return [text, style_formats]; for (let part of line) {
} let styles: string[] = [];
let resetStyles: string[] = [];
onMessage(message: Message) { for (let format of part.formats) {
let lines = message.text.formatted; switch (format.type) {
case FormatTypes.BOLD:
let prefix = ""; styles.push("font-weight: bold;");
if (message.name) prefix = `[${message.name}]=>`; resetStyles.push("font-weight: unset");
break;
if (browser) { case FormatTypes.UNDERSCORE:
let formats: string[] = []; styles.push("text-decoration: underline");
let text = lines resetStyles.push("text-decoration: unset");
.map((line) => { break;
let [t, fmts] = this.formatLine(line); case FormatTypes.BLINK:
formats.push(...fmts); styles.push("text-decoration: blink");
return t; resetStyles.push("text-decoration: unset");
}) break;
.join("\n"); case FormatTypes.COLOR:
// console.log(formats); let color = "";
console.log(text, ...formats); switch (format.color) {
} else { case Colors.RED:
lines.forEach((line) => { color = "red";
let [text] = this.formatLine(line); break;
console.log(prefix + text); case Colors.GREEN:
}); color = "green";
break;
case Colors.YELLOW:
color = "gold";
break;
case Colors.BLUE:
color = "blue";
break;
case Colors.MAGENTA:
color = "magenta";
break;
case Colors.CYAN:
color = "cyan";
break;
case Colors.WHITE:
color = "white";
break;
}
styles.push("color: " + color);
resetStyles.push("color: unset");
break;
}
}
text += "%c" + part.text.replace(/%c/g, "%%c") + "%c";
style_formats.push(styles.join(";"), resetStyles.join(";"));
} }
} }
return [text, style_formats];
}
onMessage(message: Message) {
let lines = message.text.formatted;
let prefix = "";
if (message.name) prefix = `[${message.name}]=>`;
if (browser) {
let formats: string[] = [];
let text = lines
.map(line => {
let [t, fmts] = this.formatLine(line);
formats.push(...fmts);
return prefix + t;
})
.join("\n");
// console.log(formats);
console.log(text, ...formats);
} else {
lines.forEach(line => {
let [text] = this.formatLine(line);
console.log(prefix + text);
});
}
}
} }