Skip to content

Commit ce5d683

Browse files
authored
[GEN][ZH] Fix and simplify the use of the log file in RecorderClass (#795)
1 parent 5c49484 commit ce5d683

File tree

6 files changed

+74
-64
lines changed

6 files changed

+74
-64
lines changed

Generals/Code/GameEngine/Include/Common/Debug.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ class AsciiString;
148148
#ifdef DEBUG_LOGGING
149149

150150
DEBUG_EXTERN_C void DebugLog(const char *format, ...);
151+
DEBUG_EXTERN_C const char* DebugGetLogFileName();
152+
DEBUG_EXTERN_C const char* DebugGetLogFileNamePrev();
151153

152154
// This defines a bitmask of log types that we care about, to allow some flexability
153155
// in what gets logged. This should be extended to asserts, too, but the assert box

Generals/Code/GameEngine/Source/Common/Recorder.cpp

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -282,19 +282,6 @@ void RecorderClass::logGameEnd( void )
282282
#endif
283283
}
284284

285-
#ifdef DEBUG_LOGGING
286-
#if defined(RTS_INTERNAL)
287-
#define DEBUG_FILE_NAME "DebugLogFileI.txt"
288-
#define DEBUG_FILE_NAME_PREV "DebugLogFilePrevI.txt"
289-
#elif defined(RTS_DEBUG)
290-
#define DEBUG_FILE_NAME "DebugLogFileD.txt"
291-
#define DEBUG_FILE_NAME_PREV "DebugLogFilePrevD.txt"
292-
#else
293-
#define DEBUG_FILE_NAME "DebugLogFile.txt"
294-
#define DEBUG_FILE_NAME_PREV "DebugLogFilePrev.txt"
295-
#endif
296-
#endif
297-
298285
void RecorderClass::cleanUpReplayFile( void )
299286
{
300287
#if defined(RTS_DEBUG) || defined(RTS_INTERNAL)
@@ -307,14 +294,18 @@ void RecorderClass::cleanUpReplayFile( void )
307294
AsciiString oldFname;
308295
oldFname.format("%s%s", getReplayDir().str(), m_fileName.str());
309296
CopyFile(oldFname.str(), fname, TRUE);
310-
#ifdef DEBUG_FILE_NAME
297+
298+
const char* logFileName = DebugGetLogFileName();
299+
if (logFileName[0] == '\0')
300+
return;
301+
311302
AsciiString debugFname = fname;
312303
debugFname.removeLastChar();
313304
debugFname.removeLastChar();
314305
debugFname.removeLastChar();
315306
debugFname.concat("txt");
316307
UnsignedInt fileSize = 0;
317-
FILE *fp = fopen(DEBUG_FILE_NAME, "rb");
308+
FILE *fp = fopen(logFileName, "rb");
318309
if (fp)
319310
{
320311
fseek(fp, 0, SEEK_END);
@@ -327,13 +318,13 @@ void RecorderClass::cleanUpReplayFile( void )
327318
const int MAX_DEBUG_SIZE = 65536;
328319
if (fileSize <= MAX_DEBUG_SIZE || TheGlobalData->m_saveAllStats)
329320
{
330-
DEBUG_LOG(("Using CopyFile to copy %s\n", DEBUG_FILE_NAME));
331-
CopyFile(DEBUG_FILE_NAME, debugFname.str(), TRUE);
321+
DEBUG_LOG(("Using CopyFile to copy %s\n", logFileName));
322+
CopyFile(logFileName, debugFname.str(), TRUE);
332323
}
333324
else
334325
{
335-
DEBUG_LOG(("manual copy of %s\n", DEBUG_FILE_NAME));
336-
FILE *ifp = fopen(DEBUG_FILE_NAME, "rb");
326+
DEBUG_LOG(("manual copy of %s\n", logFileName));
327+
FILE *ifp = fopen(logFileName, "rb");
337328
FILE *ofp = fopen(debugFname.str(), "wb");
338329
if (ifp && ofp)
339330
{
@@ -357,7 +348,6 @@ void RecorderClass::cleanUpReplayFile( void )
357348
ofp = NULL;
358349
}
359350
}
360-
#endif // DEBUG_FILE_NAME
361351
}
362352
#endif
363353
}

Generals/Code/GameEngine/Source/Common/System/Debug.cpp

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,12 @@ extern const char *gAppPrefix; /// So WB can have a different log file name.
105105
// ----------------------------------------------------------------------------
106106
// PRIVATE DATA
107107
// ----------------------------------------------------------------------------
108+
// TheSuperHackers @info Must not use static RAII types when set in DebugInit,
109+
// because DebugInit can be called during static module initialization before the main function is called.
108110
#ifdef DEBUG_LOGGING
109111
static FILE *theLogFile = NULL;
112+
static char theLogFileName[ _MAX_PATH ];
113+
static char theLogFileNamePrev[ _MAX_PATH ];
110114
#endif
111115
#define LARGE_BUFFER 8192
112116
static char theBuffer[ LARGE_BUFFER ]; // make it big to avoid weird overflow bugs in debug mode
@@ -372,22 +376,20 @@ void DebugInit(int flags)
372376
pEnd--;
373377
}
374378

