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