mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-09-23 05:16:23 -07:00
Fix Windows mpv logging and add log export (#88)
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import fs from 'node:fs';
|
||||
import { spawn, spawnSync } from 'node:child_process';
|
||||
import { isLogFileEnabled } from '../../shared/log-files';
|
||||
import { buildMpvLaunchModeArgs } from '../../shared/mpv-launch-mode';
|
||||
import { buildMpvMsgLevel } from '../../shared/mpv-logging-args';
|
||||
import { buildSubminerPluginRuntimeScriptOptParts } from '../../shared/subminer-plugin-script-opts';
|
||||
import type { MpvLaunchMode } from '../../types/config';
|
||||
import type { SubminerPluginRuntimeScriptOptConfig } from '../../shared/subminer-plugin-script-opts';
|
||||
@@ -10,8 +12,9 @@ export interface WindowsMpvLaunchDeps {
|
||||
getEnv: (name: string) => string | undefined;
|
||||
runWhere: () => { status: number | null; stdout: string; error?: Error };
|
||||
fileExists: (candidate: string) => boolean;
|
||||
spawnDetached: (command: string, args: string[]) => Promise<void>;
|
||||
spawnDetached: (command: string, args: string[], env?: NodeJS.ProcessEnv) => Promise<void>;
|
||||
showError: (title: string, content: string) => void;
|
||||
logInfo?: (message: string) => void;
|
||||
}
|
||||
|
||||
export type ConfiguredWindowsMpvPathStatus = 'blank' | 'configured' | 'invalid';
|
||||
@@ -126,6 +129,13 @@ export function buildWindowsMpvLaunchArgs(
|
||||
: shouldPassSubminerScriptOpts
|
||||
? [`subminer-socket_path=${inputIpcServer.replace(/,/g, '\\,')}`]
|
||||
: [];
|
||||
const logLevel = pluginRuntimeConfig?.logLevel;
|
||||
const hasMsgLevel = readExtraArgValue(extraArgs, '--msg-level') !== undefined;
|
||||
const hasLogFile = readExtraArgValue(extraArgs, '--log-file') !== undefined;
|
||||
const mpvLogLevelArg =
|
||||
logLevel && !hasMsgLevel && (isLogFileEnabled('mpv') || hasLogFile)
|
||||
? `--msg-level=${buildMpvMsgLevel(logLevel)}`
|
||||
: null;
|
||||
if (!pluginRuntimeConfig && hasBinaryPath) {
|
||||
scriptOptPairs.unshift(`subminer-binary_path=${binaryPath.trim().replace(/,/g, '\\,')}`);
|
||||
}
|
||||
@@ -147,6 +157,7 @@ export function buildWindowsMpvLaunchArgs(
|
||||
'--secondary-sub-visibility=no',
|
||||
...(scriptOpts ? [scriptOpts] : []),
|
||||
...buildMpvLaunchModeArgs(launchMode),
|
||||
...(mpvLogLevelArg ? [mpvLogLevelArg] : []),
|
||||
...extraArgs,
|
||||
...targets,
|
||||
];
|
||||
@@ -197,17 +208,39 @@ export async function launchWindowsMpv(
|
||||
if (installedPlugin?.installed && !installedPluginPrompted) {
|
||||
runtimePluginPolicy?.notifyInstalledPluginDetected?.(installedPlugin);
|
||||
}
|
||||
await deps.spawnDetached(
|
||||
mpvPath,
|
||||
buildWindowsMpvLaunchArgs(
|
||||
targets,
|
||||
extraArgs,
|
||||
binaryPath,
|
||||
runtimePluginEntrypointPath,
|
||||
launchMode,
|
||||
pluginRuntimeConfig,
|
||||
),
|
||||
const hasLogLevel = pluginRuntimeConfig?.logLevel !== undefined;
|
||||
const hasLogRotation = pluginRuntimeConfig?.logRotation !== undefined;
|
||||
const launchEnv =
|
||||
hasLogLevel || hasLogRotation
|
||||
? {
|
||||
...(hasLogLevel
|
||||
? { SUBMINER_LOG_LEVEL: pluginRuntimeConfig.logLevel }
|
||||
: {}),
|
||||
...(hasLogRotation
|
||||
? { SUBMINER_LOG_ROTATION: String(pluginRuntimeConfig.logRotation) }
|
||||
: {}),
|
||||
}
|
||||
: undefined;
|
||||
const launchArgs = buildWindowsMpvLaunchArgs(
|
||||
targets,
|
||||
extraArgs,
|
||||
binaryPath,
|
||||
runtimePluginEntrypointPath,
|
||||
launchMode,
|
||||
pluginRuntimeConfig,
|
||||
);
|
||||
const inputIpcServer =
|
||||
readExtraArgValue(launchArgs, '--input-ipc-server') ?? DEFAULT_WINDOWS_MPV_SOCKET;
|
||||
deps.logInfo?.(
|
||||
[
|
||||
`Launching mpv: mpvPath=${mpvPath}`,
|
||||
`inputIpcServer=${inputIpcServer}`,
|
||||
`bundledPlugin=${runtimePluginEntrypointPath ?? 'not injected'}`,
|
||||
`installedPlugin=${installedPlugin?.installed ? (installedPlugin.path ?? 'unknown') : 'none'}`,
|
||||
`targets=${targets.length}`,
|
||||
].join('; '),
|
||||
);
|
||||
await deps.spawnDetached(mpvPath, launchArgs, launchEnv);
|
||||
return { ok: true, mpvPath };
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
@@ -220,6 +253,7 @@ export function createWindowsMpvLaunchDeps(options: {
|
||||
getEnv?: (name: string) => string | undefined;
|
||||
fileExists?: (candidate: string) => boolean;
|
||||
showError: (title: string, content: string) => void;
|
||||
logInfo?: (message: string) => void;
|
||||
}): WindowsMpvLaunchDeps {
|
||||
return {
|
||||
getEnv: options.getEnv ?? ((name) => process.env[name]),
|
||||
@@ -235,13 +269,15 @@ export function createWindowsMpvLaunchDeps(options: {
|
||||
};
|
||||
},
|
||||
fileExists: options.fileExists ?? defaultWindowsMpvFileExists,
|
||||
spawnDetached: (command, args) =>
|
||||
logInfo: options.logInfo,
|
||||
spawnDetached: (command, args, env) =>
|
||||
new Promise((resolve, reject) => {
|
||||
try {
|
||||
const child = spawn(command, args, {
|
||||
detached: true,
|
||||
stdio: 'ignore',
|
||||
windowsHide: true,
|
||||
env: env ? { ...process.env, ...env } : process.env,
|
||||
});
|
||||
let settled = false;
|
||||
child.once('error', (error) => {
|
||||
|
||||
Reference in New Issue
Block a user