Switch file resolving

This commit is contained in:
Fabian Stamm 2020-04-15 19:51:15 +02:00
parent 30f5e241ae
commit ca896c1c34
5 changed files with 732 additions and 440 deletions

17
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"request": "launch",
"name": "Launch Program",
"skipFiles": ["<node_internals>/**"],
"program": "${workspaceFolder}\\out\\test.js",
"preLaunchTask": "tsc: build - tsconfig.json",
"outFiles": ["${workspaceFolder}/out/**/*.js"]
}
]
}

1040
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,16 +1,13 @@
{ {
"name": "@hibas123/logging", "name": "@hibas123/logging",
"version": "2.3.5", "version": "2.4.0",
"description": "", "description": "",
"main": "out/index.js", "main": "out/index.js",
"types": "out/index.d.ts", "types": "out/index.d.ts",
"scripts": { "scripts": {
"prepublish": "tsc", "prepublish": "tsc",
"build": "tsc", "build": "tsc",
"watch-ts": "tsc --watch", "dev": "nodemon -e ts --exec ts-node src/test.ts"
"watch-js": "nodemon out/test.js",
"watch": "concurrently npm:watch-*",
"test": "node out/test.js"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@ -26,10 +23,11 @@
], ],
"devDependencies": { "devDependencies": {
"concurrently": "^5.1.0", "concurrently": "^5.1.0",
"nodemon": "^2.0.2", "nodemon": "^2.0.3",
"ts-node": "^8.8.2",
"typescript": "^3.8.3" "typescript": "^3.8.3"
}, },
"dependencies": { "dependencies": {
"@hibas123/utils": "^2.2.3" "@hibas123/utils": "^2.2.4"
} }
} }

View File

@ -60,6 +60,10 @@ class AdapterSet {
const consoleAdapter = new ConsoleAdapter(); const consoleAdapter = new ConsoleAdapter();
declare var process: { cwd: () => string };
const PROJECT_ROOT = typeof process !== "undefined" ? process.cwd() : undefined;
export class LoggingBase { export class LoggingBase {
private _formatMap: FormatConfig = new DefaultFormatConfig(); private _formatMap: FormatConfig = new DefaultFormatConfig();
@ -311,14 +315,34 @@ export class LoggingBase {
private message( private message(
type: LoggingTypes, type: LoggingTypes,
message: any[], message: any[],
caller?: { file: string; line: number } caller?: { file: string; line: number; column?: number }
) { ) {
if (this.$closed) return; if (this.$closed) return;
let date = new Date().toISOString().replace(/T/, " ").replace(/\..+/, ""); let date = new Date().toISOString().replace(/T/, " ").replace(/\..+/, "");
let file_raw = caller || getCallerFile(); let file_raw = caller;
let file = `${file_raw.file}:${String(file_raw.line).padEnd(3, " ")}`; if (!file_raw) {
try {
file_raw = getCallerFile();
} catch (err) {
file_raw = {
file: "<unknown>",
line: 0,
column: 0,
};
}
}
if (PROJECT_ROOT && file_raw.file.startsWith(PROJECT_ROOT)) {
let newF = file_raw.file.substring(PROJECT_ROOT.length);
if (newF.startsWith("/") || newF.startsWith("\\"))
newF = newF.substring(1);
file_raw.file = newF;
}
let file = `${file_raw.file}:${file_raw.line}:${file_raw.column || 0}`;
let type_str = LoggingTypes[type].toUpperCase().padEnd(5, " "); let type_str = LoggingTypes[type].toUpperCase().padEnd(5, " ");
let type_format: Format[] = []; let type_format: Format[] = [];
@ -455,10 +479,6 @@ function getStack() {
} }
} }
function baseName(path) {
return path.split(/[\\/]/).pop();
}
function getCallerFile() { function getCallerFile() {
try { try {
let stack = getStack(); let stack = getStack();
@ -467,28 +487,45 @@ function getCallerFile() {
while (stack.length) { while (stack.length) {
let caller_file = stack.shift(); let caller_file = stack.shift();
if (current_file !== caller_file.getFileName()) if (current_file !== caller_file.getFileName()) {
console.log(Object.keys(caller_file));
return { return {
file: baseName(caller_file.getFileName()), file: caller_file.getFileName(),
line: caller_file.getLineNumber(), line: caller_file.getLineNumber(),
column: caller_file.getColumnNumber(),
}; };
}
} }
} catch (err) {} } catch (err) {}
return { file: undefined, line: 0 }; return { file: undefined, line: 0 };
} }
function getCallerFromExisting(err: Error): { file: string; line: number } { function getCallerFromExisting(
err: Error
): { file: string; line: number; column?: number } {
if (!err || !err.stack) return { file: "NOFILE", line: 0 }; if (!err || !err.stack) return { file: "NOFILE", line: 0 };
let lines = err.stack.split("\n"); let lines = err.stack.split("\n");
lines.shift(); // removing first line lines.shift(); // removing first line
while (lines.length > 0) { while (lines.length > 0) {
let line = lines.shift(); let line = lines.shift();
let matches = line.match(/[a-zA-Z_-]+[.][a-zA-Z_-]+[:][0-9]+/g); let matches = line.match(
/[<]?([a-zA-Z]:)?([\/\\]?[a-zA-Z_-])+[.>][a-zA-Z_-]*([:][0-9]+)+/g
);
if (matches && matches.length > 0) { if (matches && matches.length > 0) {
let [f, line] = matches[0].split(":"); let match = matches[0].trim();
let locationString = match.match(/([:][0-9]+)+$/gm)[0];
let line: number;
let column: number;
if (locationString) {
match = match.slice(0, match.length - locationString.length);
locationString = locationString.substring(1);
[line, column] = locationString.split(":").map(Number);
}
let file = match;
return { return {
file: f, file,
line: Number(line), line,
column,
}; };
} }
} }

View File

@ -11,6 +11,8 @@ import {
const browser = typeof window !== "undefined"; const browser = typeof window !== "undefined";
export class ConsoleAdapter implements Adapter { export class ConsoleAdapter implements Adapter {
constructor(private colors?: boolean) {}
init(observable: ObservableInterface<Message>) { init(observable: ObservableInterface<Message>) {
observable.subscribe(this.onMessage.bind(this)); observable.subscribe(this.onMessage.bind(this));
} }
@ -134,21 +136,29 @@ export class ConsoleAdapter implements Adapter {
if (message.name) prefix = `[${message.name}]=>`; if (message.name) prefix = `[${message.name}]=>`;
if (browser) { if (browser) {
let formats: string[] = []; if (this.colors) {
let text = lines let formats: string[] = [];
.map((line) => { let text = lines
let [t, fmts] = this.formatLine(line); .map((line) => {
formats.push(...fmts); let [t, fmts] = this.formatLine(line);
return prefix + t; formats.push(...fmts);
}) return prefix + t;
.join("\n"); })
// console.log(formats); .join("\n");
console.log(text, ...formats); // console.log(formats);
console.log(text, ...formats);
} else {
console.log(message.text.raw.join("\n"));
}
} else { } else {
lines.forEach((line) => { if (this.colors) {
let [text] = this.formatLine(line); lines.forEach((line) => {
console.log(prefix + text); let [text] = this.formatLine(line);
}); console.log(prefix + text);
});
} else {
message.text.raw.forEach(console.log.bind(console));
}
} }
} }
} }