375-
char prevbuf[ _MAX_PATH ];
376-
char curbuf[ _MAX_PATH ];
379+
strcpy(theLogFileNamePrev, dirbuf);
380+
strcat(theLogFileNamePrev, gAppPrefix);
381+
strcat(theLogFileNamePrev, DEBUG_FILE_NAME_PREV);
377382

378-
strcpy(prevbuf, dirbuf);
379-
strcat(prevbuf, gAppPrefix);
380-
strcat(prevbuf, DEBUG_FILE_NAME_PREV);
381-
strcpy(curbuf, dirbuf);
382-
strcat(curbuf, gAppPrefix);
383-
strcat(curbuf, DEBUG_FILE_NAME);
383+
strcpy(theLogFileName, dirbuf);
384+
strcat(theLogFileName, gAppPrefix);
385+
strcat(theLogFileName, DEBUG_FILE_NAME);
384386

385-
remove(prevbuf);
386-
rename(curbuf, prevbuf);
387-
theLogFile = fopen(curbuf, "w");
387+
remove(theLogFileNamePrev);
388+
rename(theLogFileName, theLogFileNamePrev);
389+
theLogFile = fopen(theLogFileName, "w");
388390
if (theLogFile != NULL)
389391
{
390-
DebugLog("Log %s opened: %s\n", curbuf, getCurrentTimeString());
392+
DebugLog("Log %s opened: %s\n", theLogFileName, getCurrentTimeString());
391393
}
392394
#endif
393395
}
@@ -424,6 +426,17 @@ void DebugLog(const char *format, ...)
424426
whackFunnyCharacters(theBuffer);
425427
doLogOutput(theBuffer);
426428
}
429+
430+
const char* DebugGetLogFileName()
431+
{
432+
return theLogFileName;
433+
}
434+
435+
const char* DebugGetLogFileNamePrev()
436+
{
437+
return theLogFileNamePrev;
438+
}
439+
427440
#endif
428441

429442
// ----------------------------------------------------------------------------

GeneralsMD/Code/GameEngine/Include/Common/Debug.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ class AsciiString;
154154
#ifdef DEBUG_LOGGING
155155

156156
DEBUG_EXTERN_C void DebugLog(const char *format, ...);
157+
DEBUG_EXTERN_C const char* DebugGetLogFileName();
158+
DEBUG_EXTERN_C const char* DebugGetLogFileNamePrev();
157159

158160
// This defines a bitmask of log types that we care about, to allow some flexability
159161
// in what gets logged. This should be extended to asserts, too, but the assert box

GeneralsMD/Code/GameEngine/Source/Common/Recorder.cpp

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -282,19 +282,6 @@ void RecorderClass::logGameEnd( void )
282282
#endif
283283
}
284284

