]> git.pond.sub.org Git - empserver/blob - src/lib/subs/journal.c
Fix journal not to truncate long lines
[empserver] / src / lib / subs / journal.c
1 /*
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
5  *
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.
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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
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.
24  *
25  *  ---
26  *
27  *  journal.c: Log a journal of events to a file
28  *
29  *  Known contributors to this file:
30  *     Markus Armbruster, 2004-2011
31  *     Ron Koenderink, 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  *     update ETU
49  */
50
51 #include <config.h>
52
53 #include <ctype.h>
54 #include <errno.h>
55 #include <stdarg.h>
56 #include <stdio.h>
57 #include <time.h>
58 #include "misc.h"
59 #include "empthread.h"
60 #include "journal.h"
61 #include "optlist.h"
62 #include "player.h"
63 #include "prototypes.h"
64
65 static char journal_fname[] = "journal.log";
66 static FILE *journal;
67
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)));
72
73 static FILE *
74 journal_open(void)
75 {
76     return fopen(journal_fname, "a+");
77 }
78
79 static void
80 journal_entry_vstart(char *fmt, va_list ap)
81 {
82     time_t now;
83
84     if (!journal)
85         return;
86     time(&now);
87     fprintf(journal, "%.24s %10.10s ",
88             ctime(&now), empth_name(empth_self()));
89     vfprintf(journal, fmt, ap);
90 }
91
92 static void
93 journal_entry_start(char *fmt, ...)
94 {
95     va_list ap;
96
97     va_start(ap, fmt);
98     journal_entry_vstart(fmt, ap);
99     va_end(ap);
100 }
101
102 static void
103 journal_entry_pr(char *s, size_t n)
104 {
105     unsigned char *p;
106
107     if (!journal)
108         return;
109     for (p = (unsigned char *)s; *p && n; p++) {
110         if (*p == '\\')
111             fputs("\\\\", journal);
112         else if (isprint(*p))
113             putc(*p, journal);
114         else
115             fprintf(journal, "\\%03o", *p);
116         n--;
117     }
118 }
119
120 static void
121 journal_entry_end(void)
122 {
123     if (!journal)
124         return;
125     fputc('\n', journal);
126     fflush(journal);
127     if (ferror(journal)) {
128         logerror("Error writing journal (%s)", strerror(errno));
129         clearerr(journal);
130     }
131 }
132
133 static void
134 journal_entry(char *fmt, ...)
135 {
136     va_list ap;
137
138     va_start(ap, fmt);
139     journal_entry_vstart(fmt, ap);
140     va_end(ap);
141     journal_entry_end();
142 }
143
144 int
145 journal_startup(void)
146 {
147     if (!keep_journal)
148         return 0;
149     journal = journal_open();
150     if (!journal) {
151         logerror("Can't open %s (%s)", journal_fname, strerror(errno));
152         return -1;
153     }
154     journal_entry("startup");
155     return 0;
156 }
157
158 void
159 journal_shutdown(void)
160 {
161     journal_entry("shutdown");
162     if (journal) {
163         fclose(journal);
164         journal = NULL;
165     }
166 }
167
168 int
169 journal_reopen(void)
170 {
171     FILE *j;
172
173     if (!keep_journal)
174         return 0;
175     j = journal_open();
176     if (!j) {
177         logerror("Can't open %s (%s)", journal_fname, strerror(errno));
178         return -1;
179     }
180     if (journal)
181         fclose(journal);
182     journal = j;
183     return 0;
184 }
185
186 void
187 journal_prng(unsigned seed)
188 {
189     journal_entry("prng BSD %d", seed);
190 }
191
192 void
193 journal_login(void)
194 {
195     journal_entry("login %d %s %s",
196                   player->cnum, player->hostaddr, player->userid);
197 }
198
199 void
200 journal_logout(void)
201 {
202     journal_entry("logout %d", player->cnum);
203 }
204
205 void
206 journal_input(char *input)
207 {
208     journal_entry_start("input ");
209     journal_entry_pr(input, -1);
210     journal_entry_end();
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 }