]> git.pond.sub.org Git - empserver/blob - src/lib/common/log.c
22184d23ccfb87c72f45afea5c11954e4059b781
[empserver] / src / lib / common / log.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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 #if defined(_WIN32) && !defined(__GNUC__)
37 #include <io.h>
38 #endif
39 #if !defined(_WIN32)
40 #include <unistd.h>
41 #endif
42 #include <fcntl.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <time.h>
46 #include "misc.h"
47 #include "optlist.h"
48 #include "player.h"
49 #include "prototypes.h"
50
51 /* Debugging?  If yes call abort() on internal error.  */
52 int debug = 0;
53
54 static char logfile[32];
55
56 /*
57  * Points log file at PROGRAM.log
58  */
59 void
60 loginit(char *program)
61 {
62     sprintf(logfile, "%.*s.log", (int)sizeof(logfile) - 5, program);
63 }
64
65 /*
66  * Write a line to the log file and to stderr.
67  * Messages are silently truncated after 512 characters or a newline.
68  */
69 void
70 logerror(char *format, ...)
71 {
72     enum {
73         ctime_len = 24,         /* output of ctime() less the newline */
74         msg_space = 512         /* space for formatted message */
75     };
76     va_list list;
77     time_t now;
78     char buf[ctime_len + 1 + msg_space + 2];
79     int logf;
80     char *msg, *p;
81
82     va_start(list, format);
83     msg = buf + ctime_len + 1;
84     vsnprintf(msg, msg_space, format, list);
85     buf[sizeof(buf)-2] = 0;
86     p = msg + strlen(msg);
87     p[0] = '\n';
88     p[1] = 0;
89     p = strchr(msg, '\n');
90     p[1] = 0;
91     fputs(msg, stderr);
92     time(&now);
93     memcpy(buf, ctime(&now), ctime_len);
94     buf[ctime_len] = ' ';
95     if ((logf = open(logfile, O_WRONLY | O_CREAT | O_APPEND, 0666)) < 0)
96         return;
97     write(logf, buf, strlen(buf));
98     close(logf);
99     va_end(list);
100 }
101
102 /*
103  * Log internal error MSG occured in FILE:LINE.
104  * If debugging, call abort(), else return 1.
105  */
106 int
107 oops(char *msg, char *file, int line)
108 {
109   logerror("Oops: %s in %s:%d", msg ? msg : "bug", file, line);
110   if (debug) abort();
111   return 1;
112 }