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"
73 // ----------------------------------------------------------------------------------------------------
74 // MULTI LOGFILE DEFINES/VARS
77 // max length for a line of the logfile
78 #define MAX_LOGFILE_LINE_LEN 256
80 // name of the multiplayer logfile
81 #define MULTI_LOGFILE_NAME "multi.log"
83 // echo all ml_printf's to the debug window
84 #define MULTI_LOGFILE_ECHO_TO_DEBUG
86 // how often we'll write an update to the logfile (in seconds)
87 #define MULTI_LOGFILE_UPDATE_TIME 2520 // every 42 minutes
90 CFILE *Multi_log_out = NULL;
92 // time when the logfile was opened
93 time_t Multi_log_open_systime = -1;
95 // time when we last updated the logfile
96 time_t Multi_log_update_systime = -1;
98 // ----------------------------------------------------------------------------------------------------
99 // MULTI LOGFILE FUNCTIONS
102 // write the standard header to the logfile
103 void multi_log_write_header()
110 strftime(str, 1024, "Freespace Multi Log - Opened %a, %b %d, %Y at %I:%M%p\n----\n----\n----\n\n", localtime(&timer));
114 // write the standard shutdown trailer
115 void multi_log_write_trailer()
122 strftime(str, 1024, "\n\n----\n----\n----\nFreespace Multi Log - Closing on %a, %b %d, %Y at %I:%M%p", localtime(&timer));
126 // write out some info about stuff
127 void multi_log_write_update()
129 int diff = (int)difftime(time(NULL), Multi_log_open_systime);
130 int hours, mins, seconds;
132 // figure out some time values
134 mins = (diff - (hours * 3600)) / 60;
135 seconds = (diff - (hours * 3600) - (mins * 60));
138 ml_printf("Server has been active for %d hours, %d minutes, and %d seconds", hours, mins, seconds);
141 // initialize the multi logfile
142 void multi_log_init()
144 // attempt to open the file
145 Multi_log_out = cfopen(MULTI_LOGFILE_NAME, "wt", CFILE_NORMAL, CF_TYPE_DATA);
147 // if we successfully opened the file, write the header
148 if(Multi_log_out != NULL){
149 multi_log_write_header();
151 // initialize our timer info
152 Multi_log_open_systime = time(NULL);
153 Multi_log_update_systime = Multi_log_open_systime;
155 nprintf(("Network","Error opening %s for writing!!\n",MULTI_LOGFILE_NAME));
159 // close down the multi logfile
160 void multi_log_close()
162 // if we have a valid file, write a trailer and close
163 if(Multi_log_out != NULL){
164 multi_log_write_trailer();
166 cfclose(Multi_log_out);
167 Multi_log_out = NULL;
171 // give some processing time to the logfile system so it can check up on stuff
172 void multi_log_process()
174 // if we don't have a valid logfile, do nothing
175 if(Multi_log_out == NULL){
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){
182 multi_log_write_update();
184 Multi_log_update_systime = time(NULL);
188 // printf function itself called by the ml_printf macro
189 void ml_printf(const char *format, ...)
191 char tmp[MAX_LOGFILE_LINE_LEN*4];
194 // if we don't have a valid logfile do nothing
195 if(Multi_log_out == NULL){
200 va_start(args, format);
201 SDL_vsnprintf(tmp, sizeof(tmp), format, args);
208 // string print function
209 void ml_string(const char *string, int add_time)
211 char tmp[MAX_LOGFILE_LINE_LEN*4];
215 // if we don't have a valid logfile do nothing
216 if(Multi_log_out == NULL){
220 // if the passed string is NULL, do nothing
225 // maybe add the time
229 strftime(time_str, 128, "%m/%d %H:%M:%S~ ", localtime(&timer));
230 SDL_strlcpy(tmp, time_str, sizeof(tmp));
231 SDL_strlcat(tmp, string, sizeof(tmp));
233 SDL_strlcpy(tmp, string, sizeof(tmp));
235 SDL_strlcat(tmp, "\n", sizeof(tmp));
237 // now print it to the logfile if necessary
238 cfputs(tmp, Multi_log_out);
239 cflush(Multi_log_out);
241 #if defined(MULTI_LOGFILE_ECHO_TO_DEBUG)
242 // nprintf(("Network","%s\n",tmp));
243 mprintf(("ML %s", tmp));