b874f07d8ca334654115fccaf6acb76fbb910228
[ctsim.git] / libctsupport / syserror.cpp
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (c) 1983-2001 Kevin Rosenberg
4 **
5 **  $Id: syserror.cpp,v 1.22 2001/02/27 03:59:30 kevin Exp $
6 **
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.
10 **
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.
15 **
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 ******************************************************************************/
20
21 #include <iostream>
22 #include <exception>
23 #include <stdexcept>
24 #include <stdarg.h>
25 #include <ctype.h>
26 #include <string>
27 #include "ct.h"
28
29 #ifdef HAVE_WXWINDOWS
30 #include "../src/ctsim.h"
31 #endif
32
33 /* NAME
34 *   sys_error                   System error handler
35 *
36 * SYNOPSIS
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
42 */
43
44 static int s_reportErrorLevel = ERR_TRACE;      // Set error reporting level 
45
46
47 void sys_error (int severity, const char *msg, ...)
48 {
49   va_list arg;
50   
51   va_start(arg, msg);
52   
53   std::string strOutput;
54   sys_verror (strOutput, severity, msg, arg);
55  
56 #ifdef HAVE_WXWINDOWS
57   if (g_bRunningWXWindows) {
58     if (theApp) {
59       wxCommandEvent eventLog (wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
60       wxString msg (strOutput.c_str());
61       msg += "\n";
62       eventLog.SetString( msg );
63       wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event, thread safe
64     } else {
65       wxMutexGuiEnter();
66       wxLog::OnLog (wxLOG_Message, strOutput.c_str(), time(NULL));
67       wxMutexGuiLeave();
68     }
69   }
70   else
71 #endif
72     std::cout << strOutput << "\n";
73
74   va_end(arg);
75 }
76
77 static int s_nErrorCount = 0;
78 const static int MAX_ERROR_COUNT = 20;
79
80
81 void sys_verror (std::string& strOutput, int severity, const char *msg, va_list arg)
82 {
83   if (severity < s_reportErrorLevel)
84     return;     // ignore error if less than reporting level
85   
86   std::ostringstream os;
87
88   if (severity > ERR_TRACE)
89     s_nErrorCount++;
90
91   if (severity != ERR_FATAL) {
92     if (s_nErrorCount > MAX_ERROR_COUNT)
93       return;
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";
97       os << "***                                                           ***\n";
98       os << "***           No further errors will be reported              ***\n";
99       os << "*****************************************************************\n";
100       strOutput = os.str();
101       return;
102     }
103   }
104   
105   switch (severity) {
106   case ERR_FATAL:
107     os << "FATAL ERROR: ";
108     break;
109   case ERR_SEVERE:
110     os << "SEVERE ERROR: ";
111     break;
112   case ERR_WARNING:
113     os << "WARNING ERROR: ";
114     break;
115   case ERR_TRACE:
116     os << "Trace: ";
117     break;
118   default:
119     os << "Illegal error severity #" << severity << ": ";
120   }
121   
122   char errStr[2000];
123   
124 #if HAVE_VSNPRINTF
125   vsnprintf (errStr, sizeof(errStr), msg, arg);
126 #elif HAVE_VSPRINTF
127   vsprintf (errStr, msg, arg);
128 #else
129   strncpy (errStr, sizeof(errStr), "Error message not available on this platform.");
130 #endif
131   
132   os << errStr;
133   strOutput = os.str();
134   
135   if (severity == ERR_FATAL) {
136     std::cerr << strOutput << "\n";
137     throw std::runtime_error (strOutput); 
138   }
139   
140 #if INTERACTIVE_ERROR_DISPLAY
141   std::cout << "A - Abort  C - Continue  W - Turn off warnings? ";
142   //  fflush(stderr);
143   do 
144   {
145     int c = cio_kb_waitc("AaBbCcWw", TRUE);       /* get code from keyboard */
146     c = tolower (c);
147     fputc (c, stderr);
148     fputc (NEWLINE, stderr);
149     
150     if (c == 'a')
151       exit (1);
152     else if (c == 'c')
153       return;
154     else if (c == 'w') 
155     {
156       sys_error_level (ERR_SEVERE);     /* report severe & fatal errors */
157       break;
158     }
159   } while (TRUE);
160 #endif
161 }
162
163
164 /* NAME
165 *   sys_error_level                     Set error reporting level
166 *
167 * SYNOPSIS
168 *   sys_error_level (severity)
169 *   int severity                Report all error as serious as severity and beyond
170 *
171 * DESCRIPTION
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
175 */
176
177 void 
178 sys_error_level (int severity)
179 {
180   if (severity == ERR_FATAL ||
181     severity == ERR_SEVERE ||
182     severity == ERR_WARNING ||
183     severity == ERR_TRACE)
184     s_reportErrorLevel = severity;
185   else
186     s_reportErrorLevel = ERR_WARNING;
187 }
188