Merge branch 'time' into 'master'

haha log files

See merge request STJr/SRB2!567
This commit is contained in:
James R 2019-12-26 21:43:46 -05:00
commit e19d0877e8
5 changed files with 212 additions and 18 deletions

View File

@ -127,7 +127,7 @@
#ifdef LOGMESSAGES
extern FILE *logstream;
extern char logfilename[1024];
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

View File

@ -2449,3 +2449,92 @@ 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_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)
p = &path[3];
else
#endif
p = path;
if (end > 0)
end -= start;
for (; start > 0; --start)
{
p += strspn(p, PATHSEP);
if (!( p = strchr(p, PATHSEP[0]) ))
return;
}
p += strspn(p, PATHSEP);
for (;;)
{
if (end > 0 && !--end)
break;
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;
}
}
}
void M_MkdirEach(const char *path, int start, int mode)
{
M_MkdirEachUntil(path, start, -1, mode);
}

View File

@ -96,6 +96,11 @@ 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);
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);

View File

@ -20,12 +20,17 @@
#include "../doomdef.h"
#include "../m_argv.h"
#include "../d_main.h"
#include "../m_misc.h"/* path shit */
#include "../i_system.h"
#ifdef __GNUC__
#if defined (__GNUC__) || defined (__unix__)
#include <unistd.h>
#endif
#ifdef __unix__
#include <errno.h>
#endif
#include "time.h" // For log timestamps
#ifdef HAVE_SDL
@ -47,7 +52,7 @@ extern int SDL_main(int argc, char *argv[]);
#ifdef LOGMESSAGES
FILE *logstream = NULL;
char logfilename[1024];
char logfilename[1024];
#endif
#ifndef DOXYGEN
@ -133,34 +138,84 @@ int main(int argc, char **argv)
{
time_t my_time;
struct tm * timeinfo;
char buf[26];
const char *format;
const char *reldir;
int left;
boolean fileabs;
const char *link;
logdir = D_Home();
my_time = time(NULL);
timeinfo = localtime(&my_time);
strftime(buf, 26, "%Y-%m-%d %H-%M-%S", timeinfo);
strcpy(logfilename, va("log-%s.txt", buf));
#ifdef DEFAULTDIR
if (logdir)
if (M_CheckParm("-logfile") && M_IsNextParm())
{
// 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);
strcpy(logfilename, va("%s%s"DEFAULTDIR"%slogs%s%s",logdir, PATHSEP, PATHSEP, PATHSEP, logfilename));
format = M_GetNextParm();
fileabs = M_IsPathAbsolute(format);
}
else
#endif
{
I_mkdir("."PATHSEP"logs"PATHSEP, 0755);
strcpy(logfilename, va("."PATHSEP"logs"PATHSEP"%s", logfilename));
format = "log-%Y-%m-%d_%H-%M-%S.txt";
fileabs = false;
}
logstream = fopen(logfilename, "wt");
if (fileabs)
{
strftime(logfilename, sizeof logfilename, format, timeinfo);
}
else
{
if (M_CheckParm("-logdir") && M_IsNextParm())
reldir = M_GetNextParm();
else
reldir = "logs";
if (M_IsPathAbsolute(reldir))
{
left = snprintf(logfilename, sizeof logfilename,
"%s"PATHSEP, reldir);
}
else
#ifdef DEFAULTDIR
if (logdir)
{
left = snprintf(logfilename, sizeof logfilename,
"%s"PATHSEP DEFAULTDIR PATHSEP"%s"PATHSEP, logdir, reldir);
}
else
#endif/*DEFAULTDIR*/
{
left = snprintf(logfilename, sizeof logfilename,
"."PATHSEP"%s"PATHSEP, reldir);
}
#endif/*LOGMESSAGES*/
strftime(&logfilename[left], sizeof logfilename - left,
format, timeinfo);
}
M_MkdirEachUntil(logfilename,
M_PathParts(logdir) - 1,
M_PathParts(logfilename) - 1, 0755);
#ifdef __unix__
logstream = fopen(logfilename, "w");
#ifdef DEFAULTDIR
if (logdir)
link = va("%s/"DEFAULTDIR"/latest-log.txt", logdir);
else
#endif/*DEFAULTDIR*/
link = "latest-log.txt";
unlink(link);
if (symlink(logfilename, link) == -1)
{
I_OutputMsg("Error symlinking latest-log.txt: %s\n", strerror(errno));
}
#else/*__unix__*/
logstream = fopen("latest-log.txt", "wt+");
#endif/*__unix__*/
}
#endif
//I_OutputMsg("I_StartupSystem() ...\n");
I_StartupSystem();

View File

@ -2484,6 +2484,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
@ -2506,6 +2548,9 @@ void I_ShutdownSystem(void)
if (logstream)
{
I_OutputMsg("I_ShutdownSystem(): end of logstream.\n");
#ifndef __unix__
Shittylogcopy();
#endif
fclose(logstream);
logstream = NULL;
}