feat: update console

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-06-02 17:15:54 +08:00
parent d407631072
commit 7e1111470c
2 changed files with 28 additions and 6 deletions

View File

@ -128,19 +128,23 @@ export class MiaoScriptConsole implements Console {
lineNumber lineNumber
} }
} }
stack(ex: Error): string[] { stack(ex: Error, color: boolean = true): string[] {
let stack = ex.getStackTrace(); let stack = ex.getStackTrace();
let cache = ['§c' + ex]; let cache = [(color ? '§c' : '') + ex];
//@ts-ignore //@ts-ignore
if (stack.class) { if (stack.class) {
stack = Arrays.asList(stack) stack = Arrays.asList(stack)
} }
stack.forEach(trace => { stack.forEach(trace => {
if (!trace.fileName || trace.fileName.startsWith('jar:file:')) { return } if (!trace.fileName || trace.fileName.startsWith('jar:file:') || trace.fileName.startsWith('file:')) { return }
if (trace.className.startsWith('<')) { if (trace.className.startsWith('<')) {
let { fileName, lineNumber } = this.readSourceMap(trace.fileName, trace.lineNumber) let { fileName, lineNumber } = this.readSourceMap(trace.fileName, trace.lineNumber)
if (fileName.startsWith(root)) { fileName = fileName.split(root)[1] } if (fileName.startsWith(root)) { fileName = fileName.split(root)[1] }
cache.push(` §e->§c ${fileName}:${lineNumber} => §4${trace.methodName}`) if (color) {
cache.push(` §e->§c ${fileName}:${lineNumber} => §4${trace.methodName}`)
} else {
cache.push(` -> ${fileName}:${lineNumber} => ${trace.methodName}`)
}
} else { } else {
let className = trace.className; let className = trace.className;
var fileName = trace.fileName as string; var fileName = trace.fileName as string;
@ -155,7 +159,11 @@ export class MiaoScriptConsole implements Console {
} }
} }
} }
cache.push(` §e->§c ${className}.${trace.methodName}(§4${fileName}:${lineNumber}§c)`); if (color) {
cache.push(` §e->§c ${className}.${trace.methodName}(§4${fileName}:${lineNumber}§c)`);
} else {
cache.push(` -> ${className}.${trace.methodName}(${fileName}:${lineNumber})`);
}
} }
}); });
return cache; return cache;

View File

@ -32,8 +32,22 @@ function mcColor2ANSI(str: string) {
} }
export class SpringConsole extends MiaoScriptConsole { export class SpringConsole extends MiaoScriptConsole {
error(...args: any[]) {
this.logger.error(args.join(' '))
}
sender(sender: any, ...args: any[]) { sender(sender: any, ...args: any[]) {
this.console(args.join(' ')) sender = sender || {
sendMessage: (message: string) => console.console(message)
}
if (!sender.sendMessage) {
this.error('第一个参数未实现 sendMessage 无法发送消息!')
return
}
if (Object.prototype.toString.call(args[0]) === '[object Array]') {
args[0].forEach(line => sender.sendMessage(this.prefix + line))
} else {
sender.sendMessage(this.prefix + args.join(' '));
}
} }
console(...args: string[]): void { console(...args: string[]): void {
this.logger.info(mcColor2ANSI(args.join(' ') + '§r')) this.logger.info(mcColor2ANSI(args.join(' ') + '§r'))