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