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