src/lib/common/journal.c

Go to the documentation of this file.
00001 /*
00002  *  Empire - A multi-player, client/server Internet based war game.
00003  *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
00004  *                           Ken Stevens, Steve McClure
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  *  ---
00021  *
00022  *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
00023  *  related information and legal notices. It is expected that any future
00024  *  projects/authors will amend these files as needed.
00025  *
00026  *  ---
00027  *
00028  *  journal.c: Log a journal of events to a file
00029  * 
00030  *  Known contributors to this file:
00031  *     Markus Armbruster, 2004-2007
00032  */
00033 
00034 /*
00035  * Journal file format: each line logs an event, and looks like this:
00036  *
00037  *     TIME THREAD EVENT DATA
00038  *
00039  * Events and their data are:
00040  *
00041  *     startup
00042  *     shutdown
00043  *     login CNUM HOSTADDR USER
00044  *     logout CNUM
00045  *     input INPUT
00046  *     update ETU
00047  */
00048 
00049 #include <config.h>
00050 
00051 #include <ctype.h>
00052 #include <errno.h>
00053 #include <stdarg.h>
00054 #include <stdio.h>
00055 #include <time.h>
00056 #include "misc.h"
00057 #include "empthread.h"
00058 #include "journal.h"
00059 #include "optlist.h"
00060 #include "player.h"
00061 #include "prototypes.h"
00062 
00063 static char journal_fname[] = "journal.log";
00064 static FILE *journal;
00065 
00066 static FILE *
00067 journal_open(void)
00068 {
00069     return fopen(journal_fname, "a+");
00070 }
00071 
00072 static void
00073 journal_entry(char *fmt, ...)
00074 {
00075     static char buf[1024];
00076     va_list ap;
00077     time_t now;
00078     unsigned char *p;
00079 
00080     if (journal) {
00081         time(&now);
00082         fprintf(journal, "%.24s %p ", ctime(&now), empth_self());
00083         
00084         va_start(ap, fmt);
00085         vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
00086         va_end(ap);
00087 
00088         for (p = (unsigned char *)buf; *p; p++) {
00089             if (isprint(*p))
00090                 putc(*p, journal);
00091             else
00092                 fprintf(journal, "\\%03o", *p);
00093         }
00094         fputs("\n", journal);
00095         if (debug)
00096             fflush(journal);
00097         if (ferror(journal)) {
00098             logerror("Error writing journal (%s)", strerror(errno));
00099             clearerr(journal);
00100         }
00101     }
00102 }
00103 
00104 int
00105 journal_startup(void)
00106 {
00107     if (!keep_journal)
00108         return 0;
00109     journal = journal_open();
00110     if (!journal) {
00111         logerror("Can't open %s (%s)", journal_fname, strerror(errno));
00112         return -1;
00113     }
00114     journal_entry("startup");
00115     return 0;
00116 }
00117 
00118 void
00119 journal_shutdown(void)
00120 {
00121     journal_entry("shutdown");
00122     if (journal) {
00123         fclose(journal);
00124         journal = NULL;
00125     }
00126 }
00127 
00128 int
00129 journal_reopen(void)
00130 {
00131     FILE *j;
00132 
00133     if (!keep_journal)
00134         return 0;
00135     j = journal_open();
00136     if (!j) {
00137         logerror("Can't open %s (%s)", journal_fname, strerror(errno));
00138         return -1;
00139     }
00140     if (journal)
00141         fclose(journal);
00142     journal = j;
00143     return 0;
00144 }
00145 
00146 void
00147 journal_login(void)
00148 {
00149     journal_entry("login %d %s %s",
00150                   player->cnum, player->hostaddr, player->userid);
00151 }
00152 
00153 void
00154 journal_logout(void)
00155 {
00156     journal_entry("logout %d", player->cnum);
00157 }
00158 
00159 void
00160 journal_input(char *input)
00161 {
00162     journal_entry("input %s", input);
00163 }
00164 
00165 void
00166 journal_update(int etu)
00167 {
00168     journal_entry("update %d", etu);
00169 }

Generated on Fri Mar 28 11:01:14 2008 for empserver by  doxygen 1.5.2