2020-02-26 17:58:23 +00:00
|
|
|
import i18m from '@ms/i18n'
|
2020-02-15 11:39:45 +00:00
|
|
|
import { SourceMapBuilder } from 'source-map-builder'
|
2020-04-24 05:04:45 +00:00
|
|
|
import * as base64 from 'base64-js'
|
2020-02-15 11:39:45 +00:00
|
|
|
|
|
|
|
const Arrays = Java.type('java.util.Arrays');
|
|
|
|
const Level = Java.type('java.util.logging.Level');
|
|
|
|
const Paths = Java.type('java.nio.file.Paths');
|
|
|
|
const ignoreLogPrefix = ['java.', 'net.minecraft.', 'org.bukkit.', 'jdk.nashorn.', 'io.netty.', 'org.spongepowered.'];
|
2020-01-15 08:48:18 +00:00
|
|
|
|
|
|
|
enum LogLevel {
|
|
|
|
ALL,
|
|
|
|
TRACE,
|
|
|
|
DEBUG,
|
|
|
|
INFO,
|
|
|
|
WARN,
|
|
|
|
ERROR,
|
|
|
|
FATAL,
|
|
|
|
OFF
|
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
|
|
|
|
export class MiaoScriptConsole implements Console {
|
|
|
|
Console: NodeJS.ConsoleConstructor;
|
|
|
|
|
2020-02-15 11:39:45 +00:00
|
|
|
private sourceMaps: { [key: string]: SourceMapBuilder } = {};
|
2020-04-24 05:04:45 +00:00
|
|
|
private sourceFileMaps: { [key: string]: string } = {};
|
2019-09-07 04:23:15 +00:00
|
|
|
private _name: string = '';
|
2020-01-31 18:44:23 +00:00
|
|
|
private _level: LogLevel = LogLevel.INFO;
|
2019-09-07 04:23:15 +00:00
|
|
|
|
2019-09-10 09:21:00 +00:00
|
|
|
protected logger: any;
|
2019-09-07 04:23:15 +00:00
|
|
|
protected prefix: string = '§6[§bMiaoScript§6]§r ';
|
|
|
|
|
2019-09-10 09:21:00 +00:00
|
|
|
constructor(name?: string) {
|
|
|
|
this.name = name;
|
2019-09-07 04:23:15 +00:00
|
|
|
this.logger = global.logger;
|
2020-01-31 18:44:23 +00:00
|
|
|
if (global.debug) {
|
|
|
|
this._level = LogLevel.DEBUG
|
|
|
|
}
|
|
|
|
if (global.level?.toUpperCase() === "TRACE") {
|
|
|
|
this._level = LogLevel.TRACE
|
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get name() {
|
|
|
|
return this._name;
|
|
|
|
}
|
|
|
|
|
|
|
|
set name(name: string) {
|
|
|
|
if (name) {
|
|
|
|
this._name = `[${name}] `;
|
|
|
|
// noinspection JSUnusedGlobalSymbols
|
|
|
|
this.prefix = `§6[§cMS§6][§b${name}§6]§r `;
|
|
|
|
}
|
|
|
|
}
|
2020-01-15 08:48:18 +00:00
|
|
|
log(...args: any[]): void {
|
2019-09-07 04:23:15 +00:00
|
|
|
this.logger.info(this.name + args.join(' '));
|
|
|
|
}
|
2020-01-15 08:48:18 +00:00
|
|
|
info(...args: any[]) {
|
2019-09-07 04:23:15 +00:00
|
|
|
this.logger.info(this.name + args.join(' '));
|
2020-01-15 08:48:18 +00:00
|
|
|
}
|
|
|
|
warn(...args: any[]) {
|
2019-09-07 04:23:15 +00:00
|
|
|
this.logger.warning(this.name + args.join(' '));
|
2020-01-15 08:48:18 +00:00
|
|
|
}
|
|
|
|
error(...args: any[]) {
|
2019-09-07 04:23:15 +00:00
|
|
|
this.logger.log(Level.SEVERE, this.name + args.join(' '));
|
2020-01-15 08:48:18 +00:00
|
|
|
}
|
|
|
|
debug(...args: any[]) {
|
2019-09-07 04:23:15 +00:00
|
|
|
if (global.debug) {
|
|
|
|
this.logger.info(this.name + '[DEBUG] ' + args.join(' '));
|
|
|
|
}
|
2020-01-15 08:48:18 +00:00
|
|
|
}
|
|
|
|
trace(...args: any[]): void {
|
|
|
|
if (this._level <= LogLevel.TRACE) {
|
|
|
|
this.logger.info(this.name + '[TRACE] ' + args.join(' '));
|
|
|
|
}
|
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
sender(...args) {
|
|
|
|
this.info(args)
|
|
|
|
}
|
|
|
|
console(...args) {
|
|
|
|
this.info(args)
|
|
|
|
}
|
2020-02-26 17:58:23 +00:00
|
|
|
i18n(name: string, param?: { [key: string]: any }) {
|
2020-02-27 04:11:18 +00:00
|
|
|
this.console(i18m.translate(name, param))
|
2020-02-26 17:58:23 +00:00
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
object(obj) {
|
2020-04-24 05:04:45 +00:00
|
|
|
for (const i in obj) {
|
2020-02-23 14:33:51 +00:00
|
|
|
this.info(i, '=>', obj[i])
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
2020-01-15 08:48:18 +00:00
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
ex(ex: Error) {
|
|
|
|
this.stack(ex).forEach(line => this.console(line))
|
2020-01-15 08:48:18 +00:00
|
|
|
}
|
2020-02-19 11:00:26 +00:00
|
|
|
readSourceMap(fileName: string, lineNumber: number) {
|
|
|
|
try {
|
|
|
|
if (fileName.endsWith('js')) {
|
2020-02-26 02:15:33 +00:00
|
|
|
if (this.sourceMaps[fileName] === undefined) {
|
2020-04-24 05:04:45 +00:00
|
|
|
this.sourceMaps[fileName] = null
|
|
|
|
let sourceLine = base.read(fileName).split('\n');
|
|
|
|
let lastLine = sourceLine[sourceLine.length - 1]
|
|
|
|
if (lastLine.startsWith('//# sourceMappingURL=')) {
|
|
|
|
let sourceContent = null;
|
|
|
|
let sourceMappingURL = lastLine.split('sourceMappingURL=', 2)[1];
|
|
|
|
if (sourceMappingURL.startsWith('data:application/json;base64,')) {
|
|
|
|
sourceContent = String.fromCharCode(...Array.from(base64.toByteArray(sourceMappingURL.split(',', 2)[1])))
|
|
|
|
} else if (sourceMappingURL.startsWith('http')) {
|
|
|
|
// TODO
|
|
|
|
} else {
|
|
|
|
let file = Paths.get(Paths.get(fileName, '..', sourceMappingURL).toFile().getCanonicalPath()).toFile();
|
|
|
|
if (file.exists()) { sourceContent = base.read(file) }
|
|
|
|
}
|
|
|
|
if (sourceContent) {
|
|
|
|
this.sourceMaps[fileName] = new SourceMapBuilder(JSON.parse(sourceContent))
|
|
|
|
this.sourceFileMaps[fileName] = Paths.get(fileName, '..', this.sourceMaps[fileName].sources[0]).toFile().getCanonicalPath();
|
|
|
|
}
|
2020-02-19 11:00:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.sourceMaps[fileName]) {
|
2020-04-24 05:04:45 +00:00
|
|
|
let sourceMapping = this.sourceMaps[fileName].getSource(lineNumber, 25, true, true);
|
|
|
|
fileName = this.sourceFileMaps[fileName]
|
|
|
|
if (sourceMapping && lineNumber != sourceMapping.mapping.sourceLine) { lineNumber = sourceMapping.mapping.sourceLine; }
|
2020-02-19 11:00:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.debug('search source map', fileName, 'line', lineNumber, 'error:', error)
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
fileName,
|
|
|
|
lineNumber
|
|
|
|
}
|
|
|
|
}
|
2019-09-21 06:58:00 +00:00
|
|
|
stack(ex: Error): string[] {
|
2020-04-24 05:04:45 +00:00
|
|
|
let stack = ex.getStackTrace();
|
|
|
|
let cache = ['§c' + ex];
|
2019-09-21 06:58:00 +00:00
|
|
|
//@ts-ignore
|
2019-09-07 04:23:15 +00:00
|
|
|
if (stack.class) {
|
|
|
|
stack = Arrays.asList(stack)
|
|
|
|
}
|
2020-02-15 11:39:45 +00:00
|
|
|
stack.forEach(trace => {
|
2019-09-07 04:23:15 +00:00
|
|
|
if (trace.className.startsWith('<')) {
|
2020-04-24 05:04:45 +00:00
|
|
|
let { fileName, lineNumber } = this.readSourceMap(trace.fileName, trace.lineNumber)
|
2019-09-10 09:21:00 +00:00
|
|
|
if (fileName.startsWith(root)) { fileName = fileName.split(root)[1] }
|
2020-02-15 11:39:45 +00:00
|
|
|
cache.push(` §e->§c ${fileName} => §4${trace.methodName}:${lineNumber}`)
|
2019-09-07 04:23:15 +00:00
|
|
|
} else {
|
2020-04-24 05:04:45 +00:00
|
|
|
let className = trace.className;
|
2020-02-19 11:00:26 +00:00
|
|
|
var fileName = trace.fileName as string;
|
2019-09-07 04:23:15 +00:00
|
|
|
if (className.startsWith('jdk.nashorn.internal.scripts')) {
|
|
|
|
className = className.substr(className.lastIndexOf('$') + 1)
|
2020-02-19 11:00:26 +00:00
|
|
|
var { fileName, lineNumber } = this.readSourceMap(trace.fileName, trace.lineNumber)
|
2019-09-21 06:58:00 +00:00
|
|
|
if (fileName.startsWith(root)) { fileName = fileName.split(root)[1] }
|
2019-09-07 04:23:15 +00:00
|
|
|
} else {
|
2020-04-24 05:04:45 +00:00
|
|
|
for (let prefix in ignoreLogPrefix) {
|
2019-09-07 04:23:15 +00:00
|
|
|
if (className.startsWith(ignoreLogPrefix[prefix])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-18 03:15:49 +00:00
|
|
|
cache.push(` §e->§c ${className}.${trace.methodName}(§4${fileName}:${lineNumber}§c)`);
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
assert(value: any, message?: string, ...optionalParams: any[]): void {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
|
|
|
clear(): void {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
|
|
|
count(label?: string): void {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
|
|
|
countReset(label?: string): void {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
|
|
|
dir(obj: any, options?: NodeJS.InspectOptions): void {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
|
|
|
dirxml(...data: any[]): void {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
|
|
|
group(...label: any[]): void {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
|
|
|
groupCollapsed(...label: any[]): void {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
|
|
|
groupEnd(): void {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
|
|
|
table(tabularData: any, properties?: string[]): void {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
|
|
|
time(label?: string): void {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
|
|
|
timeEnd(label?: string): void {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
|
|
|
timeLog(label?: string, ...data: any[]): void {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
|
|
|
markTimeline(label?: string): void {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
|
|
|
profile(label?: string): void {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
|
|
|
profileEnd(label?: string): void {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
|
|
|
timeStamp(label?: string): void {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
|
|
|
timeline(label?: string): void {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
|
|
|
timelineEnd(label?: string): void {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
|
|
|
}
|