Handle log file in parent properly

This commit is contained in:
James R 2019-12-13 16:51:49 -08:00
parent 553ad46c74
commit 8a23ff0bc8
3 changed files with 22 additions and 6 deletions

View File

@ -127,6 +127,7 @@
#ifdef LOGMESSAGES #ifdef LOGMESSAGES
extern FILE *logstream; extern FILE *logstream;
extern char logfilename[1024];
#endif #endif
//#define DEVELOP // Disable this for release builds to remove excessive cheat commands and enable MD5 checking and stuff, all in one go. :3 //#define DEVELOP // Disable this for release builds to remove excessive cheat commands and enable MD5 checking and stuff, all in one go. :3

View File

@ -47,6 +47,7 @@ extern int SDL_main(int argc, char *argv[]);
#ifdef LOGMESSAGES #ifdef LOGMESSAGES
FILE *logstream = NULL; FILE *logstream = NULL;
char logfilename[1024];
#endif #endif
#ifndef DOXYGEN #ifndef DOXYGEN
@ -116,7 +117,6 @@ int main(int argc, char **argv)
#endif #endif
{ {
const char *logdir = NULL; const char *logdir = NULL;
char logfile[MAX_WADPATH];
myargc = argc; myargc = argc;
myargv = argv; /// \todo pull out path to exe from this string myargv = argv; /// \todo pull out path to exe from this string
@ -141,7 +141,7 @@ int main(int argc, char **argv)
timeinfo = localtime(&my_time); timeinfo = localtime(&my_time);
strftime(buf, 26, "%Y-%m-%d %H-%M-%S", timeinfo); strftime(buf, 26, "%Y-%m-%d %H-%M-%S", timeinfo);
strcpy(logfile, va("log-%s.txt", buf)); strcpy(logfilename, va("log-%s.txt", buf));
#ifdef DEFAULTDIR #ifdef DEFAULTDIR
if (logdir) if (logdir)
@ -149,14 +149,16 @@ int main(int argc, char **argv)
// Create dirs here because D_SRB2Main() is too late. // Create dirs here because D_SRB2Main() is too late.
I_mkdir(va("%s%s"DEFAULTDIR, logdir, PATHSEP), 0755); I_mkdir(va("%s%s"DEFAULTDIR, logdir, PATHSEP), 0755);
I_mkdir(va("%s%s"DEFAULTDIR"%slogs",logdir, PATHSEP, PATHSEP), 0755); I_mkdir(va("%s%s"DEFAULTDIR"%slogs",logdir, PATHSEP, PATHSEP), 0755);
logstream = fopen(va("%s%s"DEFAULTDIR"%slogs%s%s",logdir, PATHSEP, PATHSEP, PATHSEP, logfile), "wt"); strcpy(logfilename, va("%s%s"DEFAULTDIR"%slogs%s%s",logdir, PATHSEP, PATHSEP, PATHSEP, logfilename));
} }
else else
#endif #endif
{ {
I_mkdir("."PATHSEP"logs"PATHSEP, 0755); I_mkdir("."PATHSEP"logs"PATHSEP, 0755);
logstream = fopen(va("."PATHSEP"logs"PATHSEP"%s", logfile), "wt"); strcpy(logfilename, va("."PATHSEP"logs"PATHSEP"%s", logfilename));
} }
logstream = fopen(logfilename, "wt");
} }
#endif #endif
@ -187,7 +189,7 @@ int main(int argc, char **argv)
D_SRB2Main(); D_SRB2Main();
#ifdef LOGMESSAGES #ifdef LOGMESSAGES
if (!M_CheckParm("-nolog")) if (!M_CheckParm("-nolog"))
CONS_Printf("Logfile: %s\n", logfile); CONS_Printf("Logfile: %s\n", logfilename);
#endif #endif
CONS_Printf("Entering main game loop...\n"); CONS_Printf("Entering main game loop...\n");
// never return // never return

View File

@ -2192,6 +2192,7 @@ static void I_Fork(void)
int child; int child;
int status; int status;
int signum; int signum;
int c;
child = fork(); child = fork();
@ -2203,7 +2204,19 @@ static void I_Fork(void)
case 0: case 0:
break; break;
default: default:
if (wait(&status) == -1) if (logstream)
fclose(logstream);/* the child has this */
c = wait(&status);
#ifdef LOGMESSAGES
/* By the way, exit closes files. */
logstream = fopen(logfilename, "at");
#else
logstream = 0;
#endif
if (c == -1)
{ {
kill(child, SIGKILL); kill(child, SIGKILL);
newsignalhandler_Warn("wait()"); newsignalhandler_Warn("wait()");