mirror of
https://gh.catmak.name/https://github.com/mihomo-party-org/mihomo-party
synced 2026-02-10 11:40:28 +08:00
refactor: replace console.log with createLogger in main process
This commit is contained in:
parent
676743d1b0
commit
9e5d11c3c8
@ -6,6 +6,9 @@ import { getAppConfig } from './app'
|
||||
import { defaultControledMihomoConfig } from '../utils/template'
|
||||
import { deepMerge } from '../utils/merge'
|
||||
import { existsSync } from 'fs'
|
||||
import { createLogger } from '../utils/logger'
|
||||
|
||||
const controledMihomoLogger = createLogger('ControledMihomo')
|
||||
|
||||
let controledMihomoConfig: Partial<IMihomoConfig> // mihomo.yaml
|
||||
|
||||
@ -23,7 +26,7 @@ export async function getControledMihomoConfig(force = false): Promise<Partial<I
|
||||
'utf-8'
|
||||
)
|
||||
} catch (error) {
|
||||
console.error('Failed to create mihomo.yaml file:', error)
|
||||
controledMihomoLogger.error('Failed to create mihomo.yaml file', error)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -14,6 +14,9 @@ import { app } from 'electron'
|
||||
import { mihomoUpgradeConfig } from '../core/mihomoApi'
|
||||
|
||||
import i18next from 'i18next'
|
||||
import { createLogger } from '../utils/logger'
|
||||
|
||||
const profileLogger = createLogger('Profile')
|
||||
|
||||
let profileConfig: IProfileConfig
|
||||
let profileConfigWriteQueue: Promise<void> = Promise.resolve()
|
||||
@ -320,16 +323,16 @@ export async function setProfileStr(id: string, content: string): Promise<void>
|
||||
const { generateProfile } = await import('../core/factory')
|
||||
await generateProfile()
|
||||
await mihomoUpgradeConfig()
|
||||
console.log('[Profile] Config reloaded successfully using mihomoUpgradeConfig')
|
||||
profileLogger.info('Config reloaded successfully using mihomoUpgradeConfig')
|
||||
} catch (error) {
|
||||
console.error('[Profile] Failed to reload config with mihomoUpgradeConfig:', error)
|
||||
profileLogger.error('Failed to reload config with mihomoUpgradeConfig', error)
|
||||
try {
|
||||
console.log('[Profile] Falling back to restart core')
|
||||
profileLogger.info('Falling back to restart core')
|
||||
const { restartCore } = await import('../core/manager')
|
||||
await restartCore()
|
||||
console.log('[Profile] Core restarted successfully')
|
||||
profileLogger.info('Core restarted successfully')
|
||||
} catch (restartError) {
|
||||
console.error('[Profile] Failed to restart core:', restartError)
|
||||
profileLogger.error('Failed to restart core', restartError)
|
||||
throw restartError
|
||||
}
|
||||
}
|
||||
|
||||
@ -21,6 +21,9 @@ import { deepMerge } from '../utils/merge'
|
||||
import vm from 'vm'
|
||||
import { existsSync, writeFileSync } from 'fs'
|
||||
import path from 'path'
|
||||
import { createLogger } from '../utils/logger'
|
||||
|
||||
const factoryLogger = createLogger('Factory')
|
||||
|
||||
let runtimeConfigStr: string
|
||||
let runtimeConfig: IMihomoConfig
|
||||
@ -132,7 +135,7 @@ export async function generateProfile(): Promise<string | undefined> {
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('读取或应用规则文件时出错:', error)
|
||||
factoryLogger.error('Failed to read or apply rule file', error)
|
||||
}
|
||||
|
||||
const profile = deepMerge(currentProfile, controledMihomoConfig)
|
||||
|
||||
@ -7,6 +7,9 @@ import { calcTraffic } from '../utils/calc'
|
||||
import { getRuntimeConfig } from './factory'
|
||||
import { floatingWindow } from '../resolve/floatingWindow'
|
||||
import { getMihomoIpcPath } from './manager'
|
||||
import { createLogger } from '../utils/logger'
|
||||
|
||||
const mihomoApiLogger = createLogger('MihomoApi')
|
||||
|
||||
let axiosIns: AxiosInstance = null!
|
||||
let currentIpcPath: string = ''
|
||||
@ -30,7 +33,7 @@ export const getAxios = async (force: boolean = false): Promise<AxiosInstance> =
|
||||
}
|
||||
|
||||
currentIpcPath = dynamicIpcPath
|
||||
console.log(`[mihomoApi] Creating axios instance with path: ${dynamicIpcPath}`)
|
||||
mihomoApiLogger.info(`Creating axios instance with path: ${dynamicIpcPath}`)
|
||||
|
||||
axiosIns = axios.create({
|
||||
baseURL: `http://localhost`,
|
||||
@ -44,9 +47,9 @@ export const getAxios = async (force: boolean = false): Promise<AxiosInstance> =
|
||||
},
|
||||
(error) => {
|
||||
if (error.code === 'ENOENT') {
|
||||
console.debug(`[mihomoApi] Pipe not ready: ${error.config?.socketPath}`)
|
||||
mihomoApiLogger.debug(`Pipe not ready: ${error.config?.socketPath}`)
|
||||
} else {
|
||||
console.error(`[mihomoApi] Axios error with path ${dynamicIpcPath}:`, error.message)
|
||||
mihomoApiLogger.error(`Axios error with path ${dynamicIpcPath}: ${error.message}`)
|
||||
}
|
||||
|
||||
if (error.response && error.response.data) {
|
||||
@ -191,28 +194,28 @@ export const mihomoUpgradeUI = async (): Promise<void> => {
|
||||
}
|
||||
|
||||
export const mihomoUpgradeConfig = async (): Promise<void> => {
|
||||
console.log('[mihomoApi] mihomoUpgradeConfig called')
|
||||
mihomoApiLogger.info('mihomoUpgradeConfig called')
|
||||
|
||||
try {
|
||||
const instance = await getAxios()
|
||||
console.log('[mihomoApi] axios instance obtained')
|
||||
mihomoApiLogger.info('axios instance obtained')
|
||||
const { diffWorkDir = false } = await getAppConfig()
|
||||
const { current } = await import('../config').then((mod) => mod.getProfileConfig(true))
|
||||
const { mihomoWorkConfigPath } = await import('../utils/dirs')
|
||||
const configPath = diffWorkDir ? mihomoWorkConfigPath(current) : mihomoWorkConfigPath('work')
|
||||
console.log('[mihomoApi] config path:', configPath)
|
||||
mihomoApiLogger.info(`config path: ${configPath}`)
|
||||
const { existsSync } = await import('fs')
|
||||
if (!existsSync(configPath)) {
|
||||
console.log('[mihomoApi] config file does not exist, generating...')
|
||||
mihomoApiLogger.info('config file does not exist, generating...')
|
||||
const { generateProfile } = await import('./factory')
|
||||
await generateProfile()
|
||||
}
|
||||
const response = await instance.put('/configs?force=true', {
|
||||
path: configPath
|
||||
})
|
||||
console.log('[mihomoApi] config upgrade request completed', response?.status || 'no status')
|
||||
mihomoApiLogger.info(`config upgrade request completed ${response?.status || 'no status'}`)
|
||||
} catch (error) {
|
||||
console.error('[mihomoApi] Failed to upgrade config:', error)
|
||||
mihomoApiLogger.error('Failed to upgrade config', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
@ -255,7 +258,7 @@ const mihomoTraffic = async (): Promise<void> => {
|
||||
const dynamicIpcPath = getMihomoIpcPath()
|
||||
const wsUrl = `ws+unix:${dynamicIpcPath}:/traffic`
|
||||
|
||||
console.log(`[mihomoApi] Creating traffic WebSocket with URL: ${wsUrl}`)
|
||||
mihomoApiLogger.info(`Creating traffic WebSocket with URL: ${wsUrl}`)
|
||||
mihomoTrafficWs = new WebSocket(wsUrl)
|
||||
|
||||
mihomoTrafficWs.onmessage = async (e): Promise<void> => {
|
||||
@ -286,7 +289,7 @@ const mihomoTraffic = async (): Promise<void> => {
|
||||
}
|
||||
|
||||
mihomoTrafficWs.onerror = (error): void => {
|
||||
console.error(`[mihomoApi] Traffic WebSocket error:`, error)
|
||||
mihomoApiLogger.error('Traffic WebSocket error', error)
|
||||
if (mihomoTrafficWs) {
|
||||
mihomoTrafficWs.close()
|
||||
mihomoTrafficWs = null
|
||||
|
||||
@ -19,7 +19,10 @@ import { showFloatingWindow } from './resolve/floatingWindow'
|
||||
import { initI18n } from '../shared/i18n'
|
||||
import i18next from 'i18next'
|
||||
import { logger } from './utils/logger'
|
||||
import { createLogger } from './utils/logger'
|
||||
import { initWebdavBackupScheduler } from './resolve/backup'
|
||||
|
||||
const mainLogger = createLogger('Main')
|
||||
import {
|
||||
createWindow,
|
||||
mainWindow,
|
||||
@ -93,7 +96,7 @@ async function checkHighPrivilegeCoreEarly(): Promise<void> {
|
||||
process.exit(0)
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Failed to check high privilege core:', e)
|
||||
mainLogger.error('Failed to check high privilege core', e)
|
||||
}
|
||||
}
|
||||
|
||||
@ -105,7 +108,7 @@ async function initHardwareAcceleration(): Promise<void> {
|
||||
app.disableHardwareAcceleration()
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('Failed to read hardware acceleration config:', e)
|
||||
mainLogger.warn('Failed to read hardware acceleration config', e)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user