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