Keep log open, rotate it just like the journal:

(logfd, logopen): New.
(loginit): Set logfd, return success.
(logerror): Use logfd.
(logreopen): New.
(relo, main): Use it.
This commit is contained in:
Markus Armbruster 2007-10-27 15:49:45 +00:00
parent 88b44bffd3
commit 9c94a23709
4 changed files with 45 additions and 11 deletions

View file

@ -305,7 +305,8 @@ extern int has_units_with_mob(coord, coord, natid);
extern int adj_units(coord, coord, natid); extern int adj_units(coord, coord, natid);
extern int has_helpful_engineer(coord x, coord y, natid cn); extern int has_helpful_engineer(coord x, coord y, natid cn);
/* log.c */ /* log.c */
extern void loginit(char *); extern int loginit(char *);
extern int logreopen(void);
extern void logerror(char *, ...) ATTRIBUTE((format (printf, 1, 2))); extern void logerror(char *, ...) ATTRIBUTE((format (printf, 1, 2)));
/* more in misc.h */ /* more in misc.h */
/* maps.c */ /* maps.c */

View file

@ -54,5 +54,11 @@ relo(void)
update_reschedule(); update_reschedule();
pr("Reload of update schedule requested.\n"); 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; return RET_OK;
} }

View file

@ -33,12 +33,13 @@
#include <config.h> #include <config.h>
#include <unistd.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/stat.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <sys/stat.h>
#include <time.h> #include <time.h>
#include <unistd.h>
#include "misc.h" #include "misc.h"
#include "optlist.h" #include "optlist.h"
#include "player.h" #include "player.h"
@ -48,14 +49,44 @@
int debug = 0; int debug = 0;
static char logfile[32]; static char logfile[32];
static int logfd = -1;
static int logopen(void);
/* /*
* Points log file at PROGRAM.log * Points log file at PROGRAM.log
*/ */
void int
loginit(char *program) loginit(char *program)
{ {
sprintf(logfile, "%.*s.log", (int)sizeof(logfile) - 5, 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; va_list list;
time_t now; time_t now;
char buf[ctime_len + 1 + msg_space + 2]; char buf[ctime_len + 1 + msg_space + 2];
int logf;
char *msg, *p; char *msg, *p;
va_start(list, format); va_start(list, format);
@ -85,15 +115,11 @@ logerror(char *format, ...)
p = strchr(msg, '\n'); p = strchr(msg, '\n');
p[1] = 0; p[1] = 0;
fputs(msg, stderr); fputs(msg, stderr);
if (logfile[0] != '\0') { if (logfd >= 0) {
time(&now); time(&now);
memcpy(buf, ctime(&now), ctime_len); memcpy(buf, ctime(&now), ctime_len);
buf[ctime_len] = ' '; buf[ctime_len] = ' ';
if ((logf = open(logfile, O_WRONLY | O_CREAT | O_APPEND, write(logfd, buf, strlen(buf));
S_IRWUG)) < 0)
return;
write(logf, buf, strlen(buf));
close(logf);
} }
va_end(list); va_end(list);
} }

View file

@ -283,6 +283,7 @@ main(int argc, char **argv)
/* if you make changes here, also update relo() */ /* if you make changes here, also update relo() */
journal_reopen(); journal_reopen();
update_reschedule(); update_reschedule();
logreopen();
continue; continue;
} }
#endif #endif