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

[Tech] Refactor how we write to game logs #3355

Merged
merged 1 commit into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 8 additions & 3 deletions src/backend/logger/logfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ import {
appendFileSync
} from 'graceful-fs'

import { configStore, currentLogFile, gamesConfigPath } from '../constants'
import { configStore, currentLogFile } from '../constants'
import { app } from 'electron'
import { join } from 'path'
import { logError, LogPrefix, logsDisabled } from './logger'
import {
lastPlayLogFileLocation,
logError,
LogPrefix,
logsDisabled
} from './logger'

interface createLogFileReturn {
currentLogFile: string
Expand Down Expand Up @@ -135,7 +140,7 @@ export function getLogFile(appNameOrRunner: string): string {
case 'nile':
return logs.nileLogFile
default:
return join(gamesConfigPath, appNameOrRunner + '-lastPlay.log')
return lastPlayLogFileLocation(appNameOrRunner)
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/backend/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { GlobalConfig } from 'backend/config'
import { getGOGdlBin, getLegendaryBin } from 'backend/utils'
import { join } from 'path'
import { formatSystemInfo, getSystemInfo } from '../utils/systeminfo'
import { appendFileSync, writeFileSync } from 'graceful-fs'
import { gamesConfigPath } from 'backend/constants'

export enum LogPrefix {
General = '',
Expand Down Expand Up @@ -344,3 +346,19 @@ export function logChangedSetting(
)
})
}

export function lastPlayLogFileLocation(appName: string) {
return join(gamesConfigPath, `${appName}-lastPlay.log`)
}

export function logFileLocation(appName: string) {
return join(gamesConfigPath, `${appName}.log`)
}

export function appendGameLog(appName: string, message: string) {
appendFileSync(lastPlayLogFileLocation(appName), message)
}

