]> git.pond.sub.org Git - empserver/blob - src/lib/subs/journal.c
New journal event output
[empserver] / src / lib / subs / journal.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2010, 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
73 static FILE *
74 journal_open(void)
75 {
76     return fopen(journal_fname, "a+");
77 }
78
79 static void
80 journal_entry(char *fmt, ...)
81 {
82     static char buf[1024];
83     va_list ap;
84     time_t now;
85     unsigned char *p;
86
87     if (journal) {
88         time(&now);
89         fprintf(journal, "%.24s %10.10s ",
90                 ctime(&now), empth_name(empth_self()));
91
92         va_start(ap, fmt);
93         vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
94         va_end(ap);
95
96         for (p = (unsigned char *)buf; *p; p++) {
97             if (isprint(*p))
98                 putc(*p, journal);
99             else
100                 fprintf(journal, "\\%03o", *p);
101         }
102         fputs("\n", journal);
103         if (fmt[0] != 'o')      /* FIXME disgusting hack */
104             fflush(journal);
105         if (ferror(journal)) {
106             logerror("Error writing journal (%s)", strerror(errno));
107             clearerr(journal);
108         }
109     }
110 }
111
112 int
113 journal_startup(void)
114 {
115     if (!keep_journal)
116         return 0;
117     journal = journal_open();
118     if (!journal) {
119         logerror("Can't open %s (%s)", journal_fname, strerror(errno));
120         return -1;
121     }
122     journal_entry("startup");
123     return 0;
124 }
125
126 void
127 journal_shutdown(void)
128 {
129     journal_entry("shutdown");
130     if (journal) {
131         fclose(journal);
132         journal = NULL;
133     }
134 }
135
136 int
137 journal_reopen(void)
138 {
139     FILE *j;
140
141     if (!keep_journal)
142         return 0;
143     j = journal_open();
144     if (!j) {
145         logerror("Can't open %s (%s)", journal_fname, strerror(errno));
146         return -1;
147     }
148     if (journal)
149         fclose(journal);
150     journal = j;
151     return 0;
152 }
153
154 void
155 journal_prng(unsigned seed)
156 {
157     journal_entry("prng BSD %d", seed);
158 }
159
160 void
161 journal_login(void)
162 {
163     journal_entry("login %d %s %s",
164                   player->cnum, player->hostaddr, player->userid);
165 }
166
167 void
168 journal_logout(void)
169 {
170     journal_entry("logout %d", player->cnum);
171 }
172
173 void
174 journal_output(struct player *pl, int id, char *output)
175 {
176     if (keep_journal < 2)
177         return;
178     if (pl && pl->state == PS_PLAYING)
179         journal_entry("output %d %d %s", pl->cnum, id, output);
180     else
181         journal_entry("output %p %d %s", pl, id, output);
182 }
183
184 void
185 journal_input(char *input)
186 {
187     journal_entry("input %s", input);
188 }
189
190 void
191 journal_command(char *cmd)
192 {
193     char *eptr = strchr(cmd, ' ');
194     journal_entry("command %.*s", eptr ? (int)(eptr - cmd) : -1, cmd);
195 }
196
197 void
198 journal_update(int etu)
199 {
200     journal_entry("update %d", etu);
201 }