]> git.pond.sub.org Git - empserver/blob - src/lib/subs/journal.c
Escape '\\' in journal, but not '\t'
[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(char *fmt, ...)
69     ATTRIBUTE((format (printf, 1, 2)));
70
71 static FILE *
72 journal_open(void)
73 {
74     return fopen(journal_fname, "a+");
75 }
76
77 static void
78 journal_entry(char *fmt, ...)
79 {
80     static char buf[1024];
81     va_list ap;
82     time_t now;
83     unsigned char *p;
84
85     if (journal) {
86         time(&now);
87         fprintf(journal, "%.24s %10.10s ",
88                 ctime(&now), empth_name(empth_self()));
89
90         va_start(ap, fmt);
91         vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
92         va_end(ap);
93
94         for (p = (unsigned char *)buf; *p; p++) {
95             if (*p == '\\')
96                 fprintf(journal, "\\\\");
97             else if (isprint(*p) || *p == '\t')
98                 putc(*p, journal);
99             else
100                 fprintf(journal, "\\%03o", *p);
101         }
102         fputs("\n", journal);
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_input(char *input)
174 {
175     journal_entry("input %s", input);
176 }
177
178 void
179 journal_command(char *cmd)
180 {
181     char *eptr = strchr(cmd, ' ');
182     journal_entry("command %.*s", eptr ? (int)(eptr - cmd) : -1, cmd);
183 }
184
185 void
186 journal_update(int etu)
187 {
188     journal_entry("update %d", etu);
189 }