]> git.pond.sub.org Git - empserver/blob - src/lib/common/log.c
Simple POSIX I/O emulation layer to work around Windows's defective
[empserver] / src / lib / common / log.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, 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  */
33
34 #include <config.h>
35
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <sys/stat.h>
39 #include <stdarg.h>
40 #include <stdio.h>
41 #include <time.h>
42 #include "misc.h"
43 #include "optlist.h"
44 #include "player.h"
45 #include "prototypes.h"
46
47 /* Debugging?  If yes call abort() on internal error.  */
48 int debug = 0;
49
50 static char logfile[32];
51
52 /*
53  * Points log file at PROGRAM.log
54  */
55 void
56 loginit(char *program)
57 {
58     sprintf(logfile, "%.*s.log", (int)sizeof(logfile) - 5, program);
59 }
60
61 /*
62  * Write a line to the log file and to stderr.
63  * Messages are silently truncated after 512 characters or a newline.
64  */
65 void
66 logerror(char *format, ...)
67 {
68     enum {
69         ctime_len = 24,         /* output of ctime() less the newline */
70         msg_space = 512         /* space for formatted message */
71     };
72     va_list list;
73     time_t now;
74     char buf[ctime_len + 1 + msg_space + 2];
75     int logf;
76     char *msg, *p;
77
78     va_start(list, format);
79     msg = buf + ctime_len + 1;
80     vsnprintf(msg, msg_space, format, list);
81     buf[sizeof(buf)-2] = 0;
82     p = msg + strlen(msg);
83     p[0] = '\n';
84     p[1] = 0;
85     p = strchr(msg, '\n');
86     p[1] = 0;
87     fputs(msg, stderr);
88     if (logfile[0] != '\0') {
89         time(&now);
90         memcpy(buf, ctime(&now), ctime_len);
91         buf[ctime_len] = ' ';
92         if ((logf = open(logfile, O_WRONLY | O_CREAT | O_APPEND,
93                          S_IRWUG)) < 0)
94             return;
95         write(logf, buf, strlen(buf));
96         close(logf);
97     }
98     va_end(list);
99 }
100
101 /*
102  * Log internal error MSG occured in FILE:LINE.
103  * If debugging, call abort(), else return 1.
104  */
105 int
106 oops(char *msg, char *file, int line)
107 {
108     logerror("Oops: %s in %s:%d", msg ? msg : "bug", file, line);
109     if (debug) abort();
110     return 1;
111 }
112
113 /*
114  * Report out-of-memory condition and terminate the program.
115  * Use this with restraint!  Clean error recovery is preferable, but
116  * not always feasible (e.g. halfway through the update) or worthwhile
117  * (during server startup).
118  */
119 void
120 exit_nomem(void)
121 {
122     logerror("Memory exhausted");
123     exit(1);
124 }