]> git.pond.sub.org Git - empserver/blob - src/lib/subs/journal.c
License upgrade to GPL version 3 or later
[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-2008
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  *     input INPUT
47  *     update ETU
48  */
49
50 #include <config.h>
51
52 #include <ctype.h>
53 #include <errno.h>
54 #include <stdarg.h>
55 #include <stdio.h>
56 #include <time.h>
57 #include "misc.h"
58 #include "empthread.h"
59 #include "journal.h"
60 #include "optlist.h"
61 #include "player.h"
62 #include "prototypes.h"
63
64 static char journal_fname[] = "journal.log";
65 static FILE *journal;
66
67 static void journal_entry(char *fmt, ...)
68     ATTRIBUTE((format (printf, 1, 2)));
69
70 static FILE *
71 journal_open(void)
72 {
73     return fopen(journal_fname, "a+");
74 }
75
76 static void
77 journal_entry(char *fmt, ...)
78 {
79     static char buf[1024];
80     va_list ap;
81     time_t now;
82     unsigned char *p;
83
84     if (journal) {
85         time(&now);
86         fprintf(journal, "%.24s %10.10s ",
87                 ctime(&now), empth_name(empth_self()));
88
89         va_start(ap, fmt);
90         vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
91         va_end(ap);
92
93         for (p = (unsigned char *)buf; *p; p++) {
94             if (isprint(*p))
95                 putc(*p, journal);
96             else
97                 fprintf(journal, "\\%03o", *p);
98         }
99         fputs("\n", journal);
100         fflush(journal);
101         if (ferror(journal)) {
102             logerror("Error writing journal (%s)", strerror(errno));
103             clearerr(journal);
104         }
105     }
106 }
107
108 int
109 journal_startup(void)
110 {
111     if (!keep_journal)
112         return 0;
113     journal = journal_open();
114     if (!journal) {
115         logerror("Can't open %s (%s)", journal_fname, strerror(errno));
116         return -1;
117     }
118     journal_entry("startup");
119     return 0;
120 }
121
122 void
123 journal_shutdown(void)
124 {
125     journal_entry("shutdown");
126     if (journal) {
127         fclose(journal);
128         journal = NULL;
129     }
130 }
131
132 int
133 journal_reopen(void)
134 {
135     FILE *j;
136
137     if (!keep_journal)
138         return 0;
139     j = journal_open();
140     if (!j) {
141         logerror("Can't open %s (%s)", journal_fname, strerror(errno));
142         return -1;
143     }
144     if (journal)
145         fclose(journal);
146     journal = j;
147     return 0;
148 }
149
150 void
151 journal_prng(unsigned seed)
152 {
153     journal_entry("prng BSD %d", seed);
154 }
155
156 void
157 journal_login(void)
158 {
159     journal_entry("login %d %s %s",
160                   player->cnum, player->hostaddr, player->userid);
161 }
162
163 void
164 journal_logout(void)
165 {
166     journal_entry("logout %d", player->cnum);
167 }
168
169 void
170 journal_input(char *input)
171 {
172     journal_entry("input %s", input);
173 }
174
175 void
176 journal_update(int etu)
177 {
178     journal_entry("update %d", etu);
179 }