File: textconsole.cpp

package info (click to toggle)
scummvm 2.7.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 363,784 kB
  • sloc: cpp: 3,622,060; asm: 27,410; python: 10,528; sh: 10,241; xml: 6,752; java: 5,579; perl: 2,570; yacc: 1,635; javascript: 1,016; lex: 539; makefile: 398; ansic: 378; awk: 275; objc: 82; sed: 11; php: 1
file content (114 lines) | stat: -rw-r--r-- 3,022 bytes parent folder | download
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#define FORBIDDEN_SYMBOL_EXCEPTION_exit

#include "common/textconsole.h"
#include "common/system.h"
#include "common/str.h"

namespace Common {

static OutputFormatter s_errorOutputFormatter = nullptr;

void setErrorOutputFormatter(OutputFormatter f) {
	s_errorOutputFormatter = f;
}

static ErrorHandler s_errorHandler = nullptr;

void setErrorHandler(ErrorHandler handler) {
	s_errorHandler = handler;
}


} // End of namespace Common


#ifndef DISABLE_TEXT_CONSOLE

void warning(const char *s, ...) {
	Common::String output;
	va_list va;

	va_start(va, s);
	output = Common::String::vformat(s, va);
	va_end(va);

	output = "WARNING: " + output + "!\n";

	if (g_system)
		g_system->logMessage(LogMessageType::kWarning, output.c_str());
	// TODO: Think of a good fallback in case we do not have
	// any OSystem yet.
}

#endif

void NORETURN_PRE error(const char *s, ...) {
	// We don't use String::vformat here, as that require
	// using the heap, and that might be impossible at this
	// point, e.g. if the error was an "out-of-memory" error.
	char buf_input[STRINGBUFLEN];
	char buf_output[STRINGBUFLEN];
	va_list va;

	// Generate the full error message
	va_start(va, s);
	vsnprintf(buf_input, STRINGBUFLEN, s, va);
	va_end(va);


	// Next, give the active engine (if any) a chance to augment the message
	if (Common::s_errorOutputFormatter) {
		(*Common::s_errorOutputFormatter)(buf_output, buf_input, STRINGBUFLEN);
	} else {
		strncpy(buf_output, buf_input, STRINGBUFLEN);
	}

	buf_output[STRINGBUFLEN - 3] = '\0';
	buf_output[STRINGBUFLEN - 2] = '\0';
	buf_output[STRINGBUFLEN - 1] = '\0';
	Common::strcat_s(buf_output, "!\n");

	if (g_system)
		g_system->logMessage(LogMessageType::kError, buf_output);
	// TODO: Think of a good fallback in case we do not have
	// any OSystem yet.

	// If there is an error handler, invoke it now
	bool handled = false;
	if (Common::s_errorHandler)
		handled = (*Common::s_errorHandler)(buf_output);

	if (!handled && g_system)
		g_system->messageBox(LogMessageType::kError, buf_output);

	if (g_system)
		g_system->fatalError();

#if defined(SAMSUNGTV)
	// FIXME
	for (;;) {}
#else
	exit(1);
#endif
}