Merge pull request #588 from FullName/LoggingPreInitStoring

Allow loginit() to be delayed, store loglog() data in intermediate buffer...
This commit is contained in:
irungentoo 2013-09-18 15:44:25 -07:00
commit e2e2719439
2 changed files with 51 additions and 7 deletions

View File

@ -352,10 +352,6 @@ Networking_Core *new_networking(IP ip, uint16_t port)
fcntl(temp->sock, F_SETFL, O_NONBLOCK, 1); fcntl(temp->sock, F_SETFL, O_NONBLOCK, 1);
#endif #endif
#ifdef LOGGING
loginit(ntohs(port));
#endif
/* Bind our socket to port PORT and the given IP address (usually 0.0.0.0 or ::) */ /* Bind our socket to port PORT and the given IP address (usually 0.0.0.0 or ::) */
uint16_t *portptr = NULL; uint16_t *portptr = NULL;
struct sockaddr_storage addr; struct sockaddr_storage addr;
@ -461,6 +457,8 @@ Networking_Core *new_networking(IP ip, uint16_t port)
if (!res) { if (!res) {
temp->port = *portptr; temp->port = *portptr;
#ifdef LOGGING #ifdef LOGGING
loginit(temp->port);
sprintf(logbuffer, "Bound successfully to %s:%u.\n", ip_ntoa(&ip), ntohs(temp->port)); sprintf(logbuffer, "Bound successfully to %s:%u.\n", ip_ntoa(&ip), ntohs(temp->port));
loglog(logbuffer); loglog(logbuffer);
#endif #endif

View File

@ -94,6 +94,9 @@ int load_state(load_state_callback_func load_state_callback, void *outer,
#ifdef LOGGING #ifdef LOGGING
time_t starttime = 0; time_t starttime = 0;
size_t logbufferprelen = 0;
char *logbufferpredata = NULL;
char *logbufferprehead = NULL;
char logbuffer[512]; char logbuffer[512];
static FILE *logfile = NULL; static FILE *logfile = NULL;
void loginit(uint16_t port) void loginit(uint16_t port)
@ -101,9 +104,23 @@ void loginit(uint16_t port)
if (logfile) if (logfile)
fclose(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"); logfile = fopen(logbuffer, "w");
starttime = now(); if (logbufferpredata) {
if (logfile)
fprintf(logfile, logbufferpredata);
free(logbufferpredata);
logbufferpredata = NULL;
}
}; };
void loglog(char *text) void loglog(char *text)
{ {
@ -111,8 +128,37 @@ void loglog(char *text)
fprintf(logfile, "%4u ", (uint32_t)(now() - starttime)); fprintf(logfile, "%4u ", (uint32_t)(now() - starttime));
fprintf(logfile, text); fprintf(logfile, text);
fflush(logfile); 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() void logexit()
{ {
if (logfile) { if (logfile) {