Merge branch 'logs-port' into 'master'

2.2 directory structure and log files port

See merge request KartKrew/Kart!224
This commit is contained in:
Sal 2020-03-28 22:16:50 -04:00
commit eb9144338a
12 changed files with 300 additions and 36 deletions

View file

@ -152,6 +152,7 @@ char srb2path[256] = ".";
#endif
boolean usehome = true;
const char *pandf = "%s" PATHSEP "%s";
static char addonsdir[MAX_WADPATH];
//
// EVENT HANDLING
@ -1091,7 +1092,6 @@ void D_SRB2Main(void)
// can't use sprintf since there is %u in savegamename
strcatbf(savegamename, srb2home, PATHSEP);
I_mkdir(srb2home, 0700);
#else
snprintf(srb2home, sizeof srb2home, "%s", userhome);
snprintf(downloaddir, sizeof downloaddir, "%s", userhome);
@ -1112,6 +1112,10 @@ void D_SRB2Main(void)
#endif
}
// Create addons dir
snprintf(addonsdir, sizeof addonsdir, "%s%s%s", srb2home, PATHSEP, "addons");
I_mkdir(addonsdir, 0755);
// rand() needs seeded regardless of password
srand((unsigned int)time(NULL));

View file

@ -812,6 +812,8 @@ void D_RegisterClientCommands(void)
CV_RegisterVar(&cv_screenshot_option);
CV_RegisterVar(&cv_screenshot_folder);
CV_RegisterVar(&cv_moviemode);
CV_RegisterVar(&cv_movie_option);
CV_RegisterVar(&cv_movie_folder);
// PNG variables
CV_RegisterVar(&cv_zlib_level);
CV_RegisterVar(&cv_zlib_memory);

View file

@ -742,7 +742,7 @@ void SV_FileSendTicker(void)
if (ram)
M_Memcpy(p->data, &f->id.ram[transfer[i].position], size);
else if (fread(p->data, 1, size, transfer[i].currentfile) != size)
I_Error("SV_FileSendTicker: can't read %s byte on %s at %d because %s", sizeu1(size), f->id.filename, transfer[i].position, strerror(ferror(transfer[i].currentfile)));
I_Error("SV_FileSendTicker: can't read %s byte on %s at %d because %s", sizeu1(size), f->id.filename, transfer[i].position, M_FileError(transfer[i].currentfile));
p->position = LONG(transfer[i].position);
// Put flag so receiver knows the total size
if (transfer[i].position + size == f->size)
@ -818,7 +818,7 @@ void Got_Filetxpak(void)
// We can receive packet in the wrong order, anyway all os support gaped file
fseek(file->file, pos, SEEK_SET);
if (fwrite(netbuffer->u.filetxpak.data,size,1,file->file) != 1)
I_Error("Can't write to %s: %s\n",filename, strerror(ferror(file->file)));
I_Error("Can't write to %s: %s\n",filename, M_FileError(file->file));
file->currentsize += size;
// Finished?

View file

@ -138,6 +138,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

View file

@ -868,7 +868,7 @@ boolean preparefilemenu(boolean samedepth, boolean replayhut)
}
else if (ext == EXT_TXT)
{
if (!strcmp(dent->d_name, "log.txt") || !strcmp(dent->d_name, "errorlog.txt"))
if (!strncmp(dent->d_name, "log-", 4) || !strcmp(dent->d_name, "errorlog.txt"))
ext |= EXT_LOADED;
}

View file

@ -16,6 +16,7 @@
#include "doomdef.h"
#include "command.h"
#include "m_argv.h"
#include "m_misc.h"
/** \brief number of arg
*/
@ -166,7 +167,7 @@ void M_FindResponseFile(void)
if (!file)
I_Error("No more free memory for the response file");
if (fread(file, size, 1, handle) != 1)
I_Error("Couldn't read response file because %s", strerror(ferror(handle)));
I_Error("Couldn't read response file because %s", M_FileError(handle));
fclose(handle);
// keep all the command line arguments following @responsefile

View file

