]> git.pond.sub.org Git - empserver/commitdiff
Keep log open, rotate it just like the journal:
authorMarkus Armbruster <armbru@pond.sub.org>
Sat, 27 Oct 2007 15:49:45 +0000 (15:49 +0000)
committerMarkus Armbruster <armbru@pond.sub.org>
Sat, 27 Oct 2007 15:49:45 +0000 (15:49 +0000)
(logfd, logopen): New.
(loginit): Set logfd, return success.
(logerror): Use logfd.
(logreopen): New.
(relo, main): Use it.

include/prototypes.h
src/lib/commands/relo.c
src/lib/common/log.c
src/server/main.c

index 476da5966b54edc0a808db783d4ea8a05691b41e..77d4f9a0819adbee5835189e08cbe68e9cc90500 100644 (file)
@@ -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 */
index 4a21ba43013cf0cc2335b84e4b3608a786bb85df..4ea292169bcd5ebd291977f506072ed00e3e336b 100644 (file)
@@ -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;
 }
index 50cf3d2472b108d2ddaaf817507395870ddbd987..67ac4a9f0c8344040b5a3c49edb3361b8f595bdc 100644 (file)
 
 #include <config.h>
 
-#include <unistd.h>
+#include <errno.h>
 #include <fcntl.h>
-#include <sys/stat.h>
 #include <stdarg.h>
 #include <stdio.h>
+#include <sys/stat.h>
 #include <time.h>
+#include <unistd.h>
 #include "misc.h"
 #include "optlist.h"
 #include "player.h"
 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);
 }
index 89a74491d43f0f9f202b95cff136ff641e422841..82de3dd6b5a3d441f29a190578dda6dd11ca9cd9 100644 (file)
@@ -283,6 +283,7 @@ main(int argc, char **argv)
            /* if you make changes here, also update relo() */
            journal_reopen();
            update_reschedule();
+           logreopen();
            continue;
        }
 #endif