285-
#ifdef DEBUG_LOGGING
286-
#if defined(RTS_INTERNAL)
287-
#define DEBUG_FILE_NAME "DebugLogFileI.txt"
288-
#define DEBUG_FILE_NAME_PREV "DebugLogFilePrevI.txt"
289-
#elif defined(RTS_DEBUG)
290-
#define DEBUG_FILE_NAME "DebugLogFileD.txt"
291-
#define DEBUG_FILE_NAME_PREV "DebugLogFilePrevD.txt"
292-
#else
293-
#define DEBUG_FILE_NAME "DebugLogFile.txt"
294-
#define DEBUG_FILE_NAME_PREV "DebugLogFilePrev.txt"
295-
#endif
296-
#endif
297-
298285
void RecorderClass::cleanUpReplayFile( void )
299286
{
300287
#if defined(RTS_DEBUG) || defined(RTS_INTERNAL)
@@ -307,14 +294,18 @@ void RecorderClass::cleanUpReplayFile( void )
307294
AsciiString oldFname;
308295
oldFname.format("%s%s", getReplayDir().str(), m_fileName.str());
309296
CopyFile(oldFname.str(), fname, TRUE);
310-
#ifdef DEBUG_FILE_NAME
297+
298+
const char* logFileName = DebugGetLogFileName();
299+
if (logFileName[0] == '\0')
300+
return;
301+
311302
AsciiString debugFname = fname;
312303
debugFname.removeLastChar();
313304
debugFname.removeLastChar();
314305
debugFname.removeLastChar();
315306
debugFname.concat("txt");
316307
UnsignedInt fileSize = 0;
317-
FILE *fp = fopen(DEBUG_FILE_NAME, "rb");
308+
FILE *fp = fopen(logFileName, "rb");
318309
if (fp)
319310
{
320311
fseek(fp, 0, SEEK_END);
@@ -327,13 +318,13 @@ void RecorderClass::cleanUpReplayFile( void )
327318
const int MAX_DEBUG_SIZE = 65536;
328319
if (fileSize <= MAX_DEBUG_SIZE || TheGlobalData->m_saveAllStats)
329320
{
330-
DEBUG_LOG(("Using CopyFile to copy %s\n", DEBUG_FILE_NAME));
331-
CopyFile(DEBUG_FILE_NAME, debugFname.str(), TRUE);
321+
DEBUG_LOG(("Using CopyFile to copy %s\n", logFileName));
322+
CopyFile(logFileName, debugFname.str(), TRUE);
332323
}
333324
else
334325
{
335-
DEBUG_LOG(("manual copy of %s\n", DEBUG_FILE_NAME));
336-
FILE *ifp = fopen(DEBUG_FILE_NAME, "rb");
326+
DEBUG_LOG(("manual copy of %s\n", logFileName));
327+
FILE *ifp = fopen(logFileName, "rb");
337328
FILE *ofp = fopen(debugFname.str(), "wb");
338329
if (ifp && ofp)
339330
{
@@ -357,7 +348,6 @@ void RecorderClass::cleanUpReplayFile( void )
357348
ofp = NULL;
358349
}
359350
}
360-
#endif // DEBUG_FILE_NAME
361351
}
362352
#endif
363353
}

GeneralsMD/Code/GameEngine/Source/Common/System/Debug.cpp

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,12 @@ extern const char *gAppPrefix; /// So WB can have a different log file name.
106106
// ----------------------------------------------------------------------------
107107
// PRIVATE DATA
108108
// ----------------------------------------------------------------------------
109+
// TheSuperHackers @info Must not use static RAII types when set in DebugInit,
110+
// because DebugInit can be called during static module initialization before the main function is called.
109111
#ifdef DEBUG_LOGGING
110112
static FILE *theLogFile = NULL;
113+
static char theLogFileName[ _MAX_PATH ];
114+
static char theLogFileNamePrev[ _MAX_PATH ];
111115
#endif
112116
#define LARGE_BUFFER 8192
113117
static char theBuffer[ LARGE_BUFFER ]; // make it big to avoid weird overflow bugs in debug mode
@@ -373,22 +377,20 @@ void DebugInit(int flags)
373377
pEnd--;
374378
}
375379

376-
char prevbuf[ _MAX_PATH ];
377-
char curbuf[ _MAX_PATH ];
380+
strcpy(theLogFileNamePrev, dirbuf);
381+
strcat(theLogFileNamePrev, gAppPrefix);
382+
strcat(theLogFileNamePrev, DEBUG_FILE_NAME_PREV);
378383

379-
strcpy(prevbuf, dirbuf);
380-
strcat(prevbuf, gAppPrefix);
381-
strcat(prevbuf, DEBUG_FILE_NAME_PREV);
382-
strcpy(curbuf, dirbuf);
383-
strcat(curbuf, gAppPrefix);
384-
strcat(curbuf, DEBUG_FILE_NAME);
384+
strcpy(theLogFileName, dirbuf);
385+
strcat(theLogFileName, gAppPrefix);
386+
strcat(theLogFileName, DEBUG_FILE_NAME);
385387

386-
remove(prevbuf);
387-
rename(curbuf, prevbuf);
388-
theLogFile = fopen(curbuf, "w");
388+
remove(theLogFileNamePrev);
389+
rename(theLogFileName, theLogFileNamePrev);
390+
theLogFile = fopen(theLogFileName, "w");
389391
if (theLogFile != NULL)
390392
{
391-
DebugLog("Log %s opened: %s\n", curbuf, getCurrentTimeString());
393+
DebugLog("Log %s opened: %s\n", theLogFileName, getCurrentTimeString());
392394
}
393395
#endif
394396
}
@@ -425,6 +427,17 @@ void DebugLog(const char *format, ...)
425427
whackFunnyCharacters(theBuffer);
426428
doLogOutput(theBuffer);
427429
}
430+
431+
const char* DebugGetLogFileName()
432+
{
433+
return theLogFileName;
434+
}
435+
436+
const char* DebugGetLogFileNamePrev()
437+
{
438+
return theLogFileNamePrev;
439+
}
440+
428441
#endif
429442

430443
// ----------------------------------------------------------------------------

0 commit comments

Comments
 (0)