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