From fabfb73c8379d9e681463767247b6a1ba8dc90c5 Mon Sep 17 00:00:00 2001 From: ac-stef Date: Mon, 22 Sep 2014 00:24:09 +0200 Subject: [PATCH] added mandatory client log file - a client will now write all os console messages to "clientlog.txt" - at every game start, the log file gets rotated (with five old logs kept) - commandline parameter "-N" can be used to limit the number of lines (after the lines are logged, the file is closed) --- .gitignore | 1 + source/src/console.cpp | 8 ++++++++ source/src/main.cpp | 27 +++++++++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/.gitignore b/.gitignore index a6cbd554..f6336723 100644 --- a/.gitignore +++ b/.gitignore @@ -20,4 +20,5 @@ source/src/ac_client source/src/ac_server source/src/cube.h.gch source/src/Makefile_local +clientlog*.txt diff --git a/source/src/console.cpp b/source/src/console.cpp index 62d4adb3..ea8cd2e0 100644 --- a/source/src/console.cpp +++ b/source/src/console.cpp @@ -94,6 +94,9 @@ COMMANDN(toggleconsole, toggleconsole, ""); void renderconsole() { con.render(); } +stream *clientlogfile = NULL; +int clientloglinesremaining = INT_MAX; + void clientlogf(const char *s, ...) { defvformatstring(sp, s, s); @@ -105,6 +108,11 @@ void clientlogf(const char *s, ...) { // break into single lines first if((p = strchr(l, '\n'))) *p = '\0'; printf("%s%s\n", ts, l); + if(clientlogfile) + { + clientlogfile->printf("%s%s\n", ts, l); + if(--clientloglinesremaining <= 0) DELETEP(clientlogfile); + } l = p + 1; } while(p); diff --git a/source/src/main.cpp b/source/src/main.cpp index d8185b8b..9c69791d 100644 --- a/source/src/main.cpp +++ b/source/src/main.cpp @@ -41,6 +41,8 @@ void quit() // normal exit writeallxmaps(); cleanup(NULL); popscontext(); + extern stream *clientlogfile; + DELETEP(clientlogfile); exit(EXIT_SUCCESS); } @@ -988,6 +990,24 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw) } #endif +void initclientlog() // rotate old logfiles and create new one +{ + extern stream *clientlogfile; + const int MAXROT = 5; // keep five old logfiles + string fname1, fname2 = ""; + const char *basename = findfile("clientlog", "w"); + for(int i = MAXROT; i >= 0; i--) + { + formatstring(fname1)(i > 0 ? "%s-old-%d.txt" : "%s.txt", basename, i); + if(fname2[0]) rename(fname1, fname2); + else remove(fname1); + copystring(fname2, fname1); + } + clientlogfile = openfile(fname2, "w"); + if(clientlogfile) clientlogfile->printf("######## start logging: %s\n", timestring(true)); + else conoutf("could not create logfile \"%s\"", fname2); +} + VARP(compatibilitymode, 0, 1, 1); // FIXME : find a better place to put this ? int main(int argc, char **argv) @@ -1010,6 +1030,8 @@ int main(int argc, char **argv) string servername, password; int serverport; + initclientlog(); + const char *initmap = rndmapname(); pushscontext(IEXC_CFG); @@ -1262,6 +1284,11 @@ int main(int argc, char **argv) #ifdef _DEBUG int lastflush = 0; #endif + if(scl.logident[0]) + { // "-Nxxx" sets the number of client log lines to xxx (after init) + extern int clientloglinesremaining; + clientloglinesremaining = atoi(scl.logident); // dual-use for scl.logident + } for(;;) { static int frames = 0;