@ -23,6 +23,8 @@
#include <unistd.h>
#endif
#include <errno.h>
// Extended map support.
#include <ctype.h>
@ -107,6 +109,9 @@ consvar_t cv_screenshot_folder = {"screenshot_folder", "", CV_SAVE, NULL, NULL,
static CV_PossibleValue_t moviemode_cons_t[] = {{MM_GIF, "GIF"}, {MM_APNG, "aPNG"}, {MM_SCREENSHOT, "Screenshots"}, {0, NULL}};
consvar_t cv_moviemode = {"moviemode_mode", "GIF", CV_SAVE|CV_CALL, moviemode_cons_t, Moviemode_mode_Onchange, 0, NULL, NULL, 0, 0, NULL};
consvar_t cv_movie_option = {"movie_option", "Default", CV_SAVE, screenshot_cons_t, NULL, 0, NULL, NULL, 0, 0, NULL};
consvar_t cv_movie_folder = {"movie_folder", "", CV_SAVE, NULL, NULL, 0, NULL, NULL, 0, 0, NULL};
static CV_PossibleValue_t zlib_mem_level_t[] = {
{1, "(Min Memory) 1"},
{2, "2"}, {3, "3"}, {4, "4"}, {5, "5"}, {6, "6"}, {7, "7"},
@ -1080,19 +1085,25 @@ static inline moviemode_t M_StartMovieGIF(const char *pathname)
void M_StartMovie(void)
{
#if NUMSCREENS > 2
const char *pathname = ".";
char pathname[MAX_WADPATH];
if (moviemode)
return;
if (cv_screenshot_option.value == 0)
pathname = usehome ? srb2home : srb2path;
else if (cv_screenshot_option.value == 1)
pathname = srb2home;
else if (cv_screenshot_option.value == 2)
pathname = srb2path;
else if (cv_screenshot_option.value == 3 && *cv_screenshot_folder.string != '\0')
pathname = cv_screenshot_folder.string;
if (cv_movie_option.value == 0)
strcpy(pathname, usehome ? srb2home : srb2path);
else if (cv_movie_option.value == 1)
strcpy(pathname, srb2home);
else if (cv_movie_option.value == 2)
strcpy(pathname, srb2path);
else if (cv_movie_option.value == 3 && *cv_movie_folder.string != '\0')
strcpy(pathname, cv_movie_folder.string);
if (cv_movie_option.value != 3)
{
strcat(pathname, PATHSEP"movies"PATHSEP);
I_mkdir(pathname, 0755);
}
if (rendermode == render_none)
I_Error("Can't make a movie without a render system\n");
@ -1424,7 +1435,8 @@ void M_ScreenShot(void)
void M_DoScreenShot(void)
{
#if NUMSCREENS > 2
const char *freename = NULL, *pathname = ".";
const char *freename = NULL;
char pathname[MAX_WADPATH];
boolean ret = false;
UINT8 *linear = NULL;
@ -1432,13 +1444,19 @@ void M_DoScreenShot(void)
takescreenshot = false;
if (cv_screenshot_option.value == 0)
pathname = usehome ? srb2home : srb2path;
strcpy(pathname, usehome ? srb2home : srb2path);
else if (cv_screenshot_option.value == 1)
pathname = srb2home;
strcpy(pathname, srb2home);
else if (cv_screenshot_option.value == 2)
pathname = srb2path;
strcpy(pathname, srb2path);
else if (cv_screenshot_option.value == 3 && *cv_screenshot_folder.string != '\0')
pathname = cv_screenshot_folder.string;
strcpy(pathname, cv_screenshot_folder.string);
if (cv_screenshot_option.value != 3)
{
strcat(pathname, PATHSEP"screenshots"PATHSEP);
I_mkdir(pathname, 0755);
}
#ifdef USE_PNG
if (rendermode != render_none)
@ -2355,3 +2373,104 @@ void M_SetupMemcpy(void)
M_Memcpy = cpu_cpy;
#endif
}
/** Return the appropriate message for a file error or end of file.
*/
const char *M_FileError(FILE *fp)
{
if (ferror(fp))
return strerror(errno);
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;
if (path == NULL)
return 0;
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

@ -30,7 +30,7 @@ typedef enum {
extern moviemode_t moviemode;
extern consvar_t cv_screenshot_option, cv_screenshot_folder;
extern consvar_t cv_moviemode;
extern consvar_t cv_moviemode, cv_movie_folder, cv_movie_option;
extern consvar_t cv_zlib_memory, cv_zlib_level, cv_zlib_strategy, cv_zlib_window_bits;
extern consvar_t cv_zlib_memorya, cv_zlib_levela, cv_zlib_strategya, cv_zlib_window_bitsa;
extern consvar_t cv_apng_delay;
@ -100,6 +100,13 @@ void strcatbf(char *s1, const char *s2, const char *s3);
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,19 @@
#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
#ifdef HAVE_TTF
@ -45,6 +52,7 @@ extern int SDL_main(int argc, char *argv[]);
#ifdef LOGMESSAGES
FILE *logstream = NULL;
char logfilename[1024];
#endif
#ifndef DOXYGEN
@ -125,16 +133,89 @@ int main(int argc, char **argv)
#endif
#endif
logdir = D_Home();
#ifdef LOGMESSAGES
if (!M_CheckParm("-nolog"))
{
time_t my_time;
struct tm * timeinfo;
const char *format;
const char *reldir;
int left;
boolean fileabs;
const char *link;
logdir = D_Home();
my_time = time(NULL);
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 (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)
logstream = fopen(va("%s/"DEFAULTDIR"/log.txt",logdir), "wt");
else
#endif
logstream = fopen("./log.txt", "wt");
#endif
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__*/
}
//I_OutputMsg("I_StartupSystem() ...\n");
I_StartupSystem();
@ -160,6 +241,10 @@ int main(int argc, char **argv)
// startup SRB2
CONS_Printf("Setting up SRB2Kart...\n");
D_SRB2Main();
#ifdef LOGMESSAGES
if (!M_CheckParm("-nolog"))
CONS_Printf("Logfile: %s\n", logfilename);
#endif
CONS_Printf("Entering main game loop...\n");
// never return
D_SRB2Loop();

View file

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

View file

@ -1435,7 +1435,7 @@ static boolean LoadSong(void *data, size_t lumplength, size_t selectpos)
if (fwrite(data, lumplength, 1, midfile) == 0)
{
CONS_Printf(M_GetText("Couldn't write music into file %s because %s\n"), tempname, strerror(ferror(midfile)));
CONS_Printf(M_GetText("Couldn't write music into file %s because %s\n"), tempname, M_FileError(midfile));
Z_Free(data);
fclose(midfile);
return false;

View file

@ -366,7 +366,7 @@ static lumpinfo_t* ResGetLumpsWad (FILE* handle, UINT16* nlmp, const char* filen
// read the header
if (fread(&header, 1, sizeof header, handle) < sizeof header)
{
CONS_Alert(CONS_ERROR, M_GetText("Can't read wad header because %s\n"), strerror(ferror(handle)));
CONS_Alert(CONS_ERROR, M_GetText("Can't read wad header because %s\n"), M_FileError(handle));
return NULL;
}
@ -389,7 +389,7 @@ static lumpinfo_t* ResGetLumpsWad (FILE* handle, UINT16* nlmp, const char* filen
if (fseek(handle, header.infotableofs, SEEK_SET) == -1
|| fread(fileinfo, 1, i, handle) < i)
{
CONS_Alert(CONS_ERROR, M_GetText("Corrupt wadfile directory (%s)\n"), strerror(ferror(handle)));
CONS_Alert(CONS_ERROR, M_GetText("Corrupt wadfile directory (%s)\n"), M_FileError(handle));
free(fileinfov);
return NULL;
}
@ -410,7 +410,7 @@ static lumpinfo_t* ResGetLumpsWad (FILE* handle, UINT16* nlmp, const char* filen
handle) < sizeof realsize)
{
I_Error("corrupt compressed file: %s; maybe %s", /// \todo Avoid the bailout?
filename, strerror(ferror(handle)));
filename, M_FileError(handle));
}
realsize = LONG(realsize);
if (realsize != 0)
@ -548,7 +548,7 @@ static lumpinfo_t* ResGetLumpsZip (FILE* handle, UINT16* nlmp)
fseek(handle, -4, SEEK_CUR);
if (fread(&zend, 1, sizeof zend, handle) < sizeof zend)
{
CONS_Alert(CONS_ERROR, "Corrupt central directory (%s)\n", strerror(ferror(handle)));
CONS_Alert(CONS_ERROR, "Corrupt central directory (%s)\n", M_FileError(handle));
return NULL;
}
numlumps = zend.entries;
@ -565,7 +565,7 @@ static lumpinfo_t* ResGetLumpsZip (FILE* handle, UINT16* nlmp)
if (fread(zentry, 1, sizeof(zentry_t), handle) < sizeof(zentry_t))
{
CONS_Alert(CONS_ERROR, "Failed to read central directory (%s)\n", strerror(ferror(handle)));
CONS_Alert(CONS_ERROR, "Failed to read central directory (%s)\n", M_FileError(handle));
Z_Free(lumpinfo);
free(zentry);
return NULL;
@ -585,7 +585,7 @@ static lumpinfo_t* ResGetLumpsZip (FILE* handle, UINT16* nlmp)
fullname = malloc(zentry->namelen + 1);
if (fgets(fullname, zentry->namelen + 1, handle) != fullname)
{
CONS_Alert(CONS_ERROR, "Unable to read lumpname (%s)\n", strerror(ferror(handle)));
CONS_Alert(CONS_ERROR, "Unable to read lumpname (%s)\n", M_FileError(handle));
Z_Free(lumpinfo);
free(zentry);
free(fullname);