]> icculus.org git repositories - taylor/freespace2.git/blob - src/network/multi_log.cpp
silence some compiler warnings
[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 #include "systemvars.h"
73
74
75 // ----------------------------------------------------------------------------------------------------
76 // MULTI LOGFILE DEFINES/VARS
77 //
78
79 // max length for a line of the logfile
80 #define MAX_LOGFILE_LINE_LEN                                    256
81
82 // name of the multiplayer logfile
83 #define MULTI_LOGFILE_NAME                                              "multi.log"
84 #define MULTI_STD_LOGFILE_NAME                                  "multi.%d.log"
85
86 // echo all ml_printf's to the debug window
87 #define MULTI_LOGFILE_ECHO_TO_DEBUG
88
89 // how often we'll write an update to the logfile (in seconds)
90 #define MULTI_LOGFILE_UPDATE_TIME                       2520                    // every 42 minutes
91
92 // outfile itself
93 CFILE *Multi_log_out = NULL;
94
95 // time when the logfile was opened
96 time_t Multi_log_open_systime = -1;
97
98 // time when we last updated the logfile
99 time_t Multi_log_update_systime = -1;
100
101 // ----------------------------------------------------------------------------------------------------
102 // MULTI LOGFILE FUNCTIONS
103 //
104
105 // write the standard header to the logfile
106 void multi_log_write_header()
107 {
108         char str[1024];
109         time_t timer;   
110
111         // header message
112         timer = time(NULL);     
113         strftime(str, 1024, "Freespace Multi Log - Opened %a, %b %d, %Y  at %I:%M%p\n----\n----\n----\n\n", localtime(&timer));
114         ml_string(str, 0);      
115 }
116
117 // write the standard shutdown trailer
118 void multi_log_write_trailer()
119 {
120         char str[1024];
121         time_t timer;           
122
123         // header message
124         timer = time(NULL);
125         strftime(str, 1024, "\n\n----\n----\n----\nFreespace Multi Log - Closing on %a, %b %d, %Y  at %I:%M%p", localtime(&timer));
126         ml_string(str, 0);      
127 }
128
129 // write out some info about stuff
130 void multi_log_write_update()
131 {
132         int diff = (int)difftime(time(NULL), Multi_log_open_systime);
133         int hours, mins, seconds;
134
135         // figure out some time values
136         hours = diff / 3600;
137         mins = (diff - (hours * 3600)) / 60;
138         seconds = (diff - (hours * 3600) - (mins * 60));
139
140         // print it out
141         ml_printf("Server has been active for %d hours, %d minutes, and %d seconds", hours, mins, seconds);
142 }
143
144 // initialize the multi logfile
145 void multi_log_init()
146 {
147         char lname[32];
148
149         if (Is_standalone) {
150                 SDL_snprintf(lname, SDL_arraysize(lname), MULTI_STD_LOGFILE_NAME, Multi_options_g.port);
151         } else {
152                 SDL_strlcpy(lname, MULTI_LOGFILE_NAME, SDL_arraysize(lname));
153         }
154
155         // attempt to open the file
156         Multi_log_out = cfopen(lname, "wt", CFILE_NORMAL, CF_TYPE_DATA);
157
158         // if we successfully opened the file, write the header
159         if(Multi_log_out != NULL){
160                 multi_log_write_header();
161
162                 // initialize our timer info
163                 Multi_log_open_systime = time(NULL);
164                 Multi_log_update_systime = Multi_log_open_systime;
165         } else {
166                 nprintf(("Network","Error opening %s for writing!!\n",MULTI_LOGFILE_NAME));
167         }
168 }
169
170 // close down the multi logfile
171 void multi_log_close()
172 {
173         // if we have a valid file, write a trailer and close
174         if(Multi_log_out != NULL){
175                 multi_log_write_trailer();
176
177                 cfclose(Multi_log_out);
178                 Multi_log_out = NULL;
179         }
180 }
181
182 // give some processing time to the logfile system so it can check up on stuff
183 void multi_log_process()
184 {
185         // if we don't have a valid logfile, do nothing
186         if(Multi_log_out == NULL){
187                 return;
188         }
189
190         // check to see if we've been active a long enough time, and 
191         if(time(NULL) - Multi_log_update_systime > MULTI_LOGFILE_UPDATE_TIME){
192                 // write the update
193                 multi_log_write_update();
194
195                 Multi_log_update_systime = time(NULL);
196         }
197 }
198
199 // printf function itself called by the ml_printf macro
200 void ml_printf(const char *format, ...)
201 {
202         char tmp[MAX_LOGFILE_LINE_LEN*4];
203         va_list args;
204
205         // if we don't have a valid logfile do nothing
206         if(Multi_log_out == NULL){
207                 return;
208         }
209         
210         // format the text
211         va_start(args, format);
212         SDL_vsnprintf(tmp, SDL_arraysize(tmp), format, args);
213         va_end(args);
214         
215         // log the string
216         ml_string(tmp);
217 }
218
219 // string print function
220 void ml_string(const char *string, int add_time)
221 {
222         char tmp[MAX_LOGFILE_LINE_LEN*4];
223         char time_str[128];
224         time_t timer;   
225
226         // if we don't have a valid logfile do nothing
227         if(Multi_log_out == NULL){
228                 return;
229         }
230
231         // if the passed string is NULL, do nothing
232         if(string == NULL){
233                 return;
234         }
235
236         // maybe add the time
237         if(add_time){
238                 timer = time(NULL);
239
240                 strftime(time_str, 128, "%m/%d %H:%M:%S~   ", localtime(&timer));
241                 SDL_strlcpy(tmp, time_str, SDL_arraysize(tmp));
242                 SDL_strlcat(tmp, string, SDL_arraysize(tmp));
243         } else{
244                 SDL_strlcpy(tmp, string, SDL_arraysize(tmp));
245         }
246         SDL_strlcat(tmp, "\n", SDL_arraysize(tmp));
247
248         // now print it to the logfile if necessary     
249         cfputs(tmp, Multi_log_out);
250         cflush(Multi_log_out);
251
252 #if defined(MULTI_LOGFILE_ECHO_TO_DEBUG)
253         // nprintf(("Network","%s\n",tmp));
254         mprintf(("ML %s", tmp));
255 #endif
256 }