]> git.pond.sub.org Git - empserver/blob - src/lib/subs/journal.c
New journal event command
[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  *     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, ...)
70     ATTRIBUTE((format (printf, 1, 2)));
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 %10.10s ",
89                 ctime(&now), empth_name(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         fflush(journal);
103         if (ferror(journal)) {
104             logerror("Error writing journal (%s)", strerror(errno));
105             clearerr(journal);
106         }
107     }
108 }
109
110 int
111 journal_startup(void)
112 {
113     if (!keep_journal)
114         return 0;
115     journal = journal_open();
116     if (!journal) {
117         logerror("Can't open %s (%s)", journal_fname, strerror(errno));
118         return -1;
119     }
120     journal_entry("startup");
121     return 0;
122 }
123
124 void
125 journal_shutdown(void)
126 {
127     journal_entry("shutdown");
128     if (journal) {
129         fclose(journal);
130         journal = NULL;
131     }
132 }
133
134 int
135 journal_reopen(void)
136 {
137     FILE *j;
138
139     if (!keep_journal)
140         return 0;
141     j = journal_open();
142     if (!j) {
143         logerror("Can't open %s (%s)", journal_fname, strerror(errno));
144         return -1;
145     }
146     if (journal)
147         fclose(journal);
148     journal = j;
149     return 0;
150 }
151
152 void
153 journal_prng(unsigned seed)
154 {
155     journal_entry("prng BSD %d", seed);
156 }
157
158 void
159 journal_login(void)
160 {
161     journal_entry("login %d %s %s",
162                   player->cnum, player->hostaddr, player->userid);
163 }
164
165 void
166 journal_logout(void)
167 {
168     journal_entry("logout %d", player->cnum);
169 }
170
171 void
172 journal_input(char *input)
173 {
174     journal_entry("input %s", input);
175 }
176
177 void
178 journal_command(char *cmd)
179 {
180     char *eptr = strchr(cmd, ' ');
181     journal_entry("command %.*s", eptr ? (int)(eptr - cmd) : -1, cmd);
182 }
183
184 void
185 journal_update(int etu)
186 {
187     journal_entry("update %d", etu);
188 }