export function initGameLog(appName: string, message: string) {
writeFileSync(lastPlayLogFileLocation(appName), message)
}
25 changes: 11 additions & 14 deletions src/backend/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import { autoUpdater } from 'electron-updater'
import { cpus, platform } from 'os'
import {
access,
appendFileSync,
constants,
existsSync,
rmSync,
Expand Down Expand Up @@ -98,6 +97,8 @@ import {
} from './constants'
import { handleProtocol } from './protocol'
import {
appendGameLog,
initGameLog,
initLogger,
logChangedSetting,
logDebug,
Expand Down Expand Up @@ -144,8 +145,6 @@ import {
libraryManagerMap
} from './storeManagers'
import { updateWineVersionInfos } from './wine/manager/utils'

import { logFileLocation as getLogFileLocation } from './storeManagers/storeManagerCommon/games'
import { addNewApp } from './storeManagers/sideload/library'
import {
getGameOverride,
Expand Down Expand Up @@ -1027,8 +1026,6 @@ ipcMain.handle(
powerDisplayId = powerSaveBlocker.start('prevent-display-sleep')
}

const logFileLocation = getLogFileLocation(appName)

const systemInfo = await getSystemInfo()
.then(formatSystemInfo)
.catch((error) => {
Expand All @@ -1038,20 +1035,21 @@ ipcMain.handle(
)
return 'Error, check general log'
})
writeFileSync(logFileLocation, 'System Info:\n' + `${systemInfo}\n` + '\n')

initGameLog(appName, 'System Info:\n' + `${systemInfo}\n` + '\n')

const gameSettingsString = JSON.stringify(gameSettings, null, '\t')
appendFileSync(
logFileLocation,
appendGameLog(
appName,
`Game Settings: ${gameSettingsString}\n` +
'\n' +
`Game launched at: ${startPlayingDate}\n` +
'\n'
)

if (logsDisabled) {
appendFileSync(
logFileLocation,
appendGameLog(
appName,
'IMPORTANT: Logs are disabled. Enable logs before reporting an issue.'
)
}
Expand All @@ -1062,8 +1060,7 @@ ipcMain.handle(
if (!isNative) {
const isWineOkToLaunch = await checkWineBeforeLaunch(
appName,
gameSettings,
logFileLocation
gameSettings
)

if (!isWineOkToLaunch) {
Expand Down Expand Up @@ -1092,8 +1089,8 @@ ipcMain.handle(

const launchResult = await command.catch((exception) => {
logError(exception, LogPrefix.Backend)
appendFileSync(
logFileLocation,
appendGameLog(
appName,
`An exception occurred when launching the game:\n${exception.stack}`
)
return false
Expand Down
30 changes: 10 additions & 20 deletions src/backend/storeManagers/gog/games.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,19 @@ import {
InstallPlatform,
InstallProgress
} from 'common/types'
import { appendFileSync, existsSync, rmSync } from 'graceful-fs'
import { gamesConfigPath, isWindows, isMac, isLinux } from '../../constants'
import { existsSync, rmSync } from 'graceful-fs'
import { isWindows, isMac, isLinux } from '../../constants'
import {
configStore,
installedGamesStore,
playtimeSyncQueue,
syncStore
} from './electronStores'
import {
appendGameLog,
logDebug,
logError,
logFileLocation,
logInfo,
LogPrefix,
logsDisabled,
Expand Down Expand Up @@ -80,7 +82,6 @@ import { t } from 'i18next'
import { showDialogBoxModalAuto } from '../../dialog/dialog'
import { sendFrontendMessage } from '../../main_window'
import { RemoveArgs } from 'common/types/game_manager'
import { logFileLocation } from 'backend/storeManagers/storeManagerCommon/games'
import { getWineFlagsArray } from 'backend/utils/compatibility_layers'
import axios, { AxiosError } from 'axios'
import { isOnline, runOnceWhenOnline } from 'backend/online_monitor'
Expand Down Expand Up @@ -288,8 +289,6 @@ export async function install(
? 'osx'
: (platformToInstall.toLowerCase() as GogInstallPlatform)

const logPath = join(gamesConfigPath, appName + '.log')

const commandParts: string[] = [
'download',
appName,
Expand All @@ -309,7 +308,7 @@ export async function install(

const res = await runGogdlCommand(commandParts, {
abortId: appName,
logFile: logPath,
logFile: logFileLocation(appName),
onOutput,
logMessagePrefix: `Installing ${appName}`
})
Expand Down Expand Up @@ -438,10 +437,7 @@ export async function launch(
steamRuntime
} = await prepareLaunch(gameSettings, gameInfo, isNative(appName))
if (!launchPrepSuccess) {
appendFileSync(
logFileLocation(appName),
`Launch aborted: ${launchPrepFailReason}`
)
appendGameLog(appName, `Launch aborted: ${launchPrepFailReason}`)
showDialogBoxModalAuto({
title: t('box.error.launchAborted', 'Launch aborted'),
message: launchPrepFailReason!,
Expand Down Expand Up @@ -479,10 +475,7 @@ export async function launch(
envVars: wineEnvVars
} = await prepareWineLaunch('gog', appName)
if (!wineLaunchPrepSuccess) {
appendFileSync(
logFileLocation(appName),
`Launch aborted: ${wineLaunchPrepFailReason}`
)
appendGameLog(appName, `Launch aborted: ${wineLaunchPrepFailReason}`)
if (wineLaunchPrepFailReason) {
showDialogBoxModalAuto({
title: t('box.error.launchAborted', 'Launch aborted'),
Expand Down Expand Up @@ -526,18 +519,15 @@ export async function launch(
commandEnv,
join(...Object.values(getGOGdlBin()))
)
appendFileSync(
logFileLocation(appName),
`Launch Command: ${fullCommand}\n\nGame Log:\n`
)
appendGameLog(appName, `Launch Command: ${fullCommand}\n\nGame Log:\n`)

const { error, abort } = await runGogdlCommand(commandParts, {
abortId: appName,
env: commandEnv,
wrappers,
logMessagePrefix: `Launching ${gameInfo.title}`,
onOutput: (output: string) => {
if (!logsDisabled) appendFileSync(logFileLocation(appName), output)
if (!logsDisabled) appendGameLog(appName, output)
}
})

Expand Down Expand Up @@ -833,7 +823,7 @@ async function getCommandParameters(appName: string) {
const { maxWorkers } = GlobalConfig.get().getSettings()
const workers = maxWorkers ? ['--max-workers', `${maxWorkers}`] : []
const gameData = getGameInfo(appName)
const logPath = join(gamesConfigPath, appName + '.log')
const logPath = logFileLocation(appName)
const credentials = await GOGUser.getCredentials()

const withDlcs = gameData.install.installedWithDLCs
Expand Down
7 changes: 1 addition & 6 deletions src/backend/storeManagers/gog/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { isWindows } from '../../constants'
import ini from 'ini'
import { isOnline } from '../../online_monitor'
import { getWinePath, runWineCommand, verifyWinePrefix } from '../../launcher'
import { logFileLocation } from 'backend/storeManagers/storeManagerCommon/games'
import { getGameInfo as getGogLibraryGameInfo } from 'backend/storeManagers/gog/library'
const nonNativePathSeparator = path.sep === '/' ? '\\' : '/'

Expand Down Expand Up @@ -50,11 +49,7 @@ async function setup(

const gameSettings = GameConfig.get(appName).config
if (!isWindows) {
const isWineOkToLaunch = await checkWineBeforeLaunch(
appName,
gameSettings,
logFileLocation(appName)
)
const isWineOkToLaunch = await checkWineBeforeLaunch(appName, gameSettings)

if (!isWineOkToLaunch) {
logError(
Expand Down
39 changes: 16 additions & 23 deletions src/backend/storeManagers/legendary/games.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { appendFileSync, existsSync } from 'graceful-fs'
import { existsSync } from 'graceful-fs'
import axios from 'axios'

import {
Expand Down Expand Up @@ -35,10 +35,16 @@ import {
isWindows,
installed,
configStore,
gamesConfigPath,
isCLINoGui
} from '../../constants'
import { logError, logInfo, LogPrefix, logsDisabled } from '../../logger/logger'
import {
appendGameLog,
logError,
logFileLocation,
logInfo,
LogPrefix,
logsDisabled
} from '../../logger/logger'
import {
prepareLaunch,
prepareWineLaunch,
Expand All @@ -63,7 +69,6 @@ import { showDialogBoxModalAuto } from '../../dialog/dialog'
import { Catalog, Product } from 'common/types/epic-graphql'
import { sendFrontendMessage } from '../../main_window'
import { RemoveArgs } from 'common/types/game_manager'
import { logFileLocation } from 'backend/storeManagers/storeManagerCommon/games'
import {
AllowedWineFlags,
getWineFlags
Expand Down Expand Up @@ -489,7 +494,6 @@ export async function update(
const { maxWorkers, downloadNoHttps } = GlobalConfig.get().getSettings()
const installPlatform = getGameInfo(appName).install.platform!
const info = await getInstallInfo(appName, installPlatform)
const logPath = join(gamesConfigPath, appName + '.log')

const command: LegendaryCommand = {
subcommand: 'update',
Expand All @@ -510,7 +514,7 @@ export async function update(

const res = await runLegendaryCommand(command, {
abortId: appName,
logFile: logPath,
logFile: logFileLocation(appName),
onOutput,
logMessagePrefix: `Updating ${appName}`
})
Expand Down Expand Up @@ -565,7 +569,7 @@ export async function install(
const { maxWorkers, downloadNoHttps } = GlobalConfig.get().getSettings()
const info = await getInstallInfo(appName, platformToInstall)

const logPath = join(gamesConfigPath, appName + '.log')
const logPath = logFileLocation(appName)

const command: LegendaryCommand = {
subcommand: 'install',
Expand Down Expand Up @@ -659,8 +663,6 @@ export async function uninstall({ appName }: RemoveArgs): Promise<ExecResult> {
export async function repair(appName: string): Promise<ExecResult> {
const { maxWorkers, downloadNoHttps } = GlobalConfig.get().getSettings()

const logPath = join(gamesConfigPath, appName + '.log')

const command: LegendaryCommand = {
subcommand: 'repair',
appName: LegendaryAppName.parse(appName),
Expand All @@ -671,7 +673,7 @@ export async function repair(appName: string): Promise<ExecResult> {

const res = await runLegendaryCommand(command, {
abortId: appName,
logFile: logPath,
logFile: logFileLocation(appName),
logMessagePrefix: `Repairing ${appName}`
})

Expand Down Expand Up @@ -770,10 +772,7 @@ export async function launch(
offlineMode
} = await prepareLaunch(gameSettings, gameInfo, isNative(appName))
if (!launchPrepSuccess) {
appendFileSync(
logFileLocation(appName),
`Launch aborted: ${launchPrepFailReason}`
)
appendGameLog(appName, `Launch aborted: ${launchPrepFailReason}`)
showDialogBoxModalAuto({
title: t('box.error.launchAborted', 'Launch aborted'),
message: launchPrepFailReason!,
Expand Down Expand Up @@ -810,10 +809,7 @@ export async function launch(
envVars: wineEnvVars
} = await prepareWineLaunch('legendary', appName)
if (!wineLaunchPrepSuccess) {
appendFileSync(
logFileLocation(appName),
`Launch aborted: ${wineLaunchPrepFailReason}`
)
appendGameLog(appName, `Launch aborted: ${wineLaunchPrepFailReason}`)
if (wineLaunchPrepFailReason) {
showDialogBoxModalAuto({
title: t('box.error.launchAborted', 'Launch aborted'),
Expand Down Expand Up @@ -859,18 +855,15 @@ export async function launch(
commandEnv,
join(...Object.values(getLegendaryBin()))
)
appendFileSync(
logFileLocation(appName),
`Launch Command: ${fullCommand}\n\nGame Log:\n`
)
appendGameLog(appName, `Launch Command: ${fullCommand}\n\nGame Log:\n`)

const { error } = await runLegendaryCommand(command, {
abortId: appName,
env: commandEnv,
wrappers: wrappers,
logMessagePrefix: `Launching ${gameInfo.title}`,
onOutput: (output) => {
if (!logsDisabled) appendFileSync(logFileLocation(appName), output)
if (!logsDisabled) appendGameLog(appName, output)
}
})

Expand Down
Loading