setup logs

This commit is contained in:
pompurin404 2024-08-01 13:16:57 +08:00
parent 8463175779
commit 85e1c27274
No known key found for this signature in database
3 changed files with 42 additions and 6 deletions

View File

@ -15,6 +15,9 @@ export function initDirs(): void {
if (!fs.existsSync(mihomoWorkDir())) {
fs.mkdirSync(mihomoWorkDir())
}
if (!fs.existsSync(logDir())) {
fs.mkdirSync(logDir())
}
}
export function mihomoCoreDir(): string {
@ -57,3 +60,13 @@ export function mihomoWorkDir(): string {
export function mihomoWorkConfigPath(): string {
return path.join(mihomoWorkDir(), 'config.yaml')
}
export function logDir(): string {
return path.join(dataDir, 'logs')
}
export function logPath(): string {
const date = new Date()
const name = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`
return path.join(logDir(), `${name}.log`)
}

View File

@ -1,15 +1,35 @@
import { execFile, ChildProcess } from 'child_process'
import { mihomoCorePath, mihomoWorkDir } from './dirs'
import { ChildProcess, execSync, spawn } from 'child_process'
import { logPath, mihomoCorePath, mihomoWorkDir } from './dirs'
import { generateProfile } from './factory'
import { appConfig } from './config'
import fs from 'fs'
let child: ChildProcess
export function startCore(): void {
export async function startCore(): Promise<void> {
const corePath = mihomoCorePath(appConfig.core ?? 'mihomo')
generateProfile()
stopCore()
child = execFile(corePath, ['-d', mihomoWorkDir()], () => {})
if (process.platform !== 'win32') {
execSync(`chmod +x ${corePath}`)
}
child = spawn(corePath, ['-d', mihomoWorkDir()])
child.stdout?.on('data', (data) => {
fs.writeFileSync(
logPath(),
data
.toString()
.split('\n')
.map((line: string) => {
if (line) return `[Mihomo]: ${line}`
return ''
})
.filter(Boolean)
.join('\n'),
{
flag: 'a'
}
)
})
}
export function stopCore(): void {

View File

@ -62,7 +62,10 @@ const MihomoCoreCard: React.FC = () => {
onAction={async (key) => {
await patchAppConfig({ core: key as 'mihomo' | 'mihomo-alpha' })
await restartCore()
await mutate()
mutate()
setTimeout(() => {
mutate()
}, 200)
}}
>
<DropdownItem key="mihomo">{CoreMap['mihomo']}</DropdownItem>