From 457e986b7514c0e9fa921c1961344012443ac164 Mon Sep 17 00:00:00 2001 From: James R Date: Mon, 23 Dec 2019 17:21:58 -0800 Subject: [PATCH] -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();