2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
22 * related information and legal notices. It is expected that any future
23 * 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-2011
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
59 #include "empthread.h"
63 #include "prototypes.h"
65 static char journal_fname[] = "journal.log";
68 static void journal_entry_start(char *fmt, ...)
69 ATTRIBUTE((format (printf, 1, 2)));
70 static void journal_entry(char *fmt, ...)
71 ATTRIBUTE((format (printf, 1, 2)));
76 return fopen(journal_fname, "a+");
80 journal_entry_vstart(char *fmt, va_list ap)
87 fprintf(journal, "%.24s %10.10s ",
88 ctime(&now), empth_name(empth_self()));
89 vfprintf(journal, fmt, ap);
93 journal_entry_start(char *fmt, ...)
98 journal_entry_vstart(fmt, ap);
103 journal_entry_pr(char *s, size_t n)
109 for (p = (unsigned char *)s; *p && n; p++) {
111 fputs("\\\\", journal);
112 else if (isprint(*p))
115 fprintf(journal, "\\%03o", *p);
121 journal_entry_end(void)
125 fputc('\n', journal);
127 if (ferror(journal)) {
128 logerror("Error writing journal (%s)", strerror(errno));
134 journal_entry(char *fmt, ...)
139 journal_entry_vstart(fmt, ap);
145 journal_startup(void)
149 journal = journal_open();
151 logerror("Can't open %s (%s)", journal_fname, strerror(errno));
154 journal_entry("startup");
159 journal_shutdown(void)
161 journal_entry("shutdown");
177 logerror("Can't open %s (%s)", journal_fname, strerror(errno));
187 journal_prng(unsigned seed)
189 journal_entry("prng BSD %d", seed);
195 journal_entry("login %d %s %s",
196 player->cnum, player->hostaddr, player->userid);
202 journal_entry("logout %d", player->cnum);
206 journal_input(char *input)
208 journal_entry_start("input ");
209 journal_entry_pr(input, -1);
214 journal_command(char *cmd)
216 char *eptr = strchr(cmd, ' ');
217 journal_entry("command %.*s", eptr ? (int)(eptr - cmd) : -1, cmd);
221 journal_update(int etu)
223 journal_entry("update %d", etu);