Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENGINEのログをVOICEVOX側のログに出力 #281

Merged
merged 9 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@quasar/extras": "^1.10.9",
"ajv": "^8.6.2",
"core-js": "^3.6.5",
"dayjs": "^1.10.7",
"electron-log": "^4.4.1",
"electron-store": "^8.0.0",
"encoding-japanese": "^1.0.30",
Expand Down
77 changes: 54 additions & 23 deletions src/background.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

import { execFile, ChildProcess } from "child_process";
import { spawn, ChildProcess } from "child_process";
import dotenv from "dotenv";
import treeKill from "tree-kill";
import Store from "electron-store";
Expand All @@ -13,11 +13,23 @@ import path from "path";
import { textEditContextMenu } from "./electron/contextMenu";
import { hasSupportedGpu } from "./electron/device";
import { ipcMainHandle, ipcMainSend } from "@/electron/ipc";
import { logError } from "./electron/log";

import fs from "fs";
import { CharacterInfo, SavingSetting } from "./type/preload";

import log from "electron-log";
import dayjs from "dayjs";

// silly以上のログをコンソールに出力
log.transports.console.format = "[{h}:{i}:{s}.{ms}] [{level}] {text}";
log.transports.console.level = "silly";

// warn以上のログをファイルに出力
const prefix = dayjs().format("YYYYMMDD_HHmmss");
log.transports.file.format = "[{h}:{i}:{s}.{ms}] [{level}] {text}";
log.transports.file.level = "warn";
log.transports.file.fileName = `${prefix}_error.log`;

const isDevelopment = process.env.NODE_ENV !== "production";

let win: BrowserWindow;
Expand All @@ -26,10 +38,10 @@ let win: BrowserWindow;
if (!isDevelopment && !app.requestSingleInstanceLock()) app.quit();

process.on("uncaughtException", (error) => {
logError(error);
log.error(error);
});
process.on("unhandledRejection", (reason) => {
logError(reason);
log.error(reason);
});

// 設定
Expand Down Expand Up @@ -92,26 +104,40 @@ async function runEngine() {
});
}

const useGpu = store.get("useGpu");
log.info(`ENGINE will start in ${useGpu ? "GPU" : "CPU"} mode`);

// エンジンプロセスの起動
const enginePath = path.resolve(
appDirPath,
process.env.ENGINE_PATH ?? "run.exe"
);
const args = store.get("useGpu") ? ["--use_gpu"] : null;
engineProcess = execFile(
enginePath,
args,
{ cwd: path.dirname(enginePath) },
() => {
if (!willQuitEngine) {
ipcMainSend(win, "DETECTED_ENGINE_ERROR");
dialog.showErrorBox(
"音声合成エンジンエラー",
"音声合成エンジンが異常終了しました。エンジンを再起動してください。"
);
}
const args = useGpu ? ["--use_gpu"] : [];

engineProcess = spawn(enginePath, args, {
cwd: path.dirname(enginePath),
});

engineProcess.stdout?.on("data", (data) => {
log.info("ENGINE: " + data.toString("utf-8"));
});

engineProcess.stderr?.on("data", (data) => {
log.error("ENGINE: " + data.toString("utf-8"));
});

engineProcess.on("close", (code, signal) => {
log.info(`ENGINE: terminated due to receipt of signal ${signal}`);
log.info(`ENGINE: exited with code ${code}`);

if (!willQuitEngine) {
ipcMainSend(win, "DETECTED_ENGINE_ERROR");
dialog.showErrorBox(
"音声合成エンジンエラー",
"音声合成エンジンが異常終了しました。エンジンを再起動してください。"
);
}
);
});
}

// temp dir
Expand Down Expand Up @@ -330,7 +356,11 @@ ipcMainHandle("MAXIMIZE_WINDOW", () => {
});

ipcMainHandle("LOG_ERROR", (_, ...params) => {
logError(...params);
log.error(...params);
});

ipcMainHandle("LOG_INFO", (_, ...params) => {
log.info(...params);
});

/**
Expand Down Expand Up @@ -363,7 +393,7 @@ ipcMainHandle(
treeKill(engineProcess.pid, (error) => {
// error変数の値がnull以外であればkillコマンドが失敗したことを意味します。
if (error !== null) {
console.log(error);
log.error(error);

// 再起動用に設定したclose listenerを削除。
engineProcess.removeListener("close", closeListenerCallBack);
Expand Down Expand Up @@ -414,8 +444,9 @@ app.on("quit", () => {
willQuitEngine = true;
try {
engineProcess.pid != undefined && treeKill(engineProcess.pid);
} catch {
logError("engine kill error");
} catch (e: unknown) {
log.error("engine kill error");
log.error(e);
}
});

Expand All @@ -429,7 +460,7 @@ app.on("ready", async () => {
await installExtension(VUEJS3_DEVTOOLS);
} catch (e: unknown) {
if (e instanceof Error) {
logError("Vue Devtools failed to install:", e.toString());
log.error("Vue Devtools failed to install:", e.toString());
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/electron/ipc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ipcMain, IpcMainInvokeEvent, BrowserWindow } from "electron";
import { logError } from "@/electron/log";
import log from "electron-log";

export function ipcMainHandle<T extends keyof IpcIHData>(
channel: T,
Expand All @@ -19,7 +19,7 @@ export function ipcMainHandle(
try {
return listener(event, ...args);
} catch (e) {
logError(e);
log.error(e);
}
};
ipcMain.handle(channel, errorHandledListener);
Expand Down
20 changes: 0 additions & 20 deletions src/electron/log.ts

This file was deleted.

4 changes: 4 additions & 0 deletions src/electron/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ const api: Sandbox = {
return ipcRenderer.invoke("LOG_ERROR", ...params);
},

logInfo: (...params) => {
return ipcRenderer.invoke("LOG_INFO", ...params);
},

restartEngine: () => {
return ipcRendererInvoke("RESTART_ENGINE");
},
Expand Down
2 changes: 1 addition & 1 deletion src/store/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export const audioStore: VoiceVoxStoreOptions<
await api.versionVersionGet();
} catch {
await new Promise((resolve) => setTimeout(resolve, 1000));
console.log("waiting engine...");
window.electron.logInfo("waiting engine...");
continue;
}
engineState = "READY";
Expand Down
3 changes: 3 additions & 0 deletions src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export const indexStore: VoiceVoxStoreOptions<
LOG_ERROR(_, ...params: unknown[]) {
window.electron.logError(...params);
},
LOG_INFO(_, ...params: unknown[]) {
window.electron.logInfo(...params);
},
},
};

Expand Down
1 change: 1 addition & 0 deletions src/store/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ export type IndexActions = {
message: string;
}): Promise<Electron.MessageBoxReturnValue>;
LOG_ERROR(...payload: unknown[]): void;
LOG_INFO(...payload: unknown[]): void;
};

/*
Expand Down
5 changes: 5 additions & 0 deletions src/type/ipc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ type IpcIHData = {
return: void;
};

LOG_INFO: {
args: [...params: unknown[]];
return: void;
};

RESTART_ENGINE: {
args: [];
return: void;
Expand Down
1 change: 1 addition & 0 deletions src/type/preload.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface Sandbox {
minimizeWindow(): void;
maximizeWindow(): void;
logError(...params: unknown[]): void;
logInfo(...params: unknown[]): void;
restartEngine(): Promise<void>;
savingSetting(newData?: SavingSetting): Promise<SavingSetting>;
checkFileExists(file: string): Promise<boolean>;
Expand Down