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.
48 lines
1.2 KiB
TypeScript
48 lines
1.2 KiB
TypeScript
4 years ago
|
import yaml from 'js-yaml';
|
||
4 years ago
|
|
||
4 years ago
|
function wrap(func: Function, type: string) {
|
||
|
return (...args: any) => {
|
||
4 years ago
|
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);
|
||
|
}
|
||
4 years ago
|
}
|
||
|
};
|
||
|
}
|
||
|
|
||
|
class Logger {
|
||
4 years ago
|
log: (...args: any[]) => void;
|
||
|
|
||
|
error: (...args: any[]) => void;
|
||
|
|
||
|
info: (...args: any[]) => void;
|
||
|
|
||
|
warn: (...args: any[]) => void;
|
||
|
|
||
|
debug: (...args: any[]) => void;
|
||
|
|
||
4 years ago
|
constructor() {
|
||
4 years ago
|
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');
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
const exp = new Logger();
|
||
|
|
||
|
global.Hydro.lib.logger = exp;
|
||
|
|
||
|
export default exp;
|