forked from mirror/MCSM-Daemon
Feat: 完善一些细节问题与日志
This commit is contained in:
parent
1287190edb
commit
36a3458581
@ -391,7 +391,9 @@ routerApp.on("instance/outputlog", async (ctx, data) => {
|
|||||||
const text = await fs.readFile(filePath, { encoding: "utf-8" });
|
const text = await fs.readFile(filePath, { encoding: "utf-8" });
|
||||||
return protocol.response(ctx, text);
|
return protocol.response(ctx, text);
|
||||||
}
|
}
|
||||||
protocol.responseError(ctx, new Error("终端日志文件不存在"));
|
protocol.responseError(ctx, new Error("终端日志文件不存在"), {
|
||||||
|
notPrintErr: true
|
||||||
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
protocol.responseError(ctx, err);
|
protocol.responseError(ctx, err);
|
||||||
}
|
}
|
||||||
|
@ -31,21 +31,24 @@ const MAX_LOG_SIZE = 512;
|
|||||||
const buffer = new Map<string, string>();
|
const buffer = new Map<string, string>();
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
buffer.forEach((buf, instanceUuid) => {
|
buffer.forEach((buf, instanceUuid) => {
|
||||||
|
if (!buf || !instanceUuid) return;
|
||||||
const logFilePath = path.join(InstanceSubsystem.LOG_DIR, `${instanceUuid}.log`);
|
const logFilePath = path.join(InstanceSubsystem.LOG_DIR, `${instanceUuid}.log`);
|
||||||
if (!fs.existsSync(InstanceSubsystem.LOG_DIR)) fs.mkdirsSync(InstanceSubsystem.LOG_DIR);
|
if (!fs.existsSync(InstanceSubsystem.LOG_DIR)) fs.mkdirsSync(InstanceSubsystem.LOG_DIR);
|
||||||
try {
|
try {
|
||||||
const fileInfo = fs.statSync(logFilePath);
|
const fileInfo = fs.statSync(logFilePath);
|
||||||
if (fileInfo && fileInfo.size > 1024 * MAX_LOG_SIZE) fs.removeSync(logFilePath);
|
if (fileInfo && fileInfo.size > 1024 * MAX_LOG_SIZE) fs.removeSync(logFilePath);
|
||||||
} catch (err) {}
|
} catch (err) {}
|
||||||
fs.writeFile(logFilePath, buf, { encoding: "utf-8", flag: "a" }, () => {});
|
fs.writeFile(logFilePath, buf, { encoding: "utf-8", flag: "a" }, () => {
|
||||||
|
buffer.set(instanceUuid, "");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}, 1000);
|
}, 500);
|
||||||
|
|
||||||
// 输出流记录到缓存区
|
// 输出流记录到缓存区
|
||||||
async function outputLog(instanceUuid: string, text: string) {
|
async function outputLog(instanceUuid: string, text: string) {
|
||||||
const buf = buffer.get(instanceUuid) + text;
|
const buf = (buffer.get(instanceUuid) ?? "") + text;
|
||||||
if (buf.length > 1024 * 1024) buffer.set(instanceUuid, "");
|
if (buf.length > 1024 * 1024) buffer.set(instanceUuid, "");
|
||||||
buffer.set(instanceUuid, buf);
|
buffer.set(instanceUuid, buf ?? null);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 实例输出流事件
|
// 实例输出流事件
|
||||||
|
@ -73,7 +73,9 @@ routerApp.on("stream/auth", (ctx, data) => {
|
|||||||
});
|
});
|
||||||
protocol.response(ctx, true);
|
protocol.response(ctx, true);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
protocol.responseError(ctx, error);
|
protocol.responseError(ctx, error, {
|
||||||
|
notPrintErr: true
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -36,6 +36,10 @@ export interface IPacket {
|
|||||||
data: any;
|
data: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface IResponseErrorConfig {
|
||||||
|
notPrintErr: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
// 全局 Socket 储存
|
// 全局 Socket 储存
|
||||||
const globalSocket = new Map<String, Socket>();
|
const globalSocket = new Map<String, Socket>();
|
||||||
|
|
||||||
@ -48,14 +52,14 @@ export function response(ctx: RouterContext, data: any) {
|
|||||||
ctx.socket.emit(ctx.event, packet);
|
ctx.socket.emit(ctx.event, packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function responseError(ctx: RouterContext, err: Error | string) {
|
export function responseError(ctx: RouterContext, err: Error | string, config?: IResponseErrorConfig) {
|
||||||
let errinfo: any = "";
|
let errinfo: any = "";
|
||||||
if (err) errinfo = err.toString();
|
if (err) errinfo = err.toString();
|
||||||
else errinfo = err;
|
else errinfo = err;
|
||||||
const packet = new Packet(ctx.uuid, STATUS_ERR, ctx.event, errinfo);
|
const packet = new Packet(ctx.uuid, STATUS_ERR, ctx.event, errinfo);
|
||||||
// 忽略因为重启守护进程没有刷新网页的权限不足错误
|
// 忽略因为重启守护进程没有刷新网页的权限不足错误
|
||||||
if (err.toString().includes("[Unauthorized Access]")) return ctx.socket.emit(ctx.event, packet);
|
if (err.toString().includes("[Unauthorized Access]")) return ctx.socket.emit(ctx.event, packet);
|
||||||
logger.warn(`会话 ${ctx.socket.id}(${ctx.socket.handshake.address})/${ctx.event} 响应数据时异常:\n`, err);
|
if (!config?.notPrintErr) logger.warn(`会话 ${ctx.socket.id}(${ctx.socket.handshake.address})/${ctx.event} 响应数据时异常:\n`, err);
|
||||||
ctx.socket.emit(ctx.event, packet);
|
ctx.socket.emit(ctx.event, packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user