]> git.pond.sub.org Git - empserver/blob - src/lib/gen/log.c
b32a996532e5819394aeb8079e8605320e2ad4d0
[empserver] / src / lib / gen / log.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  log.c: Log an Empire error to a file
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1989
31  *     Markus Armbruster, 2003-2010
32  */
33
34 #include <config.h>
35
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <stdarg.h>
39 #include <stdio.h>
40 #include <sys/stat.h>
41 #include <time.h>
42 #include <unistd.h>
43 #include "misc.h"
44 #include "prototypes.h"
45
46 void (*oops_handler)(void) = abort;
47
48 static char logfile[32];
49 static int logfd = -1;
50
51 static int logopen(void);
52
53 /*
54  * Points log file at PROGRAM.log
55  */
56 int
57 loginit(char *program)
58 {
59     sprintf(logfile, "%.*s.log", (int)sizeof(logfile) - 5, program);
60     logfd = logopen();
61     return logfd;
62 }
63
64 static int
65 logopen(void)
66 {
67     int fd;
68
69     fd = open(logfile, O_WRONLY | O_CREAT | O_APPEND,
70               S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
71     if (fd < 0)
72         logerror("Can't open %s (%s)", logfile, strerror(errno));
73     return fd;
74 }
75
76 int
77 logreopen(void)
78 {
79     int newfd, res;
80
81     if ((newfd = logopen()) < 0)
82         return -1;
83     res = close(logfd);
84     logfd = newfd;
85     if (res < 0)
86         logerror("Can't close %s (%s)", logfile, strerror(errno));
87     return res;
88 }
89
90 /*
91  * Write a line to the log file and to stderr.
92  * Messages are silently truncated after 512 characters or a newline.
93  */
94 void
95 logerror(char *format, ...)
96 {
97     enum {
98         ctime_len = 24,         /* output of ctime() less the newline */
99         msg_space = 512         /* space for formatted message */
100     };
101     va_list list;
102     time_t now;
103     char buf[ctime_len + 1 + msg_space + 2];
104     char *msg, *p;
105
106     va_start(list, format);
107     msg = buf + ctime_len + 1;
108     vsnprintf(msg, msg_space, format, list);
109     buf[sizeof(buf)-2] = 0;
110     p = msg + strlen(msg);
111     p[0] = '\n';
112     p[1] = 0;
113     p = strchr(msg, '\n');
114     p[1] = 0;
115     fputs(msg, stderr);
116     if (logfd >= 0) {
117         time(&now);
118         memcpy(buf, ctime(&now), ctime_len);
119         buf[ctime_len] = ' ';
120         write(logfd, buf, strlen(buf));
121     }
122     va_end(list);
123 }
124
125 /*
126  * Log internal error MSG occured in FILE:LINE, call oops handler.
127  */
128 void
129 oops(char *msg, char *file, int line)
130 {
131     logerror("Oops: %s in %s:%d", msg ? msg : "bug", file, line);
132     oops_handler();
133 }
134
135 /*
136  * Report out-of-memory condition and terminate the program.
137  * Use this with restraint!  Clean error recovery is preferable, but
138  * not always feasible (e.g. halfway through the update) or worthwhile
139  * (during server startup).
140  */
141 void
142 exit_nomem(void)
143 {
144     logerror("Memory exhausted");
145     exit(1);
146 }