Switch file resolving
This commit is contained in:
parent
30f5e241ae
commit
ca896c1c34
17
.vscode/launch.json
vendored
Normal file
17
.vscode/launch.json
vendored
Normal 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"]
|
||||
}
|
||||
]
|
||||
}
|
1038
package-lock.json
generated
1038
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
12
package.json
12
package.json
@ -1,16 +1,13 @@
|
||||
{
|
||||
"name": "@hibas123/logging",
|
||||
"version": "2.3.5",
|
||||
"version": "2.4.0",
|
||||
"description": "",
|
||||
"main": "out/index.js",
|
||||
"types": "out/index.d.ts",
|
||||
"scripts": {
|
||||
"prepublish": "tsc",
|
||||
"build": "tsc",
|
||||
"watch-ts": "tsc --watch",
|
||||
"watch-js": "nodemon out/test.js",
|
||||
"watch": "concurrently npm:watch-*",
|
||||
"test": "node out/test.js"
|
||||
"dev": "nodemon -e ts --exec ts-node src/test.ts"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@ -26,10 +23,11 @@
|
||||
],
|
||||
"devDependencies": {
|
||||
"concurrently": "^5.1.0",
|
||||
"nodemon": "^2.0.2",
|
||||
"nodemon": "^2.0.3",
|
||||
"ts-node": "^8.8.2",
|
||||
"typescript": "^3.8.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"@hibas123/utils": "^2.2.3"
|
||||
"@hibas123/utils": "^2.2.4"
|
||||
}
|
||||
}
|
||||
|
65
src/base.ts
65
src/base.ts
@ -60,6 +60,10 @@ class AdapterSet {
|
||||
|
||||
const consoleAdapter = new ConsoleAdapter();
|
||||
|
||||
declare var process: { cwd: () => string };
|
||||
|
||||
const PROJECT_ROOT = typeof process !== "undefined" ? process.cwd() : undefined;
|
||||
|
||||
export class LoggingBase {
|
||||
private _formatMap: FormatConfig = new DefaultFormatConfig();
|
||||
|
||||
@ -311,14 +315,34 @@ export class LoggingBase {
|
||||
private message(
|
||||
type: LoggingTypes,
|
||||
message: any[],
|
||||
caller?: { file: string; line: number }
|
||||
caller?: { file: string; line: number; column?: number }
|
||||
) {
|
||||
if (this.$closed) return;
|
||||
|
||||
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 file_raw = caller;
|
||||
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_format: Format[] = [];
|
||||
@ -455,10 +479,6 @@ function getStack() {
|
||||
}
|
||||
}
|
||||
|
||||
function baseName(path) {
|
||||
return path.split(/[\\/]/).pop();
|
||||
}
|
||||
|
||||
function getCallerFile() {
|
||||
try {
|
||||
let stack = getStack();
|
||||
@ -467,28 +487,45 @@ function getCallerFile() {
|
||||
|
||||
while (stack.length) {
|
||||
let caller_file = stack.shift();
|
||||
if (current_file !== caller_file.getFileName())
|
||||
if (current_file !== caller_file.getFileName()) {
|
||||
console.log(Object.keys(caller_file));
|
||||
return {
|
||||
file: baseName(caller_file.getFileName()),
|
||||
file: caller_file.getFileName(),
|
||||
line: caller_file.getLineNumber(),
|
||||
column: caller_file.getColumnNumber(),
|
||||
};
|
||||
}
|
||||
}
|
||||
} catch (err) {}
|
||||
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 };
|
||||
let lines = err.stack.split("\n");
|
||||
lines.shift(); // removing first line
|
||||
while (lines.length > 0) {
|
||||
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) {
|
||||
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 {
|
||||
file: f,
|
||||
line: Number(line),
|
||||
file,
|
||||
line,
|
||||
column,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,8 @@ import {
|
||||
const browser = typeof window !== "undefined";
|
||||
|
||||
export class ConsoleAdapter implements Adapter {
|
||||
constructor(private colors?: boolean) {}
|
||||
|
||||
init(observable: ObservableInterface<Message>) {
|
||||
observable.subscribe(this.onMessage.bind(this));
|
||||
}
|
||||
@ -134,6 +136,7 @@ export class ConsoleAdapter implements Adapter {
|
||||
if (message.name) prefix = `[${message.name}]=>`;
|
||||
|
||||
if (browser) {
|
||||
if (this.colors) {
|
||||
let formats: string[] = [];
|
||||
let text = lines
|
||||
.map((line) => {
|
||||
@ -145,10 +148,17 @@ export class ConsoleAdapter implements Adapter {
|
||||
// console.log(formats);
|
||||
console.log(text, ...formats);
|
||||
} else {
|
||||
console.log(message.text.raw.join("\n"));
|
||||
}
|
||||
} else {
|
||||
if (this.colors) {
|
||||
lines.forEach((line) => {
|
||||
let [text] = this.formatLine(line);
|
||||
console.log(prefix + text);
|
||||
});
|
||||
} else {
|
||||
message.text.raw.forEach(console.log.bind(console));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user