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
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 * journal.c: Log a journal of events to a file
29 * Known contributors to this file:
30 * Markus Armbruster, 2004-2012
31 * Ron Koenderink, 2008
35 * Journal file format: each line logs an event, and looks like this:
37 * TIME THREAD EVENT DATA
39 * Events and their data are:
44 * login CNUM HOSTADDR USER
48 * output THREAD ID OUTPUT
60 #include "empthread.h"
64 #include "prototypes.h"
66 static char journal_fname[] = "journal.log";
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);
78 return fopen(journal_fname, "a");
82 journal_entry_vstart(char *fmt, va_list ap)
91 fprintf(journal, "%.24s %10.10s ",
92 ctime(&now), self ? empth_name(self) : "Main");
93 vfprintf(journal, fmt, ap);
97 journal_entry_start(char *fmt, ...)
102 journal_entry_vstart(fmt, ap);
107 journal_entry_write(char *s, size_t n)
113 for (p = s; p < s + n; p++) {
115 fprintf(journal, "\\\\");
116 else if (isprint(*(unsigned char *)p) || *p == '\t')
119 fprintf(journal, "\\%03o", *p);
124 journal_entry_end(int newline, int flush)
129 fputc('\\', journal);
130 fputc('\n', journal);
133 if (ferror(journal)) {
134 logerror("Error writing journal (%s)", strerror(errno));
140 journal_entry(char *fmt, ...)
145 journal_entry_vstart(fmt, ap);
147 journal_entry_end(1, 1);
151 journal_startup(void)
155 journal = journal_open();
157 logerror("Can't open %s (%s)", journal_fname, strerror(errno));
160 journal_entry("startup");
165 journal_shutdown(void)
167 journal_entry("shutdown");
183 logerror("Can't open %s (%s)", journal_fname, strerror(errno));
193 journal_prng(unsigned seed)
195 journal_entry("prng MT19937 %u", seed);
201 journal_entry("login %d %s %s",
202 player->cnum, player->hostaddr, player->userid);
208 journal_entry("logout %d", player->cnum);
212 journal_output(struct player *pl, int id, char *output)
214 static char buf[1024];
215 static char *bp = buf;
216 static struct player *bpl;
220 if (keep_journal < 2)
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);
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);
238 if (bp + (e - s) <= buf + sizeof(buf)) {
239 memcpy(bp, s, e - s);
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);
253 journal_output_start(struct player *pl, int id)
255 journal_entry_start("output %s %d ", empth_name(pl->proc), id);
259 journal_input(char *input)
261 journal_entry_start("input ");
262 journal_entry_write(input, strlen(input));
263 journal_entry_end(1, 1);
267 journal_command(char *cmd)
269 char *eptr = strchr(cmd, ' ');
270 journal_entry("command %.*s", eptr ? (int)(eptr - cmd) : -1, cmd);
274 journal_update(int etu)
276 journal_entry("update %d", etu);