1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
/*
* Copyright (C) Volition, Inc. 1999. All rights reserved.
*
* All source code herein is the property of Volition, Inc. You may not sell
* or otherwise commercially exploit the source or things you created based on the
* source.
*
*/
#ifndef WIN32 // Goober5000
#ifndef NDEBUG
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "globalincs/pstypes.h"
#include "osapi/outwnd.h"
#include "osapi/osapi.h"
#include "osapi/osregistry.h"
#include "cfile/cfilesystem.h"
#include "globalincs/globals.h"
#include "parse/parselo.h"
void outwnd_print(const char *id = NULL, const char *temp = NULL);
#define MAX_LINE_WIDTH 128
bool outwnd_inited = false;
ubyte Outwnd_no_filter_file = 0; // 0 = .cfg file found, 1 = not found and warning not printed yet, 2 = not found and warning printed
struct outwnd_filter_struct {
char name[NAME_LENGTH];
bool enabled;
outwnd_filter_struct()
: enabled( false )
{
name[ 0 ] = 0;
}
};
SCP_vector<outwnd_filter_struct> OutwndFilter;
int outwnd_filter_loaded = 0;
// used for file logging
int Log_debug_output_to_file = 1;
FILE *Log_fp = NULL;
char *FreeSpace_logfilename = "fs2_open.log";
SCP_string safe_string;
void load_filter_info(void)
{
FILE *fp = NULL;
char pathname[MAX_PATH_LEN];
char inbuf[NAME_LENGTH+4];
outwnd_filter_struct new_filter;
int z;
outwnd_filter_loaded = 1;
snprintf( pathname, MAX_PATH_LEN, "%s/%s/%s/%s", detect_home(), Osreg_user_dir, Pathtypes[CF_TYPE_DATA].path, NOX("debug_filter.cfg") );
fp = fopen(pathname, "rt");
if (!fp) {
Outwnd_no_filter_file = 1;
memset( &new_filter, 0, sizeof(outwnd_filter_struct) );
strcpy_s( new_filter.name, "error" );
new_filter.enabled = true;
OutwndFilter.push_back( new_filter );
memset( &new_filter, 0, sizeof(outwnd_filter_struct) );
strcpy_s( new_filter.name, "general" );
new_filter.enabled = true;
OutwndFilter.push_back( new_filter );
memset( &new_filter, 0, sizeof(outwnd_filter_struct) );
strcpy_s( new_filter.name, "warning" );
new_filter.enabled = true;
OutwndFilter.push_back( new_filter );
return;
}
Outwnd_no_filter_file = 0;
while ( fgets(inbuf, NAME_LENGTH+3, fp) ) {
memset( &new_filter, 0, sizeof(outwnd_filter_struct) );
if (*inbuf == '+')
new_filter.enabled = true;
else if (*inbuf == '-')
new_filter.enabled = false;
else
continue; // skip everything else
z = strlen(inbuf) - 1;
if (inbuf[z] == '\n')
inbuf[z] = 0;
Assert( strlen(inbuf+1) < NAME_LENGTH );
strcpy_s(new_filter.name, inbuf + 1);
if ( !stricmp(new_filter.name, "error") ) {
new_filter.enabled = true;
} else if ( !stricmp(new_filter.name, "general") ) {
new_filter.enabled = true;
} else if ( !stricmp(new_filter.name, "warning") ) {
new_filter.enabled = true;
}
OutwndFilter.push_back( new_filter );
}
if ( ferror(fp) && !feof(fp) )
nprintf(("Error", "Error reading \"%s\"\n", pathname));
fclose(fp);
}
void save_filter_info(void)
{
FILE *fp = NULL;
char pathname[MAX_PATH_LEN];
if ( !outwnd_filter_loaded )
return;
if (Outwnd_no_filter_file)
return; // No file, don't save
snprintf( pathname, MAX_PATH_LEN, "%s/%s/%s/%s", detect_home(), Osreg_user_dir, Pathtypes[CF_TYPE_DATA].path, NOX("debug_filter.cfg") );
fp = fopen(pathname, "wt");
if (fp) {
for (uint i = 0; i < OutwndFilter.size(); i++)
fprintf(fp, "%c%s\n", OutwndFilter[i].enabled ? '+' : '-', OutwndFilter[i].name);
fclose(fp);
}
}
void outwnd_printf2(const char *format, ...)
{
SCP_string temp;
va_list args;
if (format == NULL)
return;
va_start(args, format);
vsprintf(temp, format, args);
va_end(args);
outwnd_print("General", temp.c_str());
}
void outwnd_printf(const char *id, const char *format, ...)
{
SCP_string temp;
va_list args;
if ( (id == NULL) || (format == NULL) )
return;
va_start(args, format);
vsprintf(temp, format, args);
va_end(args);
outwnd_print(id, temp.c_str());
}
void outwnd_print(const char *id, const char *tmp)
{
uint i;
if ( (id == NULL) || (tmp == NULL) )
return;
if ( !outwnd_inited ) {
fputs("outwnd not initialized yet... ", stdout);
fputs(tmp, stdout);
fflush(stdout);
return;
}
if (Outwnd_no_filter_file == 1) {
Outwnd_no_filter_file = 2;
outwnd_print( "general", "==========================================================================\n" );
outwnd_print( "general", "DEBUG SPEW: No debug_filter.cfg found, so only general, error, and warning\n" );
outwnd_print( "general", "categories can be shown and no debug_filter.cfg info will be saved.\n" );
outwnd_print( "general", "==========================================================================\n" );
}
if ( !id )
id = "General";
for (i = 0; i < OutwndFilter.size(); i++) {
if ( !stricmp(id, OutwndFilter[i].name) )
break;
}
// id found that isn't in the filter list yet
if ( i == OutwndFilter.size() ) {
// Only create new filters if there was a filter file
if (Outwnd_no_filter_file)
return;
Assert( strlen(id)+1 < NAME_LENGTH );
outwnd_filter_struct new_filter;
strcpy_s(new_filter.name, id);
new_filter.enabled = true;
OutwndFilter.push_back( new_filter );
save_filter_info();
}
if ( !OutwndFilter[i].enabled )
return;
if (Log_debug_output_to_file) {
if (Log_fp != NULL) {
fputs(tmp, Log_fp);
fflush(Log_fp);
}
} else {
fputs(tmp, stdout);
fflush(stdout);
}
}
void outwnd_init(int display_under_freespace_window)
{
outwnd_inited = true;
char pathname[MAX_PATH_LEN];
snprintf(pathname, MAX_PATH_LEN, "%s/%s/%s/%s", detect_home(), Osreg_user_dir, Pathtypes[CF_TYPE_DATA].path, FreeSpace_logfilename);
if (Log_fp == NULL) {
Log_fp = fopen(pathname, "wb");
if (Log_fp == NULL) {
outwnd_printf("Error", "Error opening %s\n", pathname);
} else {
time_t timedate = time(NULL);
char datestr[50];
memset( datestr, 0, sizeof(datestr) );
strftime( datestr, sizeof(datestr)-1, "%a %b %d %H:%M:%S %Y", localtime(&timedate) );
printf("Future debug output directed to: %s\n", pathname);
outwnd_printf("General", "Opened log '%s', %s ...\n", pathname, datestr);
}
}
}
void outwnd_close()
{
if (Log_fp != NULL) {
time_t timedate = time(NULL);
char datestr[50];
memset( datestr, 0, sizeof(datestr) );
strftime( datestr, sizeof(datestr)-1, "%a %b %d %H:%M:%S %Y", localtime(&timedate) );
outwnd_printf("General", "... Log closed, %s\n", datestr);
fclose(Log_fp);
Log_fp = NULL;
}
outwnd_inited = false;
}
void safe_point_print(const char *format, ...)
{
SCP_string temp;
va_list args;
va_start(args, format);
vsprintf(temp, format, args);
va_end(args);
safe_string = temp;
}
void safe_point(const char *file, int line, const char *format, ...)
{
safe_point_print("last safepoint: %s, %d; [%s]", file, line, format);
}
#endif // NDEBUG
#endif // Goober5000 - #ifndef WIN32
|