623e6629d117a76bf8756b1ce855f662be8fcd20
[ctsim.git] / libctsupport / syserror.cpp
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (C) 1983-2000 Kevin Rosenberg
4 **
5 **  $Id: syserror.cpp,v 1.15 2001/01/13 03:51:35 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
30 /* NAME
31 *   sys_error                   System error handler
32 *
33 * SYNOPSIS
34 *   sys_error (severity, msg, args . . .)
35 *   int severity                Severity of error
36 *   char *msg                   Error message
37 *   args                        Argument list, direct transfer to printf stack
38 *                               Can take 24 byte transfer
39 */
40
41 static int s_reportErrorLevel = ERR_WARNING;    // Set error reporting level 
42
43
44 void sys_error (int severity, const char *msg, ...)
45 {
46   va_list arg;
47   
48   va_start(arg, msg);
49   
50   std::string strOutput;
51   sys_verror (strOutput, severity, msg, arg);
52  
53 #ifdef HAVE_WXWINDOWS
54   if (g_bRunningWXWindows)
55     *theApp->getLog() << strOutput.c_str();
56   else
57 #endif
58     std::cout << strOutput;
59
60   va_end(arg);
61 }
62
63 static int s_nErrorCount = 0;
64 const static int MAX_ERROR_COUNT = 20;
65
66
67 void sys_verror (std::string& strOutput, int severity, const char *msg, va_list arg)
68 {
69   if (severity < s_reportErrorLevel)
70     return;     // ignore error if less than reporting level
71   
72   std::ostringstream os;
73
74   s_nErrorCount++;
75   if (severity != ERR_FATAL) {
76     if (s_nErrorCount > MAX_ERROR_COUNT)
77       return;
78     else if (s_nErrorCount == MAX_ERROR_COUNT) {
79       os << "*****************************************************************\n";
80       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";
81       os << "***                                                           ***\n";
82       os << "***           No further errors will be reported              ***\n";
83       os << "*****************************************************************\n";
84       strOutput = os.str();
85       return;
86     }
87   }
88   
89   switch (severity) {
90   case ERR_FATAL:
91     os << "FATAL ERROR: ";
92     break;
93   case ERR_SEVERE:
94     os << "SEVERE ERROR: ";
95     break;
96   case ERR_WARNING:
97     os << "WARNING ERROR: ";
98     break;
99   case ERR_TRACE:
100     os << "Trace: ";
101     break;
102   default:
103     os << "Illegal error severity #" << severity << ": ";
104   }
105   
106   char errStr[2000];
107   
108 #if HAVE_VSNPRINTF
109   vsnprintf (errStr, sizeof(errStr), msg, arg);
110 #elif HAVE_VSPRINTF
111   vsprintf (errStr, msg, arg);
112 #else
113   strncpy (errStr, sizeof(errStr), "Error message not available on this platform.");
114 #endif
115   
116   os << errStr << "\n";
117   strOutput = os.str();
118   
119   if (severity == ERR_FATAL) {
120     std::cerr << strOutput;
121     throw std::runtime_error (strOutput); 
122   }
123   
124 #if INTERACTIVE_ERROR_DISPLAY
125   std::cout << "A - Abort  C - Continue  W - Turn off warnings? ";
126   //  fflush(stderr);
127   do 
128   {
129     int c = cio_kb_waitc("AaBbCcWw", TRUE);       /* get code from keyboard */
130     c = tolower (c);
131     fputc (c, stderr);
132     fputc (NEWLINE, stderr);
133     
134     if (c == 'a')
135       exit (1);
136     else if (c == 'c')
137       return;
138     else if (c == 'w') 
139     {
140       sys_error_level (ERR_SEVERE);     /* report severe & fatal errors */
141       break;
142     }
143   } while (TRUE);
144 #endif
145 }
146
147
148 /* NAME
149 *   sys_error_level                     Set error reporting level
150 *
151 * SYNOPSIS
152 *   sys_error_level (severity)
153 *   int severity                Report all error as serious as severity and beyond
154 *
155 * DESCRIPTION
156 *   Causes the system to ignore all error below the level of severity
157 *   For example, if severity == ERR_SEVERE, then report severe & fatal
158 *   error and ignore warnings
159 */
160
161 void 
162 sys_error_level (int severity)
163 {
164   if (severity == ERR_FATAL ||
165     severity == ERR_SEVERE ||
166     severity == ERR_WARNING ||
167     severity == ERR_TRACE)
168     s_reportErrorLevel = severity;
169   else
170     s_reportErrorLevel = ERR_WARNING;
171 }
172