File: outwnd.cpp

package info (click to toggle)
freespace2 24.0.2%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: trixie
  • size: 43,188 kB
  • sloc: cpp: 583,107; ansic: 21,729; python: 1,174; sh: 464; makefile: 248; xml: 181
file content (338 lines) | stat: -rw-r--r-- 8,058 bytes parent folder | download | duplicates (3)
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
 * 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.
 *
*/

#include <cstdio>
#include <cstdarg>
#include <cstring>
#include <algorithm>

#ifdef WIN32
#include <direct.h>
#endif

#include "osapi/DebugWindow.h"
#include "osapi/osapi.h"
#include "osapi/outwnd.h"
#include "graphics/2d.h"
#include "freespaceresource.h"
#include "globalincs/systemvars.h"
#include "cfile/cfilesystem.h"
#include "parse/parselo.h"

static const char *FILTERS_ENABLED_BY_DEFAULT[] =
{
	"error",
	"warning",
	"general",
	"scripting"
};

struct outwnd_filter_struct {
	char name[NAME_LENGTH];
	bool enabled;
};

// This technique is necessary to avoid a crash in FRED.  See commit e624f6c1.
static SCP_vector<outwnd_filter_struct>& filter_vector()
{
	static SCP_vector<outwnd_filter_struct> vec;
	return vec;
}

// used for file logging
bool Log_debug_output_to_file = true;

void outwnd_print(const char* id = nullptr, const char* temp = nullptr);

// 0 = .cfg file found, 1 = not found and warning not printed yet, 2 = not found and warning printed
static ubyte Outwnd_no_filter_file = 0;

static ubyte outwnd_filter_loaded = 0;
static bool outwnd_inited         = false;

static FILE* Log_fp                      = nullptr;
static bool Log_close_fp                 = false;
static const char* FreeSpace_logfilename = nullptr;

static std::unique_ptr<osapi::DebugWindow> debugWindow;

void load_filter_info()
{
	FILE* fp;
	char pathname[MAX_PATH_LEN];
	char inbuf[NAME_LENGTH + 4];
	outwnd_filter_struct* filter_ptr;

	// add default-enabled filters
	for (const char *name : FILTERS_ENABLED_BY_DEFAULT) {
		filter_vector().emplace_back();
		filter_ptr = &filter_vector().back();

		strcpy_s(filter_ptr->name, name);
		filter_ptr->enabled = true;
	}

	outwnd_filter_loaded = 1;

	memset(pathname, 0, sizeof(pathname));
	snprintf(pathname, MAX_PATH_LEN, "%s/%s", Pathtypes[CF_TYPE_DATA].path, NOX("debug_filter.cfg"));

	fp = fopen(os_get_config_path(pathname).c_str(), "rt");

	if (!fp) {
		Outwnd_no_filter_file = 1;
		return;
	}

	Outwnd_no_filter_file = 0;

	while ( fgets(inbuf, NAME_LENGTH+3, fp) ) {
		bool enabled;

		if (*inbuf == '+')
			enabled = true;
		else if (*inbuf == '-')
			enabled = false;
		else
			continue;	// skip everything else

		auto z = strlen(inbuf) - 1;
		if (inbuf[z] == '\n')
			inbuf[z] = 0;

		Assert( strlen(inbuf+1) < NAME_LENGTH );
		const char *name = &inbuf[1];

		// find it, or create it
		filter_ptr = nullptr;
		for (auto &filter : filter_vector()) {
			if (!stricmp(filter.name, name)) {
				filter_ptr = &filter;
				break;
			}
		}
		if (filter_ptr == nullptr) {
			filter_vector().emplace_back();
			filter_ptr = &filter_vector().back();
			strcpy_s(filter_ptr->name, name);
		}

		// set it as enabled or not, depending on config
		filter_ptr->enabled = enabled;
	}

	if ( ferror(fp) && !feof(fp) )
		nprintf(("Error", "Error reading \"%s\"\n", pathname));

	fclose(fp);
}

