1
0
mirror of https://git.hibas.dev/OpenServer/NodeLogging.git synced 2025-07-01 12:41:11 +00:00

making shure to not destroy Error stack permanently

This commit is contained in:
Fabian Stamm
2018-05-10 20:11:53 +02:00
parent 4018b380ea
commit 11bc947205
8 changed files with 72 additions and 28 deletions

View File

@ -31,6 +31,8 @@ const BgWhite = "\x1b[47m"
const maxFileSize = 500000000;
const OriginalErrorStackFunction = (<any>Error.prototype).prepareStackTrace
export class Logging {
private static logFileLocation: string = "./logs/";
public static stdout: boolean = true;
@ -71,11 +73,11 @@ export class Logging {
Logging.errorMessage(error);
return;
}
let m = "";
(<any>error.stack).forEach(e => {
m += e.toString() + "\n";
})
var message = error.name + " " + error.message + "\n" + m;
// let m = "";
// (<any>error.stack).forEach(e => {
// m += e.toString() + "\n";
// })
var message = error.name + " " + error.message + "\n" + error.stack;
Logging.message(LoggingTypes.Error, [message]);
}
@ -298,18 +300,38 @@ function fsMkDir(path) {
});
}
function getStack() {
// Save original Error.prepareStackTrace
let origPrepareStackTrace = (<any>Error).prepareStackTrace;
// Override with function that just returns `stack`
(<any>Error).prepareStackTrace = function (_, stack) {
return stack
}
// Create a new `Error`, which automatically gets `stack`
let err = new Error();
// Evaluate `err.stack`, which calls our new `Error.prepareStackTrace`
let stack: any[] = <any>err.stack;
// Restore original `Error.prepareStackTrace`
(<any>Error).prepareStackTrace = origPrepareStackTrace;
// Remove superfluous function call on stack
stack.shift(); // getStack --> Error
return stack
}
function _getCallerFile() {
try {
var err = new Error();
var caller_file: string;
var current_file: string;
let stack = getStack()
(<any>Error).prepareStackTrace = function (err, stack) { return stack; };
let current_file = stack.shift().getFileName();
current_file = (<any>err.stack).shift().getFileName();
while (err.stack.length) {
caller_file = (<any>err.stack).shift().getFileName();
while (stack.length) {
let caller_file = stack.shift().getFileName();
if (current_file !== caller_file) return path.basename(caller_file);
}
} catch (err) { }

View File

@ -9,6 +9,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")
let err = new Error()
if (typeof err.stack !== "string") console.log("Stacktrace invalid", err.stack)
Logging.stdout = false;
for (let i = 0; i < 7000; i++) {
Logging.log(randomBytes(50000).toString("hex"))