From 3bbe1f9555921ef9ef8217066cd7bdc12cf1dcf5 Mon Sep 17 00:00:00 2001 From: ac-stef Date: Mon, 6 Oct 2014 12:43:32 +0200 Subject: [PATCH] fix mandatory client logfile The logfile can't be created before the commandline arguments are parsed, because before that, the home directory is unknown. This patch logs into a memory buffer which is then copied to the logfile as soon as the logfile is created after processing the commandline arguments. The memory buffer is created during the initialisation of global variables, and destroyed after the logfile is created. --- source/src/console.cpp | 2 ++ source/src/main.cpp | 13 +++++++++---- source/src/protos.h | 3 +++ source/src/tools.cpp | 8 ++++++++ source/src/tools.h | 1 + 5 files changed, 23 insertions(+), 4 deletions(-) diff --git a/source/src/console.cpp b/source/src/console.cpp index ea8cd2e0..31c64ae3 100644 --- a/source/src/console.cpp +++ b/source/src/console.cpp @@ -95,6 +95,7 @@ COMMANDN(toggleconsole, toggleconsole, ""); void renderconsole() { con.render(); } stream *clientlogfile = NULL; +vector *bootclientlog = new vector; int clientloglinesremaining = INT_MAX; void clientlogf(const char *s, ...) @@ -113,6 +114,7 @@ void clientlogf(const char *s, ...) clientlogfile->printf("%s%s\n", ts, l); if(--clientloglinesremaining <= 0) DELETEP(clientlogfile); } + else if(bootclientlog) cvecprintf(*bootclientlog, "%s%s\n", ts, l); l = p + 1; } while(p); diff --git a/source/src/main.cpp b/source/src/main.cpp index 9c69791d..d016f151 100644 --- a/source/src/main.cpp +++ b/source/src/main.cpp @@ -992,7 +992,6 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw) 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"); @@ -1003,9 +1002,13 @@ void initclientlog() // rotate old logfiles and create new one else remove(fname1); copystring(fname2, fname1); } - clientlogfile = openfile(fname2, "w"); - if(clientlogfile) clientlogfile->printf("######## start logging: %s\n", timestring(true)); + clientlogfile = openfile("clientlog.txt", "w"); + if(clientlogfile) + { + if(bootclientlog && bootclientlog->length()) clientlogfile->write(bootclientlog->getbuf(), bootclientlog->length()); + } else conoutf("could not create logfile \"%s\"", fname2); + DELETEP(bootclientlog); } VARP(compatibilitymode, 0, 1, 1); // FIXME : find a better place to put this ? @@ -1030,7 +1033,7 @@ int main(int argc, char **argv) string servername, password; int serverport; - initclientlog(); + if(bootclientlog) cvecprintf(*bootclientlog, "######## start logging: %s\n", timestring(true)); const char *initmap = rndmapname(); @@ -1041,6 +1044,7 @@ int main(int argc, char **argv) initing = INIT_RESET; for(int i = 1; i *bootclientlog; + extern void keypress(int code, bool isdown, int cooked, SDLMod mod = KMOD_NONE); extern int rendercommand(int x, int y, int w); extern void renderconsole(); diff --git a/source/src/tools.cpp b/source/src/tools.cpp index 4ac00657..f5adec60 100644 --- a/source/src/tools.cpp +++ b/source/src/tools.cpp @@ -403,6 +403,14 @@ char *concatformatstring(char *d, const char *s, ...) return concatstring(d, temp); } +int cvecprintf(vector &v, const char *s, ...) +{ + defvformatstring(temp, s, s); + int len = strlen(temp); + if(len) v.put(temp, len); + return len; +} + const char *hiddenpwd(const char *pwd, int showchars) { static int sc = 3; diff --git a/source/src/tools.h b/source/src/tools.h index 35de88bf..75c086fb 100644 --- a/source/src/tools.h +++ b/source/src/tools.h @@ -968,6 +968,7 @@ extern const char *iptoa(const enet_uint32 ip); extern const char *iprtoa(const struct iprange &ipr); extern int cmpiprange(const struct iprange *a, const struct iprange *b); extern int cmpipmatch(const struct iprange *a, const struct iprange *b); +extern int cvecprintf(vector &v, const char *s, ...); extern const char *hiddenpwd(const char *pwd, int showchars = 0); #if defined(WIN32) && !defined(_DEBUG) && !defined(__GNUC__)