]> icculus.org git repositories - taylor/freespace2.git/blob - src/network/multi_log.cpp
Initial revision
[taylor/freespace2.git] / src / network / multi_log.cpp
1 /*
2  * $Logfile: /Freespace2/code/Network/multi_log.cpp $
3  * $Revision$
4  * $Date$
5  * $Author$
6  *
7  * Header file to support multiplayer logging functions
8  *
9  * $Log$
10  * Revision 1.1  2002/05/03 03:28:10  root
11  * Initial revision
12  *
13  * 
14  * 8     8/17/99 2:24p Dave
15  * Fixed wacky squad color stuff.
16  * 
17  * 7     6/07/99 9:51p Dave
18  * Consolidated all multiplayer ports into one.
19  * 
20  * 6     4/27/99 2:59p Dave
21  * Potential fix for reliable socket connection problem.
22  * 
23  * 5     4/09/99 2:21p Dave
24  * Multiplayer beta stuff. CD checking.
25  * 
26  * 4     1/24/99 11:37p Dave
27  * First full rev of beam weapons. Very customizable. Removed some bogus
28  * Int3()'s in low level net code.
29  * 
30  * 3     11/19/98 4:19p Dave
31  * Put IPX sockets back in psnet. Consolidated all multiplayer config
32  * files into one.
33  * 
34  * 2     10/07/98 10:53a Dave
35  * Initial checkin.
36  * 
37  * 1     10/07/98 10:50a Dave
38  * 
39  * 3     8/21/98 1:14p Dave
40  * Put in log system hooks in useful places.
41  * 
42  * 2     8/20/98 5:31p Dave
43  * Put in handy multiplayer logfile system. Now need to put in useful
44  * applications of it all over the code.
45  * 
46  * 1     8/20/98 2:00p Dave
47  *  
48  *
49  * $NoKeywords: $
50  */
51
52 #include <stdio.h>
53 #include <stdarg.h>
54 #include "multi_log.h"
55 #include "multi_options.h"
56 #include "cmdline.h"
57 #include "cfile.h"
58
59 // ----------------------------------------------------------------------------------------------------
60 // MULTI LOGFILE DEFINES/VARS
61 //
62
63 // max length for a line of the logfile
64 #define MAX_LOGFILE_LINE_LEN                                    256
65
66 // name of the multiplayer logfile
67 #define MULTI_LOGFILE_NAME                                              "multi.log"
68
69 // echo all ml_printf's to the debug window
70 #define MULTI_LOGFILE_ECHO_TO_DEBUG
71
72 // how often we'll write an update to the logfile (in seconds)
73 #define MULTI_LOGFILE_UPDATE_TIME                       2520                    // every 42 minutes
74
75 // outfile itself
76 CFILE *Multi_log_out = NULL;
77
78 // time when the logfile was opened
79 int Multi_log_open_systime = -1;
80
81 // time when we last updated the logfile
82 int Multi_log_update_systime = -1;
83
84 // ----------------------------------------------------------------------------------------------------
85 // MULTI LOGFILE FUNCTIONS
86 //
87
88 // write the standard header to the logfile
89 void multi_log_write_header()
90 {
91         char str[1024];
92         time_t timer;   
93
94         // header message
95         timer = time(NULL);     
96         strftime(str, 1024, "Freespace Multi Log - Opened %a, %b %d, %Y  at %I:%M%p\n----\n----\n----\n\n", localtime(&timer));
97         ml_string(str, 0);      
98 }
99
100 // write the standard shutdown trailer
101 void multi_log_write_trailer()
102 {
103         char str[1024];
104         time_t timer;           
105
106         // header message
107         timer = time(NULL);
108         strftime(str, 1024, "\n\n----\n----\n----\nFreespace Multi Log - Closing on %a, %b %d, %Y  at %I:%M%p", localtime(&timer));
109         ml_string(str, 0);      
110 }
111
112 // write out some info about stuff
113 void multi_log_write_update()
114 {
115         int diff = (int)difftime(time(NULL), Multi_log_open_systime);
116         int hours, mins, seconds;
117
118         // figure out some time values
119         hours = diff / 3600;
120         mins = (diff - (hours * 3600)) / 60;
121         seconds = (diff - (hours * 3600) - (mins * 60));
122
123         // print it out
124         ml_printf("Server has been active for %d hours, %d minutes, and %d seconds", hours, mins, seconds);
125 }
126
127 // initialize the multi logfile
128 void multi_log_init()
129 {
130         // attempt to open the file
131         Multi_log_out = cfopen(MULTI_LOGFILE_NAME, "wt", CFILE_NORMAL, CF_TYPE_DATA);
132
133         // if we successfully opened the file, write the header
134         if(Multi_log_out != NULL){
135                 multi_log_write_header();
136
137                 // initialize our timer info
138                 Multi_log_open_systime = time(NULL);
139                 Multi_log_update_systime = Multi_log_open_systime;
140         } else {
141                 nprintf(("Network","Error opening %s for writing!!\n",MULTI_LOGFILE_NAME));
142         }
143 }
144
145 // close down the multi logfile
146 void multi_log_close()
147 {
148         // if we have a valid file, write a trailer and close
149         if(Multi_log_out != NULL){
150                 multi_log_write_trailer();
151
152                 cfclose(Multi_log_out);
153                 Multi_log_out = NULL;
154         }
155 }
156
157 // give some processing time to the logfile system so it can check up on stuff
158 void multi_log_process()
159 {
160         // if we don't have a valid logfile, do nothing
161         if(Multi_log_out == NULL){
162                 return;
163         }
164
165         // check to see if we've been active a long enough time, and 
166         if(time(NULL) - Multi_log_update_systime > MULTI_LOGFILE_UPDATE_TIME){
167                 // write the update
168                 multi_log_write_update();
169
170                 Multi_log_update_systime = time(NULL);
171         }
172 }
173
174 // printf function itself called by the ml_printf macro
175 void ml_printf(char *format, ...)
176 {
177         char tmp[MAX_LOGFILE_LINE_LEN*4];
178         va_list args;
179
180         // if we don't have a valid logfile do nothing
181         if(Multi_log_out == NULL){
182                 return;
183         }
184         
185         // format the text
186         va_start(args, format);
187         vsprintf(tmp, format, args);
188         va_end(args);
189         
190         // log the string
191         ml_string(tmp);
192 }
193
194 // string print function
195 void ml_string(char *string, int add_time)
196 {
197         char tmp[MAX_LOGFILE_LINE_LEN*4];
198         char time_str[128];
199         time_t timer;   
200
201         // if we don't have a valid logfile do nothing
202         if(Multi_log_out == NULL){
203                 return;
204         }
205
206         // if the passed string is NULL, do nothing
207         if(string == NULL){
208                 return;
209         }
210
211         // maybe add the time
212         if(add_time){
213                 timer = time(NULL);
214
215                 strftime(time_str, 128, "%m/%d %H:%M:%S~   ", localtime(&timer));
216                 strcpy(tmp, time_str);
217                 strcat(tmp, string);
218         } else{
219                 strcpy(tmp, string);
220         }
221         strcat(tmp, "\n");
222
223         // now print it to the logfile if necessary     
224         cfputs(tmp, Multi_log_out);
225         cflush(Multi_log_out);
226
227 #if defined(MULTI_LOGFILE_ECHO_TO_DEBUG)
228         // nprintf(("Network","%s\n",tmp));
229         mprintf(("ML %s", tmp));
230 #endif
231 }