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