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