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