1 /*****************************************************************************
2 ** This is part of the CTSim program
3 ** Copyright (c) 1983-2001 Kevin Rosenberg
5 ** $Id: syserror.cpp,v 1.22 2001/02/27 03:59:30 kevin Exp $
7 ** This program is free software; you can redistribute it and/or modify
8 ** it under the terms of the GNU General Public License (version 2) as
9 ** published by the Free Software Foundation.
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ******************************************************************************/
30 #include "../src/ctsim.h"
34 * sys_error System error handler
37 * sys_error (severity, msg, args . . .)
38 * int severity Severity of error
39 * char *msg Error message
40 * args Argument list, direct transfer to printf stack
41 * Can take 24 byte transfer
44 static int s_reportErrorLevel = ERR_TRACE; // Set error reporting level
47 void sys_error (int severity, const char *msg, ...)
53 std::string strOutput;
54 sys_verror (strOutput, severity, msg, arg);
57 if (g_bRunningWXWindows) {
59 wxCommandEvent eventLog (wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
60 wxString msg (strOutput.c_str());
62 eventLog.SetString( msg );
63 wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event, thread safe
66 wxLog::OnLog (wxLOG_Message, strOutput.c_str(), time(NULL));
72 std::cout << strOutput << "\n";
77 static int s_nErrorCount = 0;
78 const static int MAX_ERROR_COUNT = 20;
81 void sys_verror (std::string& strOutput, int severity, const char *msg, va_list arg)
83 if (severity < s_reportErrorLevel)
84 return; // ignore error if less than reporting level
86 std::ostringstream os;
88 if (severity > ERR_TRACE)
91 if (severity != ERR_FATAL) {
92 if (s_nErrorCount > MAX_ERROR_COUNT)
94 else if (s_nErrorCount == MAX_ERROR_COUNT) {
95 os << "*****************************************************************\n";
96 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";
98 os << "*** No further errors will be reported ***\n";
99 os << "*****************************************************************\n";
100 strOutput = os.str();
107 os << "FATAL ERROR: ";
110 os << "SEVERE ERROR: ";
113 os << "WARNING ERROR: ";
119 os << "Illegal error severity #" << severity << ": ";
125 vsnprintf (errStr, sizeof(errStr), msg, arg);
127 vsprintf (errStr, msg, arg);
129 strncpy (errStr, sizeof(errStr), "Error message not available on this platform.");
133 strOutput = os.str();
135 if (severity == ERR_FATAL) {
136 std::cerr << strOutput << "\n";
137 throw std::runtime_error (strOutput);
140 #if INTERACTIVE_ERROR_DISPLAY
141 std::cout << "A - Abort C - Continue W - Turn off warnings? ";
145 int c = cio_kb_waitc("AaBbCcWw", TRUE); /* get code from keyboard */
148 fputc (NEWLINE, stderr);
156 sys_error_level (ERR_SEVERE); /* report severe & fatal errors */
165 * sys_error_level Set error reporting level
168 * sys_error_level (severity)
169 * int severity Report all error as serious as severity and beyond
172 * Causes the system to ignore all error below the level of severity
173 * For example, if severity == ERR_SEVERE, then report severe & fatal
174 * error and ignore warnings
178 sys_error_level (int severity)
180 if (severity == ERR_FATAL ||
181 severity == ERR_SEVERE ||
182 severity == ERR_WARNING ||
183 severity == ERR_TRACE)
184 s_reportErrorLevel = severity;
186 s_reportErrorLevel = ERR_WARNING;