8721de2e9ecfa668bb43527089254289f43dae7c
[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.9 2000/12/27 20:09:19 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 "ctsupport.h"
27
28
29 /* NAME
30  *   sys_error                  System error handler
31  *
32  * SYNOPSIS
33  *   sys_error (severity, msg, args . . .)
34  *   int severity               Severity of error
35  *   char *msg                  Error message
36  *   args                       Argument list, direct transfer to printf stack
37  *                              Can take 24 byte transfer
38  */
39
40 static int errorlevel = ERR_WARNING;    /* Set error reporting level */
41
42
43 void sys_error (int severity, const char *msg, ...)
44 {
45   va_list arg;
46
47   va_start(arg, msg);
48
49   sys_verror (severity, msg, arg);
50
51   va_end(arg);
52 }
53
54 static int nErrorCount = 0;
55 const static int MAX_ERROR_COUNT = 20;
56
57
58 void sys_verror (int severity, const char *msg, va_list arg)
59 {
60   if (severity < errorlevel)
61     return;     // ignore error if less than max level
62
63   nErrorCount++;
64   if (severity != ERR_FATAL) {
65     if (nErrorCount > MAX_ERROR_COUNT)
66       return;
67     else if (nErrorCount == MAX_ERROR_COUNT) {
68                 std::cout << "*****************************************************************\n";
69                 std::cout << "***   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";
70                 std::cout << "***                                                           ***\n";
71                 std::cout << "***           No further errors will be reported              ***\n";
72                 std::cout << "*****************************************************************\n";
73       return;
74     }
75   }
76     
77   switch (severity) {
78   case ERR_FATAL:
79           std::cout << "FATAL ERROR: ";
80     break;
81   case ERR_SEVERE:
82           std::cout << "SEVERE ERROR: ";
83     break;
84   case ERR_WARNING:
85           std::cout << "WARNING ERROR: ";
86     break;\r
87   case ERR_TRACE:\r
88     std::cout << "Trace: ";\r
89     break;
90   default:
91           std::cout << "Illegal error code #" << severity << ": ";
92   }
93   
94   char errStr[512];\r
95   
96 #if HAVE_VSNPRINTF
97   vsnprintf (errStr, sizeof(errStr), msg, arg);
98 #elif HAVE_VSPRINTF
99   vsprintf (errStr, msg, arg);\r
100 #else\r
101   strncpy (errStr, sizeof(errStr), "Error message not available on this platform.");
102 #endif
103
104   std::cout << errStr << std::endl;
105   
106   if (severity == ERR_FATAL)
107           throw std::runtime_error (errStr);
108   
109 #if INTERACTIVE_ERROR_DISPLAY
110   std::cout << "A - Abort  C - Continue  W - Turn off warnings? ";
111   //  fflush(stderr);
112   do 
113    {
114       int c = cio_kb_waitc("AaBbCcWw", TRUE);     /* get code from keyboard */
115       c = tolower (c);
116       fputc (c, stderr);
117       fputc (NEWLINE, stderr);
118       
119       if (c == 'a')
120         exit (1);
121       else if (c == 'c')
122         return;
123       else if (c == 'w') 
124         {
125           sys_error_level (ERR_SEVERE); /* report severe & fatal errors */
126           break;
127         }
128     } while (TRUE);
129 #endif
130 }
131
132
133 /* NAME
134  *   sys_error_level                    Set error reporting level
135  *
136  * SYNOPSIS
137  *   sys_error_level (severity)
138  *   int severity               Report all error as serious as severity and beyond
139  *
140  * DESCRIPTION
141  *   Causes the system to ignore all error below the level of severity
142  *   For example, if severity == ERR_SEVERE, then report severe & fatal
143  *   error and ignore warnings
144  */
145
146 void 
147 sys_error_level (int severity)
148 {
149   if (severity == ERR_FATAL ||
150       severity == ERR_SEVERE ||
151       severity == ERR_WARNING)
152     errorlevel = severity;
153   else
154     errorlevel = ERR_WARNING;
155 }
156