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