summaryrefslogtreecommitdiff
path: root/comm/error.cc
blob: 103bf9680b0f082ae60062369c423ffb9ccf8762 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 *	Ivy League
 *
 *	Error management
 *
 *	Copyright 1990-2000
 *	Laboratoire de Recherche en Informatique (LRI)
 *	Centre d'Etudes de la Navigation Aerienne (CENA)
 *
 *	original code by Michel Beaudouin-Lafon,
 *	maintained by Stephane Chatty
 *
 *	$Id$
 *
 */

#include "error.h"

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef sun
extern "C" int rename (const char*, const char*);
#endif
#include <fcntl.h>
#include <stdio.h>
#include <sys/file.h>
#include "ivl/Time.h"
#include <sys/types.h>
#include <sys/stat.h>

/*?class IvlERROR
The set of global functions described here are designed to handle errors.
An error has a type, an origin, and a message.
The type defines how the error is handled.
The origin and the message are used to generate the error message.
The origin is usually the function name where the error occurred.
A log file may be defined for storing all error messages, in addition to the normal
notification mechanism.

Handling the error is a three steps process:
first the error string is generated, then the error is emitted by a user-defined error handler,
and finally the way the program continues is determined from the error type.
These types are described by the following values (enumerated type \typ{^{errtype}}):
\index{errtype :: ErrNone}\index{errtype :: ErrLog}\index{errtype :: ErrWarn}
\index{errtype :: ErrAbort}\index{errtype :: ErrExit}\index{errtype :: ErrFatal}
\index{errtype :: ErrUsage}
\begin{itemize}
\item \var{ErrNone} is not an error: the handler is not called and the program continues;
\item \var{ErrLog} is an error that is only logged to the log file: the handler is not called and the program continues;
\item \var{ErrWarn} is a warning: the handler is called and the program then continues;
\item \var{ErrAbort} handles the error and calls \fun{abort};
\item \var{ErrExit} handles the error and calls \fun{exit(0)};
\item \var{ErrFatal} handles the error and calls \fun{exit(1)};
\item \var{ErrUsage} handles the error and calls \fun{exit(1)};
\var{ErrUsage} is different from \var{ErrFatal} in that the generated error message
is not the same (see \fun{MakeErrorString}).
\end{itemize}

The type of an error handler is the following:
\index{ErrorHandler}
\begin{ccode}
typedef errtype (*ErrorHandler) (errtype, const char* who, const char* what, const char* msg);
\end{ccode}
?*/

//	static data for error handling: message table and default handler
//
char* ErrorTable [] = {
	"Subclass should implement virtual member",	// ErrShouldImplement
};

static char ProgName [256], LogFile [256];
static bool LogOn =false;
static int  LogFd = -1;
static off_t LogSize = 0;
static off_t LogHalf = 0;

#define LOG_HALF	25000

static errtype
DefaultHandler (errtype how, const char* /*who*/, const char* /*what*/, const char* msg)
{
	write (2, msg, strlen (msg));
	return how;
}

static ErrorHandler Handler = DefaultHandler;

//	main function for handling errors
//
static bool
HandleError (errtype how, const char* who, const char* what)
{
	if (how == ErrNone)
		return true;
	
	char *msg = MakeErrorString (how, who, what);
	
	if (how >= ErrLog) {
		LogMessage (msg);
		if (how == ErrLog)
			return true;
	}
	
	how = (*Handler) (how, who, what, msg);
	
	switch (how) {
		case ErrNone:
			return true;
		case ErrLog:
		case ErrWarn:
			break;
		case ErrAbort:
			write (2, "aborting\n", 9);
			abort ();
			break;
		case ErrExit:
			exit (0);
			break;
		case ErrUsage:
		case ErrFatal:
			exit (1);
			break;
	}
	return false;
}

//	public functions:
//		register program name, cleanup function, log file
//		change error handler
//		error functions
//

/*?
Set the program name, that is used to label each output message.
This usually called from \fun{main} with \com{argv[0]} as argument.
?*/
void
ProgramName (const char* name)
{
	if (name)		
		strncpy (ProgName, name, sizeof (ProgName) -1);
	else
		ProgName [0] = 0;
}

/*?
Set the log file name.
All messages are appended to the logfile.
If \var{reset} is true, the file is renamed to \var{file$\sim$}.
If this fails, the file is simply cleared.
If the \com{LOGDIR} environment variable is defined, and if \var{file}
does not start with \com{/} or \com{./}, the value of \com{LOGDIR} is
prepended to \var{file}, followed by a slash if necessary.
Otherwise, the directory \com{\$HOME/.log} is used.
?*/
void
LogfileName (const char* file, bool reset)
{
	if (file) {
		if (LogOn && LogFd >= 0)
			close (LogFd);
		if (*file == '/' || strncmp (file, "./", 2) == 0) {
			LogFile [0] = '\0';
		} else {
			// try $LOGDIR
			char* dir = getenv ("LOGDIR");
			if (dir) {
				strcpy (LogFile, dir);
			} else {
				// try $HOME/.log
				char* home = getenv ("HOME");
				if (home) {
					strcpy (LogFile, home);
					strcat (LogFile, "/.log");
					if (access (LogFile, F_OK) != 0) {
						// try to create the directory
						if (mkdir (LogFile, 0755) != 0) {
							Error (ErrWarn, "LogFileName", "logging to /tmp directory");
							strcpy (LogFile, "/tmp");
						}
					}
				}
			}
			if (LogFile [strlen (LogFile) - 1] != '/')
				strcat (LogFile, "/");
		}
			
		strcat (LogFile, file);
		LogOn = true;
		int flags = O_RDWR | O_CREAT;
		if (reset) {
			flags |= O_TRUNC;
			// try to save previous version in LogFile~
			if (access (LogFile, F_OK) == 0) {
				char old [256];
				strcpy (old, LogFile);
				strcat (old, "~");
				rename (LogFile, old);
			}
		}
		LogFd = open (LogFile, flags, 0644);
		if (LogFd < 0) {
			SysError (ErrWarn, "LogfileName");
			LogOn = false;
		} else {
			LogHalf = 0;
			if (reset)
				LogSize = 0;
			else {
				LogSize = lseek (LogFd, 0L, 2 /*SEEK_END*/);
				LogSize += write (LogFd, "\n", 1);
			}
			LogMessage ("-------- logging started --------\n");
		}
	} else {
		if (LogOn && LogFd >= 0)
			close (LogFd);
		LogOn = false;
	}
}

