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