You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Hydro/hydro/lib/logger.ts

48 lines
1.2 KiB
TypeScript

import yaml from 'js-yaml';
function wrap(func: Function, type: string) {
return (...args: any) => {
const time = new Date();
func(...args);
if (global.Hydro.model.message) {
4 years ago
try {
for (let i = 0; i <= args.length; i++) {
if (args[i] instanceof Error) {
// js-yaml cannot dump [object Error]
args[i] = `${args[i].message}\n${args[i].stack}`;
}
}
global.Hydro.model.message.send(1, 1, yaml.dump({ time, type, args }, {}));
} catch (e) {
func(e.message);
}
}
};
}
class Logger {
log: (...args: any[]) => void;
error: (...args: any[]) => void;
info: (...args: any[]) => void;
warn: (...args: any[]) => void;
debug: (...args: any[]) => void;
constructor() {
this.log = wrap(console.log, 'log');
this.error = wrap(console.error, 'error');
this.info = wrap(console.info, 'info');
this.warn = wrap(console.warn, 'warn');
this.debug = wrap(console.debug, 'debug');
}
}
const exp = new Logger();
global.Hydro.lib.logger = exp;
export default exp;