]> git.pond.sub.org Git - empserver/blob - src/lib/subs/journal.c
Fix journalling of output ids
[empserver] / src / lib / subs / journal.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2009, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  journal.c: Log a journal of events to a file
29  *
30  *  Known contributors to this file:
31  *     Markus Armbruster, 2004-2008
32  *     Ron Koenderink, 2008
33  */
34
35 /*
36  * Journal file format: each line logs an event, and looks like this:
37  *
38  *     TIME THREAD EVENT DATA
39  *
40  * Events and their data are:
41  *
42  *     startup
43  *     shutdown
44  *     prng NAME SEED
45  *     login CNUM HOSTADDR USER
46  *     logout CNUM
47  *     command NAME
48  *     input INPUT
49  *     output THREAD ID OUTPUT
50  *     update ETU
51  */
52
53 #include <config.h>
54
55 #include <ctype.h>
56 #include <errno.h>
57 #include <stdarg.h>
58 #include <stdio.h>
59 #include <time.h>
60 #include "misc.h"
61 #include "empthread.h"
62 #include "journal.h"
63 #include "optlist.h"
64 #include "player.h"
65 #include "prototypes.h"
66
67 static char journal_fname[] = "journal.log";
68 static FILE *journal;
69
70 static void journal_entry(char *fmt, ...)
71     ATTRIBUTE((format (printf, 1, 2)));
72 static void journal_output_1(struct player *, int, char *, char *, int);
73
74 static FILE *
75 journal_open(void)
76 {
77     return fopen(journal_fname, "a+");
78 }
79
80 static void
81 journal_entry(char *fmt, ...)
82 {
83     static char buf[1024];
84     va_list ap;
85     time_t now;
86     unsigned char *p;
87
88     if (journal) {
89         time(&now);
90         fprintf(journal, "%.24s %10.10s ",
91                 ctime(&now), empth_name(empth_self()));
92
93         va_start(ap, fmt);
94         vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
95         va_end(ap);
96
97         for (p = (unsigned char *)buf; *p; p++) {
98             if (isprint(*p))
99                 putc(*p, journal);
100             else
101                 fprintf(journal, "\\%03o", *p);
102         }
103         fputs("\n", journal);
104         if (fmt[0] != 'o')      /* FIXME disgusting hack */
105             fflush(journal);
106         if (ferror(journal)) {
107             logerror("Error writing journal (%s)", strerror(errno));
108             clearerr(journal);
109         }
110     }
111 }
112
113 int
114 journal_startup(void)
115 {
116     if (!keep_journal)
117         return 0;
118     journal = journal_open();
119     if (!journal) {
120         logerror("Can't open %s (%s)", journal_fname, strerror(errno));
121         return -1;
122     }
123     journal_entry("startup");
124     return 0;
125 }
126
127 void
128 journal_shutdown(void)
129 {
130     journal_entry("shutdown");
131     if (journal) {
132         fclose(journal);
133         journal = NULL;
134     }
135 }
136
137 int
138 journal_reopen(void)
139 {
140     FILE *j;
141
142     if (!keep_journal)
143         return 0;
144     j = journal_open();
145     if (!j) {
146         logerror("Can't open %s (%s)", journal_fname, strerror(errno));
147         return -1;
148     }
149     if (journal)
150         fclose(journal);
151     journal = j;
152     return 0;
153 }
154
155 void
156 journal_prng(unsigned seed)
157 {
158     journal_entry("prng BSD %d", seed);
159 }
160
161 void
162 journal_login(void)
163 {
164     journal_entry("login %d %s %s",
165                   player->cnum, player->hostaddr, player->userid);
166 }
167
168 void
169 journal_logout(void)
170 {
171     journal_entry("logout %d", player->cnum);
172 }
173
174 void
175 journal_output(struct player *pl, int id, char *output)
176 {
177     static char buf[1024];
178     static struct player *bpl;
179     static int bid;
180     char *s, *e;
181
182     if (keep_journal < 2)
183         return;
184
185     if (buf[0] && (pl != bpl && id != bid)) {
186         journal_output_1(bpl, bid, buf, "", -1);
187         buf[0] = 0;
188     }
189
190     for (s = output; (e = strchr(s, '\n')); s = e + 1) {
191         journal_output_1(pl, id, buf, s, (int)(e + 1 - s));
192         buf[0] = 0;
193     }
194     if (strlen(buf) + strlen(s) < 1024) {
195         strcpy(buf + strlen(buf), s);
196         bpl = pl;
197         bid = id;
198     } else {
199         journal_output_1(pl, id, buf, s, -1);
200         buf[0] = 0;
201     }
202 }
203
204 void
205 journal_output_1(struct player *pl, int id,
206                  char *buf1, char *buf2, int buf2prec)
207 {
208     if (pl && pl->state == PS_PLAYING)
209         journal_entry("output %d %d %s%.*s",
210                       pl->cnum, id, buf1, buf2prec, buf2);
211     else
212         journal_entry("output %p %id %s%.*s",
213                       pl, id, buf1, buf2prec, buf2);
214 }
215
216 void
217 journal_input(char *input)
218 {
219     journal_entry("input %s", input);
220 }
221
222 void
223 journal_command(char *cmd)
224 {
225     char *eptr = strchr(cmd, ' ');
226     journal_entry("command %.*s", eptr ? (int)(eptr - cmd) : -1, cmd);
227 }
228
229 void
230 journal_update(int etu)
231 {
232     journal_entry("update %d", etu);
233 }