r101: *** 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.2 2000/06/19 17:58:20 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 "kstddef.h"
27 #include "cio.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 errorlevel = 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   sys_verror (severity, msg, arg);
51
52   va_end(arg);
53 }
54
55 static int nErrorCount = 0;
56 const static int MAX_ERROR_COUNT = 20;
57
58
59 void sys_verror (int severity, const char *msg, va_list arg)
60 {
61   if (severity < errorlevel)
62     return;                     /* ignore error if less than max level */
63
64   nErrorCount++;
65   if (severity != ERR_FATAL) {
66     if (nErrorCount > MAX_ERROR_COUNT)
67       return;
68     else if (nErrorCount == MAX_ERROR_COUNT) {
69       cout << "*****************************************************************" << endl;
70       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    ***" << endl;
71       cout << "***                                                           ***" << endl;
72       cout << "***           No further errors will be reported              ***" << endl;
73       cout << "*****************************************************************" << endl;
74       return;
75     }
76   }
77     
78   switch (severity) {
79   case ERR_FATAL:
80     cout << "FATAL ERROR: ";
81     break;
82   case ERR_SEVERE:
83     cout << "SEVERE ERROR: ";
84     break;
85   case ERR_WARNING:
86     cout << "WARNING ERROR: ";
87     break;
88   default:
89     sys_error (ERR_FATAL, "illegal error code #%d  [sys_error]", severity);
90   }
91   
92   char errStr[512];
93   vsnprintf (errStr, sizeof(errStr), msg, arg);
94   cout << errStr << endl;
95   
96   if (severity == ERR_FATAL)
97     throw runtime_error (errStr);
98   
99 #if INTERACTIVE_ERROR_DISPLAY
100   cout << "A - Abort  C - Continue  W - Turn off warnings? ";
101   //  fflush(stderr);
102   do 
103    {
104       int c = cio_kb_waitc("AaBbCcWw", TRUE);     /* get code from keyboard */
105       c = tolower (c);
106       fputc (c, stderr);
107       fputc (NEWLINE, stderr);
108       
109       if (c == 'a')
110         exit (1);
111       else if (c == 'c')
112         return;
113       else if (c == 'w') 
114         {
115           sys_error_level (ERR_SEVERE); /* report severe & fatal errors */
116           break;
117         }
118     } while (TRUE);
119 #endif
120 }
121
122
123 /* NAME
124  *   sys_error_level                    Set error reporting level
125  *
126  * SYNOPSIS
127  *   sys_error_level (severity)
128  *   int severity               Report all error as serious as severity and beyond
129  *
130  * DESCRIPTION
131  *   Causes the system to ignore all error below the level of severity
132  *   For example, if severity == ERR_SEVERE, then report severe & fatal
133  *   error and ignore warnings
134  */
135
136 void 
137 sys_error_level (int severity)
138 {
139   if (severity == ERR_FATAL ||
140       severity == ERR_SEVERE ||
141       severity == ERR_WARNING)
142     errorlevel = severity;
143   else
144     errorlevel = ERR_WARNING;
145 }
146