Commit 259e3bfe authored by jclamber's avatar jclamber

back to r 109

git-svn-id: http://svn.oamp.fr/repos/unsio/trunk@185 ce2cc22f-6084-46ce-a062-084b172ee5dc
parent e1e2e983
/*++
/* NAME
/* error 3
/* SUMMARY
/* diagnostics
/* PACKAGE
/* unproto
/* SYNOPSIS
/* #include "error.h"
/*
/* int errcount;
/*
/* void error(text)
/* char *text;
/*
/* void error_where(path, line, text)
/* char *path;
/* int line;
/* char *text;
/*
/* void fatal(text)
/* char *text;
/* DESCRIPTION
/* The routines in this file print a diagnostic (text). Some also
/* terminate the program. Upon each error*() call, the errcount variable
/* is incremented.
/*
/* error() provides a default context, i.e. the source-file
/* coordinate of the last read token.
/*
/* error_where() allows the caller to explicitly specify context: path
/* is a source-file name, and line is a line number.
/*
/* fatal() is like error() but terminates the program with a non-zero
/* exit status.
/*
/* context is ignored if the line number is zero or if the path
/* is an empty string.
/* AUTHOR(S)
/* Wietse Venema
/* Eindhoven University of Technology
/* Department of Mathematics and Computer Science
/* Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
/* LAST MODIFICATION
/* 92/01/15 21:53:10
/* VERSION/RELEASE
/* 1.2
/*--*/
* ERROR: scream (on stderr) and die (quickly?).
* WARNING: scream (on stderr)
* RECOVER: set a recover() function when error() called
* STOP: exit to system with proper shell status (does not return)
*
* See: dprintf() for a similar teachnique that issues a
* warning message and then continues.
*
* xx-xxx-86 - origal program for Nemo JEB
* xx-xxx-88 - proper <varargs.h>
* 12-sep-90 - first merged NEMO and Starlab program PJT
* 16-nov-90 - no #ifdef MICRO anymore PJT
* 22-mar-91 - ERROR env.var. introduced - via stop() PJT
* 8-jul-91 V1.2 using vfprintf() pjt
* 12-oct-91 - added __BORLANDC__ to the vfprintf() users pjt
* 25-feb-92 made all of them void'd pjt
* 6-apr-93 V1.3 moved stop() in here, plus local data pjt
* 20-sep-93 V1.4 ansi requires > 0 named arguments pjt
* 26-feb-94 V1.5 keep old <varags> code around too pjt
* 21-mar-01 V1.6 removed old <varargs> code, only allow ANSI compilers PJT
* 28-nov-01 V1.7 allow to set an exit level pjt
* 8-dec-01 V1.8 added errno reporting pjt
* 16-jan-01 V1.8a calling abort() will be announced pjt
* 13-feb-03 V1.8b revoking errno reporting pjt
* 06-jun-08 V1.8c nemo_exit wd
* 12-jun-08 V1.8d report MPI proc wd
* 16-sep-08 V1.8e removed nemo_exit (see stdinc.h) wd
*/
#include <stdinc.h>
#include <getparam.h>
#include <stdarg.h>
#include <errno.h>
extern int debug_level; /* see also user interface getparam.c for this */
int error_level = 0; /* yuck, but this needs to be globally visible */
local proc cleanup=NULL; /* if not NULL this points to error cleanup proc */
local int error_count=0;
local int exit_level=0;
local int last_errno=0;
static void report_errno(void)
{
#if 0
/* something wrong here, this thing is lying half the time */
if (errno)
fprintf(stderr,"### Fatal errno %d errmsg=%s\n",
errno, strerror(errno));
#endif
}
/*
* commented out WD 10-09-2008, also in stdinc.h
void errorn(string fmt, ...)
{
error("errorn is sadly not implemented; use debug>0 to get errno messages");
}
*/
/* Start changes WD 12/06/2008 */
extern bool mpi_proc; /* dprintf.c */
extern int mpi_rank; /* dprintf.c */
/* End changes WD 12/06/2008 */
void error(string fmt, ...)
{
va_list ap;
report_errno();
fprintf(stderr,"### Fatal error [%s]: ",getargv0()); /* report name */
/* Start changes WD 12/06/2008 */
if(mpi_proc)
fprintf(stderr,"@%d: ",mpi_rank);
/* End changes WD 12/06/2008 */
va_start(ap, fmt); /* ap starts with string 'fmt' */
#if defined(NEED_DOPRNT)
_doprnt(fmt, ap, stderr); /* print out on stderr */
#else
vfprintf(stderr, fmt, ap); /* print out on stderr */
#endif
if (fmt[strlen(fmt)-1] != '\n') /* be nice if no newline supplied */
fprintf(stderr,"\n"); /* and print it anyhow */
fflush(stderr); /* flush it NOW */
va_end(ap); /* end varargs */
if (cleanup==NULL){ /* if no cleanup set */
if (debug_level>5) { /* produce coredump if requested */
fprintf(stderr,"Now aborting....\n");
fflush(stderr);
abort();
}
if (exit_level)
stop(exit_level);
else
stop(-1); /* close the shop and pass this to parent */
} else { /* else say so : */
fprintf(stderr,"### Recoverable error ....\n");
(*cleanup)(); /* clean up */
} /* and proceed as if nothing happened */
}
static char error_sccsid[] = "@(#) error.c 1.2 92/01/15 21:53:10";
void fatal(string fmt, ...)
{
va_list ap;
report_errno();
fprintf(stderr,"### Fatal error [%s]: ",getargv0()); /* report name */
va_start(ap, fmt); /* ap starts with string 'fmt' */
#if defined(NEED_DOPRNT)
_doprnt(fmt, ap, stderr); /* print out on stderr */
#else
vfprintf(stderr, fmt, ap); /* print out on stderr */
#endif
if (fmt[strlen(fmt)-1] != '\n') /* be nice if no newline supplied */
fprintf(stderr,"\n"); /* and print it anyhow */
fflush(stderr); /* flush it NOW */
va_end(ap); /* end varargs */
fprintf(stderr,"Now aborting....\n");
fflush(stderr);
abort(); /* nasty, but writes a core dump and dies */
}
/* C library */
void warning(string fmt, ...)
{
va_list ap;
#include <stdio.h>
fprintf(stderr,"### Warning [%s]: ",getargv0()); /* report name */
extern void exit();
/* Start changes WD 12/06/2008 */
if(mpi_proc)
fprintf(stderr,"@%d: ",mpi_rank);
/* End changes WD 12/06/2008 */
/* Application-specific stuff */
va_start(ap, fmt); /* ap starts with string 'fmt' */
#include "token.h"
#include "error.h"
#if defined(NEED_DOPRNT)
_doprnt(fmt, ap, stderr); /* print out on stderr */
#else
vfprintf(stderr, fmt, ap); /* print out on stderr */
#endif
int errcount = 0; /* error counter */
if (fmt[strlen(fmt)-1] != '\n') /* be nice if no newline supplied */
fprintf(stderr,"\n"); /* and print it anyhow */
fflush(stderr); /* flush it NOW */
va_end(ap); /* end varargs */
}
/* error - report problem (implicit context) */
void error(text)
char *text;
void recover(proc cl)
{
error_where(in_path, in_line, text);
if (cl)
dprintf(1,"Setting recoverable error\n");
else
dprintf(1,"Resetting recoverable error\n");
cleanup = cl;
}
/* error_where - report problem (explicit context) */
void error_where(path, line, text)
char *path;
int line;
char *text;
void stop(int lev)
{
if (lev<0)
if (error_count++ < error_level) {
warning("[%d/%d] error ignored",error_count,error_level);
return;
}
finiparam();
exit(lev);
/*NOTREACHED*/
}
void set_exit_level(int lev)
{
errcount++;
exit_level = lev;
}
/* Suppress context info if there is none. */
if (line && path[0])
fprintf(stderr, "%s, line %d: ", path, line);
#ifdef TESTBED
string defv[]={
"recover=f\n Try a recoverable error",
"count=1\n Loopcount calling error",
"VERSION=1.5\n 26-feb-94 PJT",
NULL,
};
fprintf(stderr, "%s\n", text);
}
nemo_main()
{
int fun(), n;
/* fatal - report problem and terminate unsuccessfully */
if (getbparam("recover"))
recover( (proc) fun );
n = getiparam("count");
while(n-- > 0)
error("error: foo=%f bar=%d fum=\"%s\"", 3.1415, 32768, "waldo");
}
void fatal(text)
char *text;
fun()
{
error(text);
exit(1);
printf("Fun Fun Fun - a recoverable error occurred\n");
}
#endif
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment