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