2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
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
10 * $Logfile: /Freespace2/code/Network/multi_log.cpp $
15 * Header file to support multiplayer logging functions
18 * Revision 1.3 2002/06/09 04:41:23 relnev
19 * added copyright header
21 * Revision 1.2 2002/05/07 03:16:47 theoddone33
22 * The Great Newline Fix
24 * Revision 1.1.1.1 2002/05/03 03:28:10 root
28 * 8 8/17/99 2:24p Dave
29 * Fixed wacky squad color stuff.
31 * 7 6/07/99 9:51p Dave
32 * Consolidated all multiplayer ports into one.
34 * 6 4/27/99 2:59p Dave
35 * Potential fix for reliable socket connection problem.
37 * 5 4/09/99 2:21p Dave
38 * Multiplayer beta stuff. CD checking.
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.
44 * 3 11/19/98 4:19p Dave
45 * Put IPX sockets back in psnet. Consolidated all multiplayer config
48 * 2 10/07/98 10:53a Dave
51 * 1 10/07/98 10:50a Dave
53 * 3 8/21/98 1:14p Dave
54 * Put in log system hooks in useful places.
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.
60 * 1 8/20/98 2:00p Dave
68 #include "multi_log.h"
69 #include "multi_options.h"
72 #include "systemvars.h"
75 // ----------------------------------------------------------------------------------------------------
76 // MULTI LOGFILE DEFINES/VARS
79 // max length for a line of the logfile
80 #define MAX_LOGFILE_LINE_LEN 256
82 // name of the multiplayer logfile
83 #define MULTI_LOGFILE_NAME "multi.log"
84 #define MULTI_STD_LOGFILE_NAME "multi.%d.log"
86 // echo all ml_printf's to the debug window
87 #define MULTI_LOGFILE_ECHO_TO_DEBUG
89 // how often we'll write an update to the logfile (in seconds)
90 #define MULTI_LOGFILE_UPDATE_TIME 2520 // every 42 minutes
93 CFILE *Multi_log_out = NULL;
95 // time when the logfile was opened
96 time_t Multi_log_open_systime = -1;
98 // time when we last updated the logfile
99 time_t Multi_log_update_systime = -1;
101 // ----------------------------------------------------------------------------------------------------
102 // MULTI LOGFILE FUNCTIONS
105 // write the standard header to the logfile
106 void multi_log_write_header()
113 strftime(str, 1024, "Freespace Multi Log - Opened %a, %b %d, %Y at %I:%M%p\n----\n----\n----\n\n", localtime(&timer));
117 // write the standard shutdown trailer
118 void multi_log_write_trailer()
125 strftime(str, 1024, "\n\n----\n----\n----\nFreespace Multi Log - Closing on %a, %b %d, %Y at %I:%M%p", localtime(&timer));
129 // write out some info about stuff
130 void multi_log_write_update()
132 int diff = (int)difftime(time(NULL), Multi_log_open_systime);
133 int hours, mins, seconds;
135 // figure out some time values
137 mins = (diff - (hours * 3600)) / 60;
138 seconds = (diff - (hours * 3600) - (mins * 60));
141 ml_printf("Server has been active for %d hours, %d minutes, and %d seconds", hours, mins, seconds);
144 // initialize the multi logfile
145 void multi_log_init()
150 SDL_snprintf(lname, SDL_arraysize(lname), MULTI_STD_LOGFILE_NAME, Multi_options_g.port);
152 SDL_strlcpy(lname, MULTI_LOGFILE_NAME, SDL_arraysize(lname));
155 // attempt to open the file
156 Multi_log_out = cfopen(lname, "wt", CFILE_NORMAL, CF_TYPE_DATA);
158 // if we successfully opened the file, write the header
159 if(Multi_log_out != NULL){
160 multi_log_write_header();
162 // initialize our timer info
163 Multi_log_open_systime = time(NULL);
164 Multi_log_update_systime = Multi_log_open_systime;
166 nprintf(("Network","Error opening %s for writing!!\n",MULTI_LOGFILE_NAME));
170 // close down the multi logfile
171 void multi_log_close()
173 // if we have a valid file, write a trailer and close
174 if(Multi_log_out != NULL){
175 multi_log_write_trailer();
177 cfclose(Multi_log_out);
178 Multi_log_out = NULL;
182 // give some processing time to the logfile system so it can check up on stuff
183 void multi_log_process()
185 // if we don't have a valid logfile, do nothing
186 if(Multi_log_out == NULL){
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){
193 multi_log_write_update();
195 Multi_log_update_systime = time(NULL);
199 // printf function itself called by the ml_printf macro
200 void ml_printf(const char *format, ...)
202 char tmp[MAX_LOGFILE_LINE_LEN*4];
205 // if we don't have a valid logfile do nothing
206 if(Multi_log_out == NULL){
211 va_start(args, format);
212 SDL_vsnprintf(tmp, SDL_arraysize(tmp), format, args);
219 // string print function
220 void ml_string(const char *string, int add_time)
222 char tmp[MAX_LOGFILE_LINE_LEN*4];
226 // if we don't have a valid logfile do nothing
227 if(Multi_log_out == NULL){
231 // if the passed string is NULL, do nothing
236 // maybe add the time
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));
244 SDL_strlcpy(tmp, string, SDL_arraysize(tmp));
246 SDL_strlcat(tmp, "\n", SDL_arraysize(tmp));
248 // now print it to the logfile if necessary
249 cfputs(tmp, Multi_log_out);
250 cflush(Multi_log_out);
252 #if defined(MULTI_LOGFILE_ECHO_TO_DEBUG)
253 // nprintf(("Network","%s\n",tmp));
254 mprintf(("ML %s", tmp));