From 88e80dc88f87897f8cf4c4839b1225e899310067 Mon Sep 17 00:00:00 2001 From: "Coren[m]" Date: Wed, 18 Sep 2013 16:25:55 +0200 Subject: [PATCH] Allow loginit() to be delayed, store loglog() data in intermediate buffer and flush it out when loginit() is called util.c: - handle loglog() before loginit() by storing the lines into an expanding buffer - when loginit() is called, write out and kill the buffer network.c: - push loginit() to the point where we know the actually used port --- toxcore/network.c | 6 ++---- toxcore/util.c | 52 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/toxcore/network.c b/toxcore/network.c index a44ef4c4..e04d2608 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -352,10 +352,6 @@ Networking_Core *new_networking(IP ip, uint16_t port) fcntl(temp->sock, F_SETFL, O_NONBLOCK, 1); #endif -#ifdef LOGGING - loginit(ntohs(port)); -#endif - /* Bind our socket to port PORT and the given IP address (usually 0.0.0.0 or ::) */ uint16_t *portptr = NULL; struct sockaddr_storage addr; @@ -461,6 +457,8 @@ Networking_Core *new_networking(IP ip, uint16_t port) if (!res) { temp->port = *portptr; #ifdef LOGGING + loginit(temp->port); + sprintf(logbuffer, "Bound successfully to %s:%u.\n", ip_ntoa(&ip), ntohs(temp->port)); loglog(logbuffer); #endif diff --git a/toxcore/util.c b/toxcore/util.c index b3263d05..7b2735a0 100644 --- a/toxcore/util.c +++ b/toxcore/util.c @@ -94,6 +94,9 @@ int load_state(load_state_callback_func load_state_callback, void *outer, #ifdef LOGGING time_t starttime = 0; +size_t logbufferprelen = 0; +char *logbufferpredata = NULL; +char *logbufferprehead = NULL; char logbuffer[512]; static FILE *logfile = NULL; void loginit(uint16_t port) @@ -101,9 +104,23 @@ void loginit(uint16_t port) if (logfile) fclose(logfile); - sprintf(logbuffer, "%u-%u.log", ntohs(port), (uint32_t)now()); + if (!starttime) + starttime = now(); + + struct tm *tm = localtime(&starttime); + if (strftime(logbuffer + 32, sizeof(logbuffer) - 32, "%F %T", tm)) + sprintf(logbuffer, "%u-%s.log", ntohs(port), logbuffer + 32); + else + sprintf(logbuffer, "%u-%lu.log", ntohs(port), starttime); logfile = fopen(logbuffer, "w"); - starttime = now(); + if (logbufferpredata) { + if (logfile) + fprintf(logfile, logbufferpredata); + + free(logbufferpredata); + logbufferpredata = NULL; + } + }; void loglog(char *text) { @@ -111,8 +128,37 @@ void loglog(char *text) fprintf(logfile, "%4u ", (uint32_t)(now() - starttime)); fprintf(logfile, text); fflush(logfile); + + return; } -}; + + /* log messages before file was opened: store */ + + size_t len = strlen(text); + if (!starttime) { + starttime = now(); + logbufferprelen = 1024 + len - (len % 1024); + logbufferpredata = malloc(logbufferprelen); + logbufferprehead = logbufferpredata; + } + + /* loginit() called meanwhile? (but failed to open) */ + if (!logbufferpredata) + return; + + if (len + logbufferprehead - logbufferpredata + 16U < logbufferprelen) { + size_t logpos = logbufferprehead - logbufferpredata; + size_t lennew = logbufferprelen * 1.4; + logbufferpredata = realloc(logbufferpredata, lennew); + logbufferprehead = logbufferpredata + logpos; + logbufferprelen = lennew; + } + + size_t written; + sprintf(logbufferprehead, "%4u %s%n", (uint32_t)(now() - starttime), text, &written); + logbufferprehead += written; +} + void logexit() { if (logfile) {