diff --git a/include/prototypes.h b/include/prototypes.h index 476da596..77d4f9a0 100644 --- a/include/prototypes.h +++ b/include/prototypes.h @@ -305,7 +305,8 @@ extern int has_units_with_mob(coord, coord, natid); extern int adj_units(coord, coord, natid); extern int has_helpful_engineer(coord x, coord y, natid cn); /* log.c */ -extern void loginit(char *); +extern int loginit(char *); +extern int logreopen(void); extern void logerror(char *, ...) ATTRIBUTE((format (printf, 1, 2))); /* more in misc.h */ /* maps.c */ diff --git a/src/lib/commands/relo.c b/src/lib/commands/relo.c index 4a21ba43..4ea29216 100644 --- a/src/lib/commands/relo.c +++ b/src/lib/commands/relo.c @@ -54,5 +54,11 @@ relo(void) update_reschedule(); pr("Reload of update schedule requested.\n"); + if (logreopen() < 0) { + pr("Can't reopen log"); + return RET_SYS; + } + pr("Log reopened.\n"); + return RET_OK; } diff --git a/src/lib/common/log.c b/src/lib/common/log.c index 50cf3d24..67ac4a9f 100644 --- a/src/lib/common/log.c +++ b/src/lib/common/log.c @@ -33,12 +33,13 @@ #include -#include +#include #include -#include #include #include +#include #include +#include #include "misc.h" #include "optlist.h" #include "player.h" @@ -48,14 +49,44 @@ int debug = 0; static char logfile[32]; +static int logfd = -1; + +static int logopen(void); /* * Points log file at PROGRAM.log */ -void +int loginit(char *program) { sprintf(logfile, "%.*s.log", (int)sizeof(logfile) - 5, program); + logfd = logopen(); + return logfd; +} + +static int +logopen(void) +{ + int fd; + + fd = open(logfile, O_WRONLY | O_CREAT | O_APPEND, S_IRWUG); + if (fd < 0) + logerror("Can't open %s (%s)", logfile, strerror(errno)); + return fd; +} + +int +logreopen(void) +{ + int newfd, res; + + if ((newfd = logopen()) < 0) + return -1; + res = close(logfd); + logfd = newfd; + if (res < 0) + logerror("Can't close %s (%s)", logfile, strerror(errno)); + return res; } /* @@ -72,7 +103,6 @@ logerror(char *format, ...) va_list list; time_t now; char buf[ctime_len + 1 + msg_space + 2]; - int logf; char *msg, *p; va_start(list, format); @@ -85,15 +115,11 @@ logerror(char *format, ...) p = strchr(msg, '\n'); p[1] = 0; fputs(msg, stderr); - if (logfile[0] != '\0') { + if (logfd >= 0) { time(&now); memcpy(buf, ctime(&now), ctime_len); buf[ctime_len] = ' '; - if ((logf = open(logfile, O_WRONLY | O_CREAT | O_APPEND, - S_IRWUG)) < 0) - return; - write(logf, buf, strlen(buf)); - close(logf); + write(logfd, buf, strlen(buf)); } va_end(list); } diff --git a/src/server/main.c b/src/server/main.c index 89a74491..82de3dd6 100644 --- a/src/server/main.c +++ b/src/server/main.c @@ -283,6 +283,7 @@ main(int argc, char **argv) /* if you make changes here, also update relo() */ journal_reopen(); update_reschedule(); + logreopen(); continue; } #endif