Skip to content

Commit 91cd627

Browse files
authored
[Log] Print installed winetricks packages in logs (#3593)
* Print installed winetricks packages in logs * Set processing to false after processing * Refactor merging queue elements strings
1 parent 0cbe1ec commit 91cd627

File tree

5 files changed

+73
-9
lines changed

5 files changed

+73
-9
lines changed

src/backend/logger/logger.ts

+59-8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { appendFile, writeFile } from 'fs/promises'
1616
import { gamesConfigPath, isWindows } from 'backend/constants'
1717
import { gameManagerMap } from 'backend/storeManagers'
1818
import { existsSync, mkdirSync, openSync } from 'graceful-fs'
19+
import { Winetricks } from 'backend/tools'
1920

2021
export enum LogPrefix {
2122
General = '',
@@ -361,43 +362,71 @@ const logsWriters: Record<string, LogWriter> = {}
361362

362363
// Base abstract class for all LogWriters
363364
class LogWriter {
364-
queue: string[]
365+
queue: (string | Promise<string | string[]>)[]
365366
initialized: boolean
366367
filePath: string
367368
timeoutId: NodeJS.Timeout | undefined
369+
processing: boolean
368370

369371
constructor() {
370372
this.initialized = false
371373
this.queue = []
372374
this.filePath = ''
375+
this.processing = false
373376

374377
if (new.target === LogWriter) {
375378
throw new Error('LogWriter is an abstract class')
376379
}
377380
}
378381

379-
logMessage(message: string) {
382+
/**
383+
* Append a message to the queue
384+
* @param message string or promise that returns a string or string[]
385+
*/
386+
logMessage(message: string | Promise<string | string[]>) {
380387
// push messages to append to the log
381388
this.queue.push(message)
382389

383-
// if the logger is initialized and we don't have a timeout,
384-
// append the message and start a timeout
390+
// if the logger is initialized and we don't have a timeout
391+
// and we are not proccesing the previous batch, write a new batch
385392
//
386393
// otherwise it means there's a timeout already running that will
387-
// write the elements in the queue in a second
388-
if (this.initialized && !this.timeoutId) this.appendMessages()
394+
// write the elements in the queue in a second or that we are processing
395+
// promises
396+
if (this.initialized && !this.processing && !this.timeoutId)
397+
this.appendMessages()
389398
}
390399

391400
async appendMessages() {
392-
const messagesToWrite = this.queue
401+
const itemsInQueue = this.queue
393402

394403
// clear pending message if any
395404
this.queue = []
396405

397406
// clear timeout if any
398407
delete this.timeoutId
399408

400-
if (!messagesToWrite?.length) return
409+
if (!itemsInQueue?.length) return
410+
411+
this.processing = true
412+
413+
// process items in queue, if they are promises we wait
414+
// for them so we can write them in the right order
415+
let messagesToWrite: string[] = []
416+
for (const item of itemsInQueue) {
417+
try {
418+
let result = await item
419+
420+
// support promises returning a string or an array of strings
421+
result = Array.isArray(result) ? result : [result]
422+
423+
messagesToWrite = messagesToWrite.concat(result)
424+
} catch (error) {
425+
logError(error, LogPrefix.Backend)
426+
}
427+
}
428+
429+
this.processing = false
401430

402431
// if we have messages, write them and check again in 1 second
403432
// we start the timeout before writing so we don't wait until
@@ -510,6 +539,28 @@ export async function initGamePlayLog(gameInfo: GameInfo) {
510539
return logsWriters[`${gameInfo.app_name}-lastPlay`].initLog()
511540
}
512541

542+
export async function appendWinetricksGamePlayLog(gameInfo: GameInfo) {
543+
const logWriter = logsWriters[`${gameInfo.app_name}-lastPlay`]
544+
if (logWriter) {
545+
// append a promise to the queue
546+
logWriter.logMessage(
547+
new Promise((resolve, reject) => {
548+
Winetricks.listInstalled(gameInfo.runner, gameInfo.app_name)
549+
.then((installedPackages) => {
550+
const packagesString = installedPackages
551+
? installedPackages.join(', ')
552+
: 'none'
553+
554+
resolve(`Winetricks packages: ${packagesString}\n\n`)
555+
})
556+
.catch((error) => {
557+
reject(error)
558+
})
559+
})
560+
)
561+
}
562+
}
563+
513564
export function stopLogger(appName: string) {
514565
logsWriters[`${appName}-lastPlay`]?.logMessage(
515566
'============= End of log ============='

src/backend/storeManagers/gog/games.ts

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import {
5555
} from './electronStores'
5656
import {
5757
appendGamePlayLog,
58+
appendWinetricksGamePlayLog,
5859
logDebug,
5960
logError,
6061
logFileLocation,
@@ -545,6 +546,8 @@ export async function launch(
545546
return false
546547
}
547548

549+
appendWinetricksGamePlayLog(gameInfo)
550+
548551
commandEnv = {
549552
...commandEnv,
550553
...wineEnvVars

src/backend/storeManagers/legendary/games.ts

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
} from '../../constants'
4242
import {
4343
appendGamePlayLog,
44+
appendWinetricksGamePlayLog,
4445
logError,
4546
logFileLocation,
4647
logInfo,
@@ -828,6 +829,8 @@ export async function launch(
828829
return false
829830
}
830831

832+
appendWinetricksGamePlayLog(gameInfo)
833+
831834
commandEnv = {
832835
...commandEnv,
833836
...wineEnvVars

src/backend/storeManagers/nile/games.ts

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
import {
2222
LogPrefix,
2323
appendGamePlayLog,
24+
appendWinetricksGamePlayLog,
2425
logDebug,
2526
logError,
2627
logFileLocation,
@@ -371,6 +372,8 @@ export async function launch(
371372
return false
372373
}
373374

375+
appendWinetricksGamePlayLog(gameInfo)
376+
374377
commandEnv = {
375378
...commandEnv,
376379
...wineEnvVars

src/backend/storeManagers/storeManagerCommon/games.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { GameConfig } from '../../game_config'
33
import { isMac, isLinux, icon } from '../../constants'
44
import {
55
appendGamePlayLog,
6+
appendWinetricksGamePlayLog,
67
lastPlayLogFileLocation,
78
logInfo,
89
LogPrefix,
@@ -162,7 +163,10 @@ export async function launchGame(
162163
steamRuntime
163164
} = await prepareLaunch(gameSettings, gameInfo, isNative)
164165

165-
if (!isNative) await prepareWineLaunch(runner, appName)
166+
if (!isNative) {
167+
await prepareWineLaunch(runner, appName)
168+
appendWinetricksGamePlayLog(gameInfo)
169+
}
166170

167171
const wrappers = setupWrappers(
168172
gameSettings,

0 commit comments

Comments
 (0)