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