]> git.pond.sub.org Git - empserver/blob - src/lib/subs/journal.c
Fix trailing whitespace
[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  *     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  *     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 (isprint(*p))
96                 putc(*p, journal);
97             else
98                 fprintf(journal, "\\%03o", *p);
99         }
100         fputs("\n", journal);
101         fflush(journal);
102         if (ferror(journal)) {
103             logerror("Error writing journal (%s)", strerror(errno));
104             clearerr(journal);
105         }
106     }
107 }
108
109 int
110 journal_startup(void)
111 {
112     if (!keep_journal)
113         return 0;
114     journal = journal_open();
115     if (!journal) {
116         logerror("Can't open %s (%s)", journal_fname, strerror(errno));
117         return -1;
118     }
119     journal_entry("startup");
120     return 0;
121 }
122
123 void
124 journal_shutdown(void)
125 {
126     journal_entry("shutdown");
127     if (journal) {
128         fclose(journal);
129         journal = NULL;
130     }
131 }
132
133 int
134 journal_reopen(void)
135 {
136     FILE *j;
137
138     if (!keep_journal)
139         return 0;
140     j = journal_open();
141     if (!j) {
142         logerror("Can't open %s (%s)", journal_fname, strerror(errno));
143         return -1;
144     }
145     if (journal)
146         fclose(journal);
147     journal = j;
148     return 0;
149 }
150
151 void
152 journal_prng(unsigned seed)
153 {
154     journal_entry("prng BSD %d", seed);
155 }
156
157 void
158 journal_login(void)
159 {
160     journal_entry("login %d %s %s",
161                   player->cnum, player->hostaddr, player->userid);
162 }
163
164 void
165 journal_logout(void)
166 {
167     journal_entry("logout %d", player->cnum);
168 }
169
170 void
171 journal_input(char *input)
172 {
173     journal_entry("input %s", input);
174 }
175
176 void
177 journal_update(int etu)
178 {
179     journal_entry("update %d", etu);
180 }