1 /*****************************************************************************
2 ** This is part of the CTSim program
3 ** Copyright (c) 1983-2009 Kevin Rosenberg
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License (version 2) as
7 ** published by the Free Software Foundation.
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ** GNU General Public License for more details.
14 ** You should have received a copy of the GNU General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 ******************************************************************************/
28 #include "../src/ctsim.h"
33 * sys_error System error handler
36 * sys_error (severity, msg, args . . .)
37 * int severity Severity of error
38 * char *msg Error message
39 * args Argument list, direct transfer to printf stack
42 static int s_reportErrorLevel = ERR_TRACE; // Set error reporting level
45 void sys_error (int severity, const char *msg, ...)
51 std::string strOutput;
52 sys_verror (strOutput, severity, msg, arg);
55 if (g_bRunningWXWindows) {
57 wxCommandEvent eventLog (wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
58 wxString msg (wxConvCurrent->cMB2WX(strOutput.c_str()));
59 if (msg.length() > 0) {
61 eventLog.SetString( msg );
62 wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event, thread safe
66 wxLog::OnLog (wxLOG_Message, wxConvCurrent->cMB2WX(strOutput.c_str()), time(NULL));
71 std::cout << strOutput << "\n";
73 std::cout << strOutput << "\n";
79 static unsigned long s_nErrorCount = 0;
80 unsigned long int g_lSysErrorMaxCount = 2000;
83 void sys_verror (std::string& strOutput, int severity, const char *msg, va_list arg)
85 if (severity < s_reportErrorLevel)
86 return; // ignore error if less than reporting level
88 std::ostringstream os;
90 if (severity > ERR_TRACE)
93 if (severity != ERR_FATAL) {
94 if (s_nErrorCount > g_lSysErrorMaxCount)
96 else if (s_nErrorCount == g_lSysErrorMaxCount) {
97 os << "*****************************************************************\n";
98 os << "*** M A X I M U M E R R O R C O U N T R E A C H E D ***\n";
100 os << "*** No further errors will be reported ***\n";
101 os << "*****************************************************************\n";
102 strOutput = os.str();
109 os << "FATAL ERROR: ";
112 os << "SEVERE ERROR: ";
115 os << "WARNING ERROR: ";
121 os << "Illegal error severity #" << severity << ": ";
127 vsnprintf (errStr, sizeof(errStr), msg, arg);
129 vsprintf (errStr, msg, arg);
131 strncpy (errStr, sizeof(errStr), "Error message not available on this platform.");
135 strOutput = os.str();
137 if (severity == ERR_FATAL) {
138 std::cerr << strOutput << "\n";
139 throw std::runtime_error (strOutput);
142 #if INTERACTIVE_ERROR_DISPLAY
143 std::cout << "A - Abort C - Continue W - Turn off warnings? ";
147 int c = cio_kb_waitc("AaBbCcWw", TRUE); /* get code from keyboard */
150 fputc (NEWLINE, stderr);
158 sys_error_level (ERR_SEVERE); /* report severe & fatal errors */
167 * sys_error_level Set error reporting level
170 * sys_error_level (severity)
171 * int severity Report all error as serious as severity and beyond
174 * Causes the system to ignore all error below the level of severity
175 * For example, if severity == ERR_SEVERE, then report severe & fatal
176 * error and ignore warnings
180 sys_error_level (int severity)
182 if (severity == ERR_FATAL ||
183 severity == ERR_SEVERE ||
184 severity == ERR_WARNING ||
185 severity == ERR_TRACE)
186 s_reportErrorLevel = severity;
188 s_reportErrorLevel = ERR_WARNING;