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
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.
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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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.
28 * journal.c: Log a journal of events to a file
30 * Known contributors to this file:
31 * Markus Armbruster, 2004-2008
32 * Ron Koenderink, 2008
36 * Journal file format: each line logs an event, and looks like this:
38 * TIME THREAD EVENT DATA
40 * Events and their data are:
45 * login CNUM HOSTADDR USER
49 * output THREAD ID OUTPUT
61 #include "empthread.h"
65 #include "prototypes.h"
67 static char journal_fname[] = "journal.log";
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);
77 return fopen(journal_fname, "a+");
81 journal_entry(char *fmt, ...)
83 static char buf[1024];
90 fprintf(journal, "%.24s %10.10s ",
91 ctime(&now), empth_name(empth_self()));
94 vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
97 for (p = (unsigned char *)buf; *p; p++) {
101 fprintf(journal, "\\%03o", *p);
103 fputs("\n", journal);
104 if (fmt[0] != 'o') /* FIXME disgusting hack */
106 if (ferror(journal)) {
107 logerror("Error writing journal (%s)", strerror(errno));
114 journal_startup(void)
118 journal = journal_open();
120 logerror("Can't open %s (%s)", journal_fname, strerror(errno));
123 journal_entry("startup");
128 journal_shutdown(void)
130 journal_entry("shutdown");
146 logerror("Can't open %s (%s)", journal_fname, strerror(errno));
156 journal_prng(unsigned seed)
158 journal_entry("prng BSD %d", seed);
164 journal_entry("login %d %s %s",
165 player->cnum, player->hostaddr, player->userid);
171 journal_entry("logout %d", player->cnum);
175 journal_output(struct player *pl, int id, char *output)
177 static char buf[1024];
178 static struct player *bpl;
182 if (keep_journal < 2)
185 if (buf[0] && (pl != bpl && id != bid)) {
186 journal_output_1(bpl, bid, buf, "", -1);
190 for (s = output; (e = strchr(s, '\n')); s = e + 1) {
191 journal_output_1(pl, id, buf, s, (int)(e + 1 - s));
194 if (strlen(buf) + strlen(s) < 1024) {
195 strcpy(buf + strlen(buf), s);
199 journal_output_1(pl, id, buf, s, -1);
205 journal_output_1(struct player *pl, int id,
206 char *buf1, char *buf2, int buf2prec)
208 if (pl && pl->state == PS_PLAYING)
209 journal_entry("output %d %d %s%.*s",
210 pl->cnum, id, buf1, buf2prec, buf2);
212 journal_entry("output %p %id %s%.*s",
213 pl, id, buf1, buf2prec, buf2);
217 journal_input(char *input)
219 journal_entry("input %s", input);
223 journal_command(char *cmd)
225 char *eptr = strchr(cmd, ' ');
226 journal_entry("command %.*s", eptr ? (int)(eptr - cmd) : -1, cmd);
230 journal_update(int etu)
232 journal_entry("update %d", etu);