Skip to content

Commit

Permalink
Log revamp: add log sanitizer function to callRunner
Browse files Browse the repository at this point in the history
  • Loading branch information
arielj committed Oct 25, 2023
1 parent 02d9c63 commit 21a0fc1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
16 changes: 12 additions & 4 deletions src/backend/launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -838,13 +838,17 @@ async function callRunner(

child.stdout.setEncoding('utf-8')
child.stdout.on('data', (data: string) => {
const stringToLog = options?.logSanitizer
? options.logSanitizer(data)
: data

if (!logsDisabled) {
if (options?.logFile) {
appendFileSync(options.logFile, data)
appendFileSync(options.logFile, stringToLog)
}

if (options?.verboseLogFile) {
appendFileSync(options.verboseLogFile, data)
appendFileSync(options.verboseLogFile, stringToLog)
}
}

Expand All @@ -857,13 +861,17 @@ async function callRunner(

child.stderr.setEncoding('utf-8')
child.stderr.on('data', (data: string) => {
const stringToLog = options?.logSanitizer
? options.logSanitizer(data)
: data

if (!logsDisabled) {
if (options?.logFile) {
appendFileSync(options.logFile, data)
appendFileSync(options.logFile, stringToLog)
}

if (options?.verboseLogFile) {
appendFileSync(options.verboseLogFile, data)
appendFileSync(options.verboseLogFile, stringToLog)
}
}

Expand Down
23 changes: 21 additions & 2 deletions src/backend/storeManagers/gog/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ import {
import { gogdlAuthConfig } from 'backend/constants'
import { clearCache } from 'backend/utils'

function authLogSanitizer(line: string) {
try {
const output = JSON.parse(line)
output.access_token = '<redacted>'
output.session_id = '<redacted>'
output.refresh_token = '<redacted>'
output.user_id = '<redacted>'
return JSON.stringify(output) + '\n'
} catch (error) {
return line
}
}

export class GOGUser {
static async login(
code: string
Expand All @@ -26,7 +39,10 @@ export class GOGUser {
// Gets token from GOG basaed on authorization code
const { stdout } = await runRunnerCommand(
['auth', '--code', code],
createAbortController('gogdl-auth')
createAbortController('gogdl-auth'),
{
logSanitizer: authLogSanitizer
}
)

try {
Expand Down Expand Up @@ -102,7 +118,10 @@ export class GOGUser {
}
const { stdout } = await runRunnerCommand(
['auth'],
createAbortController('gogdl-get-credentials')
createAbortController('gogdl-get-credentials'),
{
logSanitizer: authLogSanitizer
}
)

deleteAbortController('gogdl-get-credentials')
Expand Down
18 changes: 17 additions & 1 deletion src/backend/storeManagers/nile/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,28 @@ import { nileUserData } from 'backend/constants'
import { configStore } from './electronStores'
import { clearCache } from 'backend/utils'

function authLogSanitizer(line: string) {
try {
const output = JSON.parse(line)
output.url = '<redacted>'
output.code_verifier = '<redacted>'
output.serial = '<redacted>'
output.client_id = '<redacted>'
return JSON.stringify(output) + '\n'
} catch (error) {
return line
}
}

export class NileUser {
static async getLoginData(): Promise<NileLoginData> {
logDebug('Getting login data from Nile', LogPrefix.Nile)
const { stdout } = await runRunnerCommand(
['auth', '--login', '--non-interactive'],
createAbortController('nile-auth')
createAbortController('nile-auth'),
{
logSanitizer: authLogSanitizer
}
)
deleteAbortController('nile-auth')
const output: NileLoginData = JSON.parse(stdout)
Expand Down
1 change: 1 addition & 0 deletions src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ export interface CallRunnerOptions {
logMessagePrefix?: string
logFile?: string
verboseLogFile?: string
logSanitizer?: (line: string) => string
env?: Record<string, string> | NodeJS.ProcessEnv
wrappers?: string[]
onOutput?: (output: string, child: ChildProcess) => void
Expand Down

0 comments on commit 21a0fc1

Please sign in to comment.