29 lines
892 B
TypeScript
Executable File
29 lines
892 B
TypeScript
Executable File
/* monkey-patch console.log to prefix with file/line-number */
|
|
if (process.env['LOG_LINE']) {
|
|
let cwd = process.cwd(),
|
|
cwdRe = new RegExp("^[^/]*" + cwd.replace("/", "\\/") + "\/([^:]*:[0-9]*).*$");
|
|
[ "log", "warn", "error" ].forEach(function(method: string) {
|
|
(console as any)[method] = (function () {
|
|
let orig = (console as any)[method];
|
|
return function (this: any, ...args: any[]) {
|
|
function getErrorObject(): Error {
|
|
try {
|
|
throw Error('');
|
|
} catch (err) {
|
|
return err as Error;
|
|
}
|
|
}
|
|
|
|
let err = getErrorObject(),
|
|
caller_line = err.stack?.split("\n")[3] || '',
|
|
prefixedArgs = [caller_line.replace(cwdRe, "$1 -")];
|
|
|
|
/* arguments.unshift() doesn't exist... */
|
|
prefixedArgs.push(...args);
|
|
|
|
orig.apply(this, prefixedArgs);
|
|
};
|
|
})();
|
|
});
|
|
}
|