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
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.
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.
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/>.
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.
27 * log.c: Log an Empire error to a file
29 * Known contributors to this file:
31 * Markus Armbruster, 2003-2009
46 #include "prototypes.h"
48 void (*oops_handler)(void) = abort;
50 static char logfile[32];
51 static int logfd = -1;
53 static int logopen(void);
56 * Points log file at PROGRAM.log
59 loginit(char *program)
61 sprintf(logfile, "%.*s.log", (int)sizeof(logfile) - 5, program);
71 fd = open(logfile, O_WRONLY | O_CREAT | O_APPEND,
72 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
74 logerror("Can't open %s (%s)", logfile, strerror(errno));
83 if ((newfd = logopen()) < 0)
88 logerror("Can't close %s (%s)", logfile, strerror(errno));
93 * Write a line to the log file and to stderr.
94 * Messages are silently truncated after 512 characters or a newline.
97 logerror(char *format, ...)
100 ctime_len = 24, /* output of ctime() less the newline */
101 msg_space = 512 /* space for formatted message */
105 char buf[ctime_len + 1 + msg_space + 2];
108 va_start(list, format);
109 msg = buf + ctime_len + 1;
110 vsnprintf(msg, msg_space, format, list);
111 buf[sizeof(buf)-2] = 0;
112 p = msg + strlen(msg);
115 p = strchr(msg, '\n');
120 memcpy(buf, ctime(&now), ctime_len);
121 buf[ctime_len] = ' ';
122 write(logfd, buf, strlen(buf));
128 * Log internal error MSG occured in FILE:LINE, call oops handler.
131 oops(char *msg, char *file, int line)
133 logerror("Oops: %s in %s:%d", msg ? msg : "bug", file, line);
138 * Report out-of-memory condition and terminate the program.
139 * Use this with restraint! Clean error recovery is preferable, but
140 * not always feasible (e.g. halfway through the update) or worthwhile
141 * (during server startup).
146 logerror("Memory exhausted");