Switch file resolving
This commit is contained in:
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,21 +136,29 @@ export class ConsoleAdapter implements Adapter {
|
||||
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);
|
||||
if (this.colors) {
|
||||
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 {
|
||||
console.log(message.text.raw.join("\n"));
|
||||
}
|
||||
} else {
|
||||
lines.forEach((line) => {
|
||||
let [text] = this.formatLine(line);
|
||||
console.log(prefix + text);
|
||||
});
|
||||
if (this.colors) {
|
||||
lines.forEach((line) => {
|
||||
let [text] = this.formatLine(line);
|
||||
console.log(prefix + text);
|
||||
});
|
||||
} else {
|
||||
message.text.raw.forEach(console.log.bind(console));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user