|
| 1 | +import { app } from 'electron'; |
| 2 | +import * as path from 'path'; |
1 | 3 | import * as fs from 'fs-extra';
|
2 | 4 | import * as rxjs from 'rxjs';
|
3 | 5 | import { v4 as uuid } from 'uuid';
|
4 | 6 | import { waitUntil } from '../../common/async';
|
5 | 7 | import { type GameEvent, GameEventType } from '../../common/game';
|
| 8 | +import { LogLevel, isLogLevelEnabled } from '../../common/logger'; |
6 | 9 | import { createLogger } from '../logger';
|
7 | 10 | import type { SGEGameCredentials } from '../sge';
|
8 | 11 | import { GameParserImpl } from './game.parser';
|
@@ -75,33 +78,12 @@ export class GameServiceImpl implements GameService {
|
75 | 78 | this.sentCommandsSubject$
|
76 | 79 | );
|
77 | 80 |
|
78 |
| - // TODO remove writing to file; just helpful for early development |
79 |
| - const socketWriteStream = fs.createWriteStream('game-socket.log'); |
80 |
| - socketData$.subscribe({ |
81 |
| - next: (data: string) => { |
82 |
| - socketWriteStream.write(`---\n${data}`); |
83 |
| - }, |
84 |
| - error: () => { |
85 |
| - socketWriteStream.end(); |
86 |
| - }, |
87 |
| - complete: () => { |
88 |
| - socketWriteStream.end(); |
89 |
| - }, |
90 |
| - }); |
91 |
| - |
92 |
| - // TODO remove writing to file; just helpful for early development |
93 |
| - const gameEventWriteStream = fs.createWriteStream('game-event.log'); |
94 |
| - gameEvents$.subscribe({ |
95 |
| - next: (data: GameEvent) => { |
96 |
| - gameEventWriteStream.write(`---\n${JSON.stringify(data, null, 2)}`); |
97 |
| - }, |
98 |
| - error: () => { |
99 |
| - gameEventWriteStream.end(); |
100 |
| - }, |
101 |
| - complete: () => { |
102 |
| - gameEventWriteStream.end(); |
103 |
| - }, |
104 |
| - }); |
| 81 | + if (isLogLevelEnabled(LogLevel.TRACE)) { |
| 82 | + this.logGameStreams({ |
| 83 | + socketData$, |
| 84 | + gameEvents$, |
| 85 | + }); |
| 86 | + } |
105 | 87 |
|
106 | 88 | return gameEvents$;
|
107 | 89 | }
|
@@ -146,4 +128,39 @@ export class GameServiceImpl implements GameService {
|
146 | 128 | throw new Error(`[GAME:SERVICE:DISCONNECT:TIMEOUT] ${timeout}`);
|
147 | 129 | }
|
148 | 130 | }
|
| 131 | + |
| 132 | + protected logGameStreams(options: { |
| 133 | + socketData$: rxjs.Observable<string>; |
| 134 | + gameEvents$: rxjs.Observable<GameEvent>; |
| 135 | + }): void { |
| 136 | + const { socketData$, gameEvents$ } = options; |
| 137 | + |
| 138 | + const writeStreamToFile = <T>(options: { |
| 139 | + stream$: rxjs.Observable<T>; |
| 140 | + filePath: string; |
| 141 | + }): void => { |
| 142 | + const { stream$, filePath } = options; |
| 143 | + |
| 144 | + const fileWriteStream = fs.createWriteStream(filePath); |
| 145 | + |
| 146 | + stream$.subscribe({ |
| 147 | + next: (data: T) => { |
| 148 | + fileWriteStream.write(`---\n${data}`); |
| 149 | + }, |
| 150 | + error: () => { |
| 151 | + fileWriteStream.end(); |
| 152 | + }, |
| 153 | + complete: () => { |
| 154 | + fileWriteStream.end(); |
| 155 | + }, |
| 156 | + }); |
| 157 | + }; |
| 158 | + |
| 159 | + const logPath = app.getPath('logs'); |
| 160 | + const socketLogPath = path.join(logPath, 'game-socket.log'); |
| 161 | + const eventLogPath = path.join(logPath, 'game-event.log'); |
| 162 | + |
| 163 | + writeStreamToFile({ stream$: socketData$, filePath: socketLogPath }); |
| 164 | + writeStreamToFile({ stream$: gameEvents$, filePath: eventLogPath }); |
| 165 | + } |
149 | 166 | }
|
0 commit comments