From d9d13764e6840e036c095c6d87838a4dfede80f4 Mon Sep 17 00:00:00 2001 From: James R Date: Mon, 23 Dec 2019 15:31:50 -0800 Subject: [PATCH 1/8] -logfile to let the user change the log file name --- src/sdl/i_main.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/sdl/i_main.c b/src/sdl/i_main.c index 029febc05..32cc16208 100644 --- a/src/sdl/i_main.c +++ b/src/sdl/i_main.c @@ -133,15 +133,19 @@ int main(int argc, char **argv) { time_t my_time; struct tm * timeinfo; - char buf[26]; + const char *format; logdir = D_Home(); my_time = time(NULL); timeinfo = localtime(&my_time); - strftime(buf, 26, "%Y-%m-%d %H-%M-%S", timeinfo); - strcpy(logfile, va("log-%s.txt", buf)); + if (M_CheckParm("-logfile") && M_IsNextParm()) + format = M_GetNextParm(); + else + format = "log-%Y-%m-%d %H-%M-%S.txt"; + + strftime(logfile, sizeof logfile, format, timeinfo); #ifdef DEFAULTDIR if (logdir) From c285000c5668ab82545ec9ee4989aabf65e2585e Mon Sep 17 00:00:00 2001 From: James R Date: Mon, 23 Dec 2019 15:34:27 -0800 Subject: [PATCH 2/8] Change default log filename to not use a space bleh --- src/sdl/i_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sdl/i_main.c b/src/sdl/i_main.c index 32cc16208..326f1a929 100644 --- a/src/sdl/i_main.c +++ b/src/sdl/i_main.c @@ -143,7 +143,7 @@ int main(int argc, char **argv) if (M_CheckParm("-logfile") && M_IsNextParm()) format = M_GetNextParm(); else - format = "log-%Y-%m-%d %H-%M-%S.txt"; + format = "log-%Y-%m-%d_%H-%M-%S.txt"; strftime(logfile, sizeof logfile, format, timeinfo); From 457e986b7514c0e9fa921c1961344012443ac164 Mon Sep 17 00:00:00 2001 From: James R Date: Mon, 23 Dec 2019 17:21:58 -0800 Subject: [PATCH 3/8] -logdir lets the user change the log directory --- src/m_misc.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ src/m_misc.h | 4 +++ src/sdl/i_main.c | 34 ++++++++++++++++------ 3 files changed, 102 insertions(+), 9 deletions(-) diff --git a/src/m_misc.c b/src/m_misc.c index d97383385..69e554a2c 100644 --- a/src/m_misc.c +++ b/src/m_misc.c @@ -2453,3 +2453,76 @@ const char *M_FileError(FILE *fp) else return "end-of-file"; } + +/** Return the number of parts of this path. +*/ +int M_PathParts(const char *path) +{ + int n; + const char *p; + const char *t; + for (n = 0, p = path ;; ++n) + { + t = p; + if (( p = strchr(p, PATHSEP[0]) )) + p += strspn(p, PATHSEP); + else + { + if (*t)/* there is something after the final delimiter */ + n++; + break; + } + } + return n; +} + +/** Check whether a path is an absolute path. +*/ +boolean M_IsPathAbsolute(const char *path) +{ +#ifdef _WIN32 + return ( strncmp(&path[1], ":\\", 2) == 0 ); +#else + return ( path[0] == '/' ); +#endif +} + +/** I_mkdir for each part of the path. +*/ +void M_MkdirEach(const char *cpath, int start, int mode) +{ + char path[MAX_WADPATH]; + char *p; + char *t; + strlcpy(path, cpath, sizeof path); +#ifdef _WIN32 + if (strncmp(&path[1], ":\\", 2) == 0) + p = &path[3]; + else +#endif + p = path; + for (; start > 0; --start) + { + p += strspn(p, PATHSEP); + if (!( p = strchr(p, PATHSEP[0]) )) + return; + } + p += strspn(p, PATHSEP); + for (;;) + { + t = p; + if (( p = strchr(p, PATHSEP[0]) )) + { + *p = '\0'; + I_mkdir(path, mode); + *p = PATHSEP[0]; + p += strspn(p, PATHSEP); + } + else + { + if (*t) + I_mkdir(path, mode); + break; + } + } +} diff --git a/src/m_misc.h b/src/m_misc.h index 99ca8d0c9..9f570df20 100644 --- a/src/m_misc.h +++ b/src/m_misc.h @@ -96,6 +96,10 @@ void M_SetupMemcpy(void); const char *M_FileError(FILE *handle); +int M_PathParts (const char *path); +boolean M_IsPathAbsolute (const char *path); +void M_MkdirEach (const char *path, int start, int mode); + // counting bits, for weapon ammo code, usually FUNCMATH UINT8 M_CountBits(UINT32 num, UINT8 size); diff --git a/src/sdl/i_main.c b/src/sdl/i_main.c index 326f1a929..7a8ebcfd3 100644 --- a/src/sdl/i_main.c +++ b/src/sdl/i_main.c @@ -20,6 +20,7 @@ #include "../doomdef.h" #include "../m_argv.h" #include "../d_main.h" +#include "../m_misc.h"/* path shit */ #include "../i_system.h" #ifdef __GNUC__ @@ -134,6 +135,8 @@ int main(int argc, char **argv) time_t my_time; struct tm * timeinfo; const char *format; + const char *reldir; + int left; logdir = D_Home(); @@ -145,24 +148,37 @@ int main(int argc, char **argv) else format = "log-%Y-%m-%d_%H-%M-%S.txt"; - strftime(logfile, sizeof logfile, format, timeinfo); + if (M_CheckParm("-logdir") && M_IsNextParm()) + reldir = M_GetNextParm(); + else + reldir = "logs"; + if (M_IsPathAbsolute(reldir)) + { + left = snprintf(logfile, sizeof logfile, + "%s"PATHSEP, reldir); + } + else #ifdef DEFAULTDIR if (logdir) { - // Create dirs here because D_SRB2Main() is too late. - I_mkdir(va("%s%s"DEFAULTDIR, logdir, 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"); + left = snprintf(logfile, sizeof logfile, + "%s"PATHSEP DEFAULTDIR PATHSEP"%s"PATHSEP, logdir, reldir); } else -#endif +#endif/*DEFAULTDIR*/ { - I_mkdir("."PATHSEP"logs"PATHSEP, 0755); - logstream = fopen(va("."PATHSEP"logs"PATHSEP"%s", logfile), "wt"); + left = snprintf(logfile, sizeof logfile, + "."PATHSEP"%s"PATHSEP, reldir); } +#endif/*LOGMESSAGES*/ + + M_MkdirEach(logfile, M_PathParts(logdir) - 1, 0755); + + strftime(&logfile[left], sizeof logfile - left, format, timeinfo); + + logstream = fopen(logfile, "wt"); } -#endif //I_OutputMsg("I_StartupSystem() ...\n"); I_StartupSystem(); From 5fbe77cdda7ec52bc2ca10ab9253df8dc56d2317 Mon Sep 17 00:00:00 2001 From: James R Date: Mon, 23 Dec 2019 17:40:43 -0800 Subject: [PATCH 4/8] Let an asbolute path work with -logfile --- src/m_misc.c | 18 ++++++++++++++- src/m_misc.h | 1 + src/sdl/i_main.c | 58 +++++++++++++++++++++++++++++++----------------- 3 files changed, 56 insertions(+), 21 deletions(-) diff --git a/src/m_misc.c b/src/m_misc.c index 69e554a2c..9149c9cce 100644 --- a/src/m_misc.c +++ b/src/m_misc.c @@ -2489,11 +2489,15 @@ boolean M_IsPathAbsolute(const char *path) /** I_mkdir for each part of the path. */ -void M_MkdirEach(const char *cpath, int start, int mode) +void M_MkdirEachUntil(const char *cpath, int start, int end, int mode) { char path[MAX_WADPATH]; char *p; char *t; + + if (end > 0 && end <= start) + return; + strlcpy(path, cpath, sizeof path); #ifdef _WIN32 if (strncmp(&path[1], ":\\", 2) == 0) @@ -2501,6 +2505,10 @@ void M_MkdirEach(const char *cpath, int start, int mode) else #endif p = path; + + if (end > 0) + end -= start; + for (; start > 0; --start) { p += strspn(p, PATHSEP); @@ -2510,6 +2518,9 @@ void M_MkdirEach(const char *cpath, int start, int mode) p += strspn(p, PATHSEP); for (;;) { + if (end > 0 && !--end) + break; + t = p; if (( p = strchr(p, PATHSEP[0]) )) { @@ -2526,3 +2537,8 @@ void M_MkdirEach(const char *cpath, int start, int mode) } } } + +void M_MkdirEach(const char *path, int start, int mode) +{ + M_MkdirEachUntil(path, start, -1, mode); +} diff --git a/src/m_misc.h b/src/m_misc.h index 9f570df20..e0a73e0b7 100644 --- a/src/m_misc.h +++ b/src/m_misc.h @@ -99,6 +99,7 @@ const char *M_FileError(FILE *handle); int M_PathParts (const char *path); boolean M_IsPathAbsolute (const char *path); void M_MkdirEach (const char *path, int start, int mode); +void M_MkdirEachUntil (const char *path, int start, int end, int mode); // counting bits, for weapon ammo code, usually FUNCMATH UINT8 M_CountBits(UINT32 num, UINT8 size); diff --git a/src/sdl/i_main.c b/src/sdl/i_main.c index 7a8ebcfd3..71b38841a 100644 --- a/src/sdl/i_main.c +++ b/src/sdl/i_main.c @@ -137,6 +137,7 @@ int main(int argc, char **argv) const char *format; const char *reldir; int left; + boolean fileabs; logdir = D_Home(); @@ -144,38 +145,55 @@ int main(int argc, char **argv) timeinfo = localtime(&my_time); if (M_CheckParm("-logfile") && M_IsNextParm()) + { format = M_GetNextParm(); + fileabs = M_IsPathAbsolute(format); + } else + { format = "log-%Y-%m-%d_%H-%M-%S.txt"; + fileabs = false; + } - if (M_CheckParm("-logdir") && M_IsNextParm()) - reldir = M_GetNextParm(); - else - reldir = "logs"; - - if (M_IsPathAbsolute(reldir)) + if (fileabs) { - left = snprintf(logfile, sizeof logfile, - "%s"PATHSEP, reldir); + strftime(logfile, sizeof logfile, format, timeinfo); + + M_MkdirEachUntil(logfile, + M_PathParts(logdir) - 1, + M_PathParts(logfile) - 1, 0755); } else + { + if (M_CheckParm("-logdir") && M_IsNextParm()) + reldir = M_GetNextParm(); + else + reldir = "logs"; + + if (M_IsPathAbsolute(reldir)) + { + left = snprintf(logfile, sizeof logfile, + "%s"PATHSEP, reldir); + } + else #ifdef DEFAULTDIR - if (logdir) - { - left = snprintf(logfile, sizeof logfile, - "%s"PATHSEP DEFAULTDIR PATHSEP"%s"PATHSEP, logdir, reldir); - } - else + if (logdir) + { + left = snprintf(logfile, sizeof logfile, + "%s"PATHSEP DEFAULTDIR PATHSEP"%s"PATHSEP, logdir, reldir); + } + else #endif/*DEFAULTDIR*/ - { - left = snprintf(logfile, sizeof logfile, - "."PATHSEP"%s"PATHSEP, reldir); - } + { + left = snprintf(logfile, sizeof logfile, + "."PATHSEP"%s"PATHSEP, reldir); + } #endif/*LOGMESSAGES*/ - M_MkdirEach(logfile, M_PathParts(logdir) - 1, 0755); + M_MkdirEach(logfile, M_PathParts(logdir) - 1, 0755); - strftime(&logfile[left], sizeof logfile - left, format, timeinfo); + strftime(&logfile[left], sizeof logfile - left, format, timeinfo); + } logstream = fopen(logfile, "wt"); } From 98cb238d36e0f85d9d4b4d2cff59c720e1630bdd Mon Sep 17 00:00:00 2001 From: James R Date: Mon, 23 Dec 2019 17:43:42 -0800 Subject: [PATCH 5/8] Create directories from -logfile too --- src/sdl/i_main.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/sdl/i_main.c b/src/sdl/i_main.c index 71b38841a..70f582676 100644 --- a/src/sdl/i_main.c +++ b/src/sdl/i_main.c @@ -158,10 +158,6 @@ int main(int argc, char **argv) if (fileabs) { strftime(logfile, sizeof logfile, format, timeinfo); - - M_MkdirEachUntil(logfile, - M_PathParts(logdir) - 1, - M_PathParts(logfile) - 1, 0755); } else { @@ -190,11 +186,13 @@ int main(int argc, char **argv) } #endif/*LOGMESSAGES*/ - M_MkdirEach(logfile, M_PathParts(logdir) - 1, 0755); - strftime(&logfile[left], sizeof logfile - left, format, timeinfo); } + M_MkdirEachUntil(logfile, + M_PathParts(logdir) - 1, + M_PathParts(logfile) - 1, 0755); + logstream = fopen(logfile, "wt"); } From 25525a6aae84581ac03ca3089950ad4ff25b8512 Mon Sep 17 00:00:00 2001 From: James R Date: Mon, 23 Dec 2019 18:20:04 -0800 Subject: [PATCH 6/8] symlink latest-log.txt on nix, copy to the real log file everywhere else --- src/doomdef.h | 1 + src/sdl/i_main.c | 40 +++++++++++++++++++++++++++++----------- src/sdl/i_system.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 11 deletions(-) diff --git a/src/doomdef.h b/src/doomdef.h index 6c4f1fef3..fe6a5faa3 100644 --- a/src/doomdef.h +++ b/src/doomdef.h @@ -127,6 +127,7 @@ #ifdef LOGMESSAGES extern FILE *logstream; +extern char logfilename[1024]; #endif //#define DEVELOP // Disable this for release builds to remove excessive cheat commands and enable MD5 checking and stuff, all in one go. :3 diff --git a/src/sdl/i_main.c b/src/sdl/i_main.c index 70f582676..af34da8d1 100644 --- a/src/sdl/i_main.c +++ b/src/sdl/i_main.c @@ -23,10 +23,14 @@ #include "../m_misc.h"/* path shit */ #include "../i_system.h" -#ifdef __GNUC__ +#if defined (__GNUC__) || defined (__unix__) #include #endif +#ifdef __unix__ +#include +#endif + #include "time.h" // For log timestamps #ifdef HAVE_SDL @@ -48,6 +52,7 @@ extern int SDL_main(int argc, char *argv[]); #ifdef LOGMESSAGES FILE *logstream = NULL; +char logfilename[1024]; #endif #ifndef DOXYGEN @@ -117,7 +122,6 @@ int main(int argc, char **argv) #endif { const char *logdir = NULL; - char logfile[MAX_WADPATH]; myargc = argc; myargv = argv; /// \todo pull out path to exe from this string @@ -157,7 +161,7 @@ int main(int argc, char **argv) if (fileabs) { - strftime(logfile, sizeof logfile, format, timeinfo); + strftime(logfilename, sizeof logfilename, format, timeinfo); } else { @@ -168,32 +172,46 @@ int main(int argc, char **argv) if (M_IsPathAbsolute(reldir)) { - left = snprintf(logfile, sizeof logfile, + left = snprintf(logfilename, sizeof logfilename, "%s"PATHSEP, reldir); } else #ifdef DEFAULTDIR if (logdir) { - left = snprintf(logfile, sizeof logfile, + left = snprintf(logfilename, sizeof logfilename, "%s"PATHSEP DEFAULTDIR PATHSEP"%s"PATHSEP, logdir, reldir); } else #endif/*DEFAULTDIR*/ { - left = snprintf(logfile, sizeof logfile, + left = snprintf(logfilename, sizeof logfilename, "."PATHSEP"%s"PATHSEP, reldir); } #endif/*LOGMESSAGES*/ - strftime(&logfile[left], sizeof logfile - left, format, timeinfo); + strftime(&logfilename[left], sizeof logfilename - left, + format, timeinfo); } - M_MkdirEachUntil(logfile, + M_MkdirEachUntil(logfilename, M_PathParts(logdir) - 1, - M_PathParts(logfile) - 1, 0755); + M_PathParts(logfilename) - 1, 0755); - logstream = fopen(logfile, "wt"); +#ifdef __unix__ + logstream = fopen(logfilename, "w"); +#ifdef DEFAULTDIR + if (symlink(logfilename, + va("%s/"DEFAULTDIR"/latest-log.txt", logdir)) == -1) +#else + if (symlink(logfilename, va("%s/latest-log.txt", logdir)) == -1) +#endif/*DEFAULTDIR*/ + { + I_OutputMsg("Error symlinking latest-log.txt: %s\n", strerror(errno)); + } +#else/*__unix__*/ + logstream = fopen("latest-log.txt", "wt+"); +#endif/*__unix__*/ } //I_OutputMsg("I_StartupSystem() ...\n"); @@ -222,7 +240,7 @@ int main(int argc, char **argv) D_SRB2Main(); #ifdef LOGMESSAGES if (!M_CheckParm("-nolog")) - CONS_Printf("Logfile: %s\n", logfile); + CONS_Printf("Logfile: %s\n", logfilename); #endif CONS_Printf("Entering main game loop...\n"); // never return diff --git a/src/sdl/i_system.c b/src/sdl/i_system.c index 13fb25bea..355131f3b 100644 --- a/src/sdl/i_system.c +++ b/src/sdl/i_system.c @@ -2377,6 +2377,48 @@ void I_RemoveExitFunc(void (*func)()) } } +#ifndef __unix__ +static void Shittycopyerror(const char *name) +{ + I_OutputMsg( + "Error copying log file: %s: %s\n", + name, + strerror(errno) + ); +} + +static void Shittylogcopy(void) +{ + char buf[8192]; + FILE *fp; + int n; + if (fseek(logstream, 0, SEEK_SET) == -1) + { + Shittycopyerror("fseek"); + } + else if (( fp = fopen(logfilename, "wt") )) + { + while (( n = fread(buf, 1, sizeof buf, logstream) )) + { + if (fwrite(buf, 1, n, fp) < n) + { + Shittycopyerror("fwrite"); + break; + } + } + if (ferror(logstream)) + { + Shittycopyerror("fread"); + } + fclose(fp); + } + else + { + Shittycopyerror(logfilename); + } +} +#endif/*__unix__*/ + // // Closes down everything. This includes restoring the initial // palette and video mode, and removing whatever mouse, keyboard, and @@ -2395,6 +2437,9 @@ void I_ShutdownSystem(void) if (logstream) { I_OutputMsg("I_ShutdownSystem(): end of logstream.\n"); +#ifndef __unix__ + Shittylogcopy(); +#endif fclose(logstream); logstream = NULL; } From f2c2836301d4dc59242d09f08a5473341c4d692c Mon Sep 17 00:00:00 2001 From: James R Date: Tue, 24 Dec 2019 01:55:47 -0800 Subject: [PATCH 7/8] Overwrite an already existing symlink --- src/sdl/i_main.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/sdl/i_main.c b/src/sdl/i_main.c index af34da8d1..f798ca6cd 100644 --- a/src/sdl/i_main.c +++ b/src/sdl/i_main.c @@ -142,6 +142,7 @@ int main(int argc, char **argv) const char *reldir; int left; boolean fileabs; + const char *link; logdir = D_Home(); @@ -201,11 +202,12 @@ int main(int argc, char **argv) #ifdef __unix__ logstream = fopen(logfilename, "w"); #ifdef DEFAULTDIR - if (symlink(logfilename, - va("%s/"DEFAULTDIR"/latest-log.txt", logdir)) == -1) + link = va("%s/"DEFAULTDIR"/latest-log.txt", logdir); #else - if (symlink(logfilename, va("%s/latest-log.txt", logdir)) == -1) + link = va("%s/latest-log.txt", logdir); #endif/*DEFAULTDIR*/ + unlink(link); + if (symlink(logfilename, link) == -1) { I_OutputMsg("Error symlinking latest-log.txt: %s\n", strerror(errno)); } From b7b4945c36fb95f406a365d64f109cfc83ad4d63 Mon Sep 17 00:00:00 2001 From: James R Date: Tue, 24 Dec 2019 01:58:39 -0800 Subject: [PATCH 8/8] Correct usage of logdir --- src/sdl/i_main.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/sdl/i_main.c b/src/sdl/i_main.c index f798ca6cd..0de96ad97 100644 --- a/src/sdl/i_main.c +++ b/src/sdl/i_main.c @@ -202,10 +202,11 @@ int main(int argc, char **argv) #ifdef __unix__ logstream = fopen(logfilename, "w"); #ifdef DEFAULTDIR - link = va("%s/"DEFAULTDIR"/latest-log.txt", logdir); -#else - link = va("%s/latest-log.txt", logdir); + if (logdir) + link = va("%s/"DEFAULTDIR"/latest-log.txt", logdir); + else #endif/*DEFAULTDIR*/ + link = "latest-log.txt"; unlink(link); if (symlink(logfilename, link) == -1) {