]> git.pond.sub.org Git - empserver/blob - src/lib/subs/journal.c
client: Unbreak standalone build
[empserver] / src / lib / subs / journal.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2021, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
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.
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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
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.
24  *
25  *  ---
26  *
27  *  journal.c: Log a journal of events to a file
28  *
29  *  Known contributors to this file:
30  *     Markus Armbruster, 2004-2012
31  *     Ron Koenderink, 2008
32  */
33
34 /*
35  * Journal file format: each line logs an event, and looks like this:
36  *
37  *     TIME THREAD EVENT DATA
38  *
39  * Events and their data are:
40  *
41  *     startup
42  *     shutdown
43  *     prng NAME SEED
44  *     login CNUM HOSTADDR USER
45  *     logout CNUM
46  *     command NAME
47  *     input INPUT
48  *     output THREAD ID OUTPUT
49  *     update ETU
50  */
51
52 #include <config.h>
53
54 #include <ctype.h>
55 #include <errno.h>
56 #include <stdarg.h>
57 #include <stdio.h>
58 #include <time.h>
59 #include "misc.h"
60 #include "empthread.h"
61 #include "journal.h"
62 #include "optlist.h"
63 #include "player.h"
64 #include "prototypes.h"
65
66 static char journal_fname[] = "journal.log";
67 static FILE *journal;
68
69 static void journal_entry_start(char *fmt, ...)
70     ATTRIBUTE((format (printf, 1, 2)));
71 static void journal_entry(char *fmt, ...)
72     ATTRIBUTE((format (printf, 1, 2)));
73 static void journal_output_start(struct player *, int);
74
75 static FILE *
76 journal_open(void)
77 {
78     return fopen(journal_fname, "a");
79 }
80
81 static void
82 journal_entry_vstart(char *fmt, va_list ap)
83 {
84     time_t now;
85     empth_t *self;
86
87     if (!journal)
88         return;
89     time(&now);
90     self = empth_self();
91     fprintf(journal, "%.24s %10.10s ",
92             ctime(&now), self ? empth_name(self) : "Main");
93     vfprintf(journal, fmt, ap);
94 }
95
96 static void
97 journal_entry_start(char *fmt, ...)
98 {
99     va_list ap;
100
101     va_start(ap, fmt);
102     journal_entry_vstart(fmt, ap);
103     va_end(ap);
104 }
105
106 static void
107 journal_entry_write(char *s, size_t n)
108 {
109     char *p;
110
111     if (!journal)
112         return;
113     for (p = s; p < s + n; p++) {
114         if (*p == '\\')
115             fprintf(journal, "\\\\");
116         else if (isprint(*(unsigned char *)p) || *p == '\t')
117             putc(*p, journal);
118         else
119             fprintf(journal, "\\%03o", *p);
120     }
121 }
122
123 static void
124 journal_entry_end(int newline, int flush)
125 {
126     if (!journal)
127         return;
128     if (!newline)
129         fputc('\\', journal);
130     fputc('\n', journal);
131     if (flush)
132         fflush(journal);
133     if (ferror(journal)) {
134         logerror("Error writing journal (%s)", strerror(errno));
135         clearerr(journal);
136     }
137 }
138
139 static void
140 journal_entry(char *fmt, ...)
141 {
142     va_list ap;
143
144     va_start(ap, fmt);
145     journal_entry_vstart(fmt, ap);
146     va_end(ap);
147     journal_entry_end(1, 1);
148 }
149
150 int
151 journal_startup(void)
152 {
153     if (!keep_journal)
154         return 0;
155     journal = journal_open();
156     if (!journal) {
157         logerror("Can't open %s (%s)", journal_fname, strerror(errno));
158         return -1;
159     }
160     journal_entry("startup");
161     return 0;
162 }
163
164 void
165 journal_shutdown(void)
166 {
167     journal_entry("shutdown");
168     if (journal) {
169         fclose(journal);
170         journal = NULL;
171     }
172 }
173
174 int
175 journal_reopen(void)
176 {
177     FILE *j;
178
179     if (!keep_journal)
180         return 0;
181     j = journal_open();
182     if (!j) {
183         logerror("Can't open %s (%s)", journal_fname, strerror(errno));
184         return -1;
185     }
186     if (journal)
187         fclose(journal);
188     journal = j;
189     return 0;
190 }
191
192 void
193 journal_prng(unsigned seed)
194 {
195     journal_entry("prng MT19937 %u", seed);
196 }
197
198 void
199 journal_login(void)
200 {
201     journal_entry("login %d %s %s",
202                   player->cnum, player->hostaddr, player->userid);
203 }
204
205 void
206 journal_logout(void)
207 {
208     journal_entry("logout %d", player->cnum);
209 }
210
211 void
212 journal_output(struct player *pl, int id, char *output)
213 {
214     static char buf[1024];
215     static char *bp = buf;
216     static struct player *bpl;
217     static int bid;
218     char *s, *e;
219
220     if (keep_journal < 2)
221         return;
222
223     if (bp != buf && (pl != bpl || id != bid)) {
224         journal_output_start(bpl, bid);
225         journal_entry_write(buf, bp - buf);
226         journal_entry_end(0, 0);
227         bp = buf;
228     }
229
230     for (s = output; (e = strchr(s, '\n')); s = e + 1) {
231         journal_output_start(pl, id);
232         journal_entry_write(buf, bp - buf);
233         journal_entry_write(s, e - s);
234         journal_entry_end(1, 0);
235         bp = buf;
236     }
237     e = strchr(s, 0);
238     if (bp + (e - s) <= buf + sizeof(buf)) {
239         memcpy(bp, s, e - s);
240         bp += e - s;
241         bpl = pl;
242         bid = id;
243     } else {
244         journal_output_start(pl, id);
245         journal_entry_write(buf, bp - buf);
246         journal_entry_write(s, e - s);
247         journal_entry_end(0, 0);
248         bp = buf;
249     }
250 }
251
252 static void
253 journal_output_start(struct player *pl, int id)
254 {
255     journal_entry_start("output %s %d ", empth_name(pl->proc), id);
256 }
257
258 void
259 journal_input(char *input)
260 {
261     journal_entry_start("input ");
262     journal_entry_write(input, strlen(input));
263     journal_entry_end(1, 1);
264 }
265
266 void
267 journal_command(char *cmd)
268 {
269     char *eptr = strchr(cmd, ' ');
270     journal_entry("command %.*s", eptr ? (int)(eptr - cmd) : -1, cmd);
271 }
272
273 void
274 journal_update(int etu)
275 {
276     journal_entry("update %d", etu);
277 }