void save_filter_info()
{
	FILE* fp;
	char pathname[MAX_PATH_LEN];

	if (!outwnd_filter_loaded)
		return;

	if (Outwnd_no_filter_file)
		return; // No file, don't save

	memset(pathname, 0, sizeof(pathname));
	snprintf(pathname, MAX_PATH_LEN, "%s/%s", Pathtypes[CF_TYPE_DATA].path, NOX("debug_filter.cfg"));

	fp = fopen(os_get_config_path(pathname).c_str(), "wt");

	if (fp) {
		for (auto& i : filter_vector()) {
			fprintf(fp, "%c%s\n", i.enabled ? '+' : '-', i.name);
		}

		fclose(fp);
	}
}

void outwnd_printf2(const char *format, ...)
{
	SCP_string temp;
	va_list args;

	if (format == nullptr)
		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 == nullptr) || (format == nullptr))
		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)
{
	if (running_unittests) {
		// Ignore all messages when running unit tests
		return;
	}

	if ((id == nullptr) || (tmp == nullptr))
		return;

	if (!outwnd_inited)
		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, warning and\n");
		outwnd_print("general", "scripting categories can be shown and no debug_filter.cfg info will be saved.\n");
		outwnd_print("general", "==========================================================================\n");
	}

	auto filter = std::find_if(filter_vector().begin(), filter_vector().end(), [&id] (const outwnd_filter_struct& f) { return stricmp(f.name, id) == 0; });

	// id found that isn't in the filter list yet
	if ( filter == filter_vector().end() ) {
		// 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;
		new_filter.enabled = false;
		strcpy_s(new_filter.name, id);

		for (const char *name : FILTERS_ENABLED_BY_DEFAULT){
			if (stricmp(new_filter.name, name) == 0)
			{
				new_filter.enabled = true;
				break;
			}
		}
		filter_vector().push_back( new_filter );
		save_filter_info();
	}
	else if (!filter->enabled)
			return;

	if (Log_debug_output_to_file) {
		if (Log_fp != nullptr) {
			fputs(tmp, Log_fp);
			fflush(Log_fp);
		}
	}

	if (debugWindow) {
		debugWindow->addDebugMessage(id, tmp);
	}
}

extern const char* Osapi_legacy_mode_reason; // This was not exported in a header since it's not intended for general use

void outwnd_init()
{
	if (outwnd_inited)
		return;

	if (!running_unittests && Log_fp == nullptr) {
		SCP_string logpath;
		if (!Cmdline_log_to_stdout) {
			char pathname[MAX_PATH_LEN];

			/* Set where the log file is going to go */
			// Zacam: Set various conditions based on what type of log to generate.
			if (Fred_running) {
				FreeSpace_logfilename = "fred2_open.log";
			} else if (Is_standalone) {
				FreeSpace_logfilename = "fs2_standalone.log";
			} else {
				FreeSpace_logfilename = "fs2_open.log";
			}

			// create data file path if it does not exist
			_mkdir(os_get_config_path(Pathtypes[CF_TYPE_DATA].path).c_str());

			memset(pathname, 0, sizeof(pathname));
			snprintf(pathname, MAX_PATH_LEN, "%s/%s", Pathtypes[CF_TYPE_DATA].path, FreeSpace_logfilename);

			logpath = os_get_config_path(pathname);

			Log_fp       = fopen(logpath.c_str(), "wb");
			Log_close_fp = true;
		} else {
			Log_fp       = stdout;
			Log_close_fp = false;

			logpath = "<stdout>";
		}

		outwnd_inited = Log_fp != nullptr;

		if (Log_fp == nullptr) {
			fprintf(stderr, "Error opening %s\n", logpath.c_str());
		} else {
			time_t timedate = time(nullptr);
			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", "Opened log '%s', %s ...\n", logpath.c_str(), datestr);
			mprintf(("Legacy config mode is %s.\nReason: %s\n", os_is_legacy_mode() ? "ENABLED" : "DISABLED",
					 Osapi_legacy_mode_reason));
		}
	}
}

void outwnd_close()
{
	if (!running_unittests && Log_fp != nullptr) {
		time_t timedate = time(nullptr);
		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);

		if (Log_close_fp) {
			fclose(Log_fp);
		}
		Log_fp       = nullptr;
		Log_close_fp = false;
	}

	outwnd_inited = false;
}

void outwnd_debug_window_init() {
	debugWindow.reset(new osapi::DebugWindow());
}
void outwnd_debug_window_do_frame(float frametime) {
	debugWindow->doFrame(frametime);
}
void outwnd_debug_window_deinit() {
	debugWindow.reset();
}