/* * The Unix Channel * * by Michel Beaudouin-Lafon * * Copyright 1990-1993 * Laboratoire de Recherche en Informatique (LRI) * * Error management * * $Id$ * $CurLog$ */ #include "error.h" #include #include #include #ifdef sun extern "C" int rename (const char*, const char*); #endif #include #include #include #include "ccu/Time.h" #include #include /*?class UchERROR 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; CcuTimeStamp 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 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 char* sys_errlist []; char* msg; char defmsg [80]; if (! errno || errno == exc1 || errno == exc2) return false; if (errno >= sys_nerr) sprintf (msg = defmsg, "system error code %d", errno); else if (errno == 0) msg = "internal error"; else msg = sys_errlist [errno]; return HandleError (how, who, msg); }