/*?nodoc?*/
void
CleanUp (CleanUpProc)
{
	// to do: register function
}

/*?
Append a message to the logfile, if one has been specified.
?*/
void
LogMessage (const char* msg)
{
	if (! LogOn || LogFd < 0)
		return;

	IvlTimeStamp now;
	// format of time: Sun Sep 16 01:03:52 1973\n\0
	//                 123456789012345678901234
	LogSize += write (LogFd, now.AsString (), 20); // strip year 
	LogSize += write (LogFd, msg, strlen (msg));
	
	// limit the size of the log file
	if (LogSize > LOG_HALF && LogHalf == 0)
		LogHalf = LogSize;
	
	if (LogSize < 2 * LOG_HALF)
		return;
	
	// copy bytes from LogHalf to LogSize to beginning of file
	off_t src, dst;
	int n;
	char buf [4096];
	lseek (LogFd, 0L, 0 /*SEEK_SET*/);
	write (LogFd, "...\n", 4);
	for (src = LogHalf, dst = 4; src < LogSize; src += n, dst += n) {
		lseek (LogFd, src, 0 /*SEEK_SET*/);
		n = read (LogFd, buf, sizeof (buf));
		if (n <= 0)
			break;
		lseek (LogFd, dst, 0 /*SEEK_SET*/);
		write (LogFd, buf, n);
	}
	lseek (LogFd, dst, 0 /*SEEK_SET*/);
//////	ftruncate (LogFd, dst); /////// this seems not to be a POSIX call
	LogHalf = LogSize = dst;
}

/*?
Build an error message from the type of error, the place where the error occurred,
and the error message itself, with the following format:\\
``\com{        [program\_name :] [fatal error in]}\var{who}\com{: }\var{what}''\\
When \var{how} is \var{ErrUsage}, the format is\\
``\com{        usage: program\_name}\var{what}''.\\
The string returned is the address of a static buffer that is overwritten
each time this function is called.
?*/
char*
MakeErrorString (errtype how, const char* who, const char* what)
{
	static char errmsg [1024];
	
	if (how == ErrUsage)
		sprintf (errmsg, "usage: %s %s\n", ProgName, what);
	else
		sprintf (errmsg, "%s%s%s%s: %s\n",
			ProgName,
			ProgName [0] ? ": " : "",
			how >= ErrAbort ? "fatal error in " : "",
			who,
			what);
	return errmsg;
}

/*?
Change the error handler.
The error handler is called each time an error is emitted.
It is called with four arguments:
the error type, the error source, the error message,
and the error string as returned by \fun{MakeErrorString}.
It should return an error type (usually the first argument) that determines
what happens next: nothing, logging to a file, aborting, or exiting.
The default error handler can be reset by passing the argument 0.
The default error handler writes the error string to the standard error,
and returns its first argument.
?*/
ErrorHandler
SetErrorHandler (ErrorHandler h)
{
	ErrorHandler old = Handler;
	if (! h)
		h = DefaultHandler;
	Handler = h;
	return old;
}

/*?
Emit an error of type \var{how}, from function \var{who}, with message \var{what}.
?*/
void
Error (errtype how, const char* who, const char* what)
{
	HandleError (how, who, what);
}

/*?
Emit a system error of type \var{how}, from function \var{who}.
The message is retrieved from the value of the global variable \var{errno}.
Thus, this function should be used after system calls that set \var{errno}.
\var{exc1} and \var{exc2}, if non negative, are error codes (values of \var{errno}) to be ignored:
if the current error code is one of these, the function returns false.
This is useful for instance to ignore interrupted system calls:
\begin{ccode}
 #include <errno.h>
 n = read (...);
 if (n < 0)
     SysError (ErrWarn, "read", EINTR);
\end{ccode}
?*/
bool
SysError (errtype how, const char* who, int exc1, int exc2)
{
	extern int errno;
	extern int sys_nerr;
	extern const char* const sys_errlist [];
	const char* msg;
	char defmsg [80];
	
	if (! errno || errno == exc1 || errno == exc2)
		return false;
	
	if (errno >= sys_nerr) {
		sprintf (defmsg, "system error code %d", errno);
		msg = defmsg;

	} else if (errno == 0)
		msg = "internal error";

	else
		msg = sys_errlist [errno];

	return HandleError (how, who, msg);
}