diff options
author | jthorn <jthorn@f88db872-0e4f-0410-b76b-b9085cfa78c5> | 2002-09-29 23:05:18 +0000 |
---|---|---|
committer | jthorn <jthorn@f88db872-0e4f-0410-b76b-b9085cfa78c5> | 2002-09-29 23:05:18 +0000 |
commit | 9b1661fa6b260c89738e4be936b525fff4ee86aa (patch) | |
tree | e0d7e249af1b76e951c844af8669b5ec9b3dc192 /src/jtutil | |
parent | e6c8a049f33e648fbf83522636141c99b14c0d9d (diff) |
redo my error_exit() routine so that
#ifdef STANDALONE_TEST
it's just what it used to be -- prints a msg to stderr and then
does an exit() or abort()
#else
it formats the message into a buffer, removes the trailing '\n'
if any, and then calls CCTK_VWarn() to print the msg andcleanly
terminate the Cactus run
#endif
Modified Files:
make.code.defn makefile
Added Files:
error_exit.cc test_error_exit.cc
Removed Files:
error_exit.c
git-svn-id: http://svn.einsteintoolkit.org/cactus/EinsteinAnalysis/AHFinderDirect/trunk@781 f88db872-0e4f-0410-b76b-b9085cfa78c5
Diffstat (limited to 'src/jtutil')
-rw-r--r-- | src/jtutil/error_exit.c | 48 | ||||
-rw-r--r-- | src/jtutil/error_exit.cc | 81 | ||||
-rw-r--r-- | src/jtutil/make.code.defn | 2 | ||||
-rw-r--r-- | src/jtutil/makefile | 6 | ||||
-rw-r--r-- | src/jtutil/test_error_exit.cc | 13 |
5 files changed, 99 insertions, 51 deletions
diff --git a/src/jtutil/error_exit.c b/src/jtutil/error_exit.c deleted file mode 100644 index b93e0cf..0000000 --- a/src/jtutil/error_exit.c +++ /dev/null @@ -1,48 +0,0 @@ -/* error_exit -- print an error message and exit */ -/* $Header$ */ -/* - * error_exit -- print an error message and exit - */ - -#include <stdio.h> -#include <stdarg.h> -#include <stdlib.h> - -#include "stdc.h" - -/*****************************************************************************/ - -/* - * This function prints an error message (formatted via vfprintf(3S)), - * then exits. It does *not* return to its caller. Normally the exit - * is done via exit(2), but optionally it may be done via abort(2) to - * produce a core dump. Despite not actually returning, this function - * is declared as returning an int , so it may be easily used in - * conditional expressions like - * foo = bar_ok ? baz(bar) : error_exit(...); - * - * Usage: - * error_exit(exit_code, message, args...) - * - * ***** THIS VERSION IS FOR ANSI/ISO C ***** - * - * Arguments: - * exit_code = (in) Exit code for exit(2), or PANIC_EXIT (defined in - * <jt/stdc.h>) to abort(2) instead. - * format = (in) vprintf(3S) format string for error message to be printed. - * args... = (in) Any additional arguments are (presumably) used in formatting - * the error message string. - */ -/*VARARGS*/ -int error_exit(const int exit_code, const char *const format, ...) -{ -va_list ap; - -va_start(ap, format); -vfprintf(stderr, format, ap); -va_end(ap); - -if (exit_code == PANIC_EXIT) - then abort(); /*NOTREACHED*/ - else exit(exit_code); /*NOTREACHED*/ -} diff --git a/src/jtutil/error_exit.cc b/src/jtutil/error_exit.cc new file mode 100644 index 0000000..bb2739f --- /dev/null +++ b/src/jtutil/error_exit.cc @@ -0,0 +1,81 @@ +// error_exit.cc -- print an error message and exit (= CCTK_VWarn() wrapper) +// $Header$ +// +// error_exit -- print an error message and exit (= CCTK_VWarn() wrapper) +// + +#include <stdio.h> +#include <stdarg.h> +#include <stdlib.h> +#include <string.h> + +#ifdef STANDALONE_TEST + #include "fake_cctk.h" +#else + #include "cctk.h" +#endif + +#include "config.h" +#include "stdc.h" + +//****************************************************************************** + +// +// This function prints an error message (formatted via vfprintf(3S)), +// then exits. It does *not* return to its caller. +#ifdef STANDALONE_TEST +// The exit is done via either exit() (for ERROR_EXIT) or abort() +// (for PANIC_EXIT). +#else +// The exit is done via CCTK_VWarn() with the caller-supplied +// "message level" argument; if this is one of +// ERROR_EXIT +// PANIC_EXIT +// defined in "stdc.h", then the CCTK_VWarn() call itself will terminate +// the Cactus run; if not we do an abort() . +#endif +// +// Despite not actually returning, this function is declared as returning +// an int , so it may be easily used in conditional expressions like +// foo = bar_ok ? baz(bar) : error_exit(...); +// +// Usage: +// error_exit(exit_code, message, args...) +// +// Arguments: +// msg_level = (in) The "message level" for CCTK_VWarn() . +// format = (in) vprintf(3S) format string for error message to be printed. +// args... = (in) Any additional arguments are (presumably) used in formatting +// the error message string. +// +extern "C" +/*VARARGS*/ +int error_exit(const int msg_level, const char *format, ...) +{ +const int N_buffer = 2000; +char buffer[N_buffer]; + +va_list ap; +va_start(ap, format); +// FIXME: We should really do something if msg was truncated due to +// overflowing the buffer. But what to do? +vsnprintf(buffer, N_buffer, format, ap); +va_end(ap); + +// delete trailing '\n' if present, since CCTK_VWarn() doesn't like this +const int len = strlen(buffer); +if ((len > 0) && (buffer[len-1] == '\n')) + then buffer[len-1] = '\0'; + +#ifdef STANDALONE_TEST + fprintf(stderr, "%s\n", buffer); + if (msg_level == PANIC_EXIT) + then abort(); + else exit(msg_level); +#else + CCTK_VWarn(msg_level, __LINE__, __FILE__, CCTK_THORNSTRING, "%s", buffer); +#endif + +// if we got here, evidently msg_level wasn't drastic enough +abort(); /*NOTREACHED*/ +} diff --git a/src/jtutil/make.code.defn b/src/jtutil/make.code.defn index 490a902..a3ecb79 100644 --- a/src/jtutil/make.code.defn +++ b/src/jtutil/make.code.defn @@ -3,7 +3,7 @@ # Source files in this directory SRCS = array.cc cpm_map.cc fuzzy.cc linear_map.cc miscfp.cc norm.cc round.cc \ - error_exit.c + error_exit.cc # Subdirectories containing source files SUBDIRS = diff --git a/src/jtutil/makefile b/src/jtutil/makefile index 2b2c4c6..8a7e0da 100644 --- a/src/jtutil/makefile +++ b/src/jtutil/makefile @@ -1,5 +1,5 @@ # Makefile for standalone test drivers in this directory -# $Header: /usr/local/svn/cvs-repositories/numrelcvs/AEIThorns/AHFinderDirect/src/jtutil/makefile,v 1.6 2002-09-16 14:12:52 jthorn Exp $ +# $Header: /usr/local/svn/cvs-repositories/numrelcvs/AEIThorns/AHFinderDirect/src/jtutil/makefile,v 1.7 2002-09-29 23:05:18 jthorn Exp $ # # CC, CXX = C and C++ compilers. Defaults are gcc and g++ if # variables aren't set from command line or environment. @@ -25,7 +25,8 @@ CXXFLAGS := $(STD_GXX_FLAGS) -I../include -DSTANDALONE_TEST -g ALL_TESTS := test_array test_array2 \ test_cpm_map test_linear_map \ test_fuzzy test_round \ - test_modulo test_norm + test_modulo test_norm \ + test_error_exit ################################################################################ @@ -45,6 +46,7 @@ test_modulo : test_modulo.o miscfp.o \ fuzzy.o round.o error_exit.o -lm test_norm : test_norm.o norm.o \ fuzzy.o round.o -lm +test_error_exit : test_error_exit.o error_exit.o .PHONY : clean clean : diff --git a/src/jtutil/test_error_exit.cc b/src/jtutil/test_error_exit.cc new file mode 100644 index 0000000..a1b568e --- /dev/null +++ b/src/jtutil/test_error_exit.cc @@ -0,0 +1,13 @@ +// test_error_exit.cc -- test driver for error_exit() function +// $Header$ + +#include <stdio.h> + +#include "stdc.h" +using jtutil::error_exit; + +int main() +{ +error_exit(ERROR_EXIT, "two+two=%.3f", 4.0); /*NOTREACHED*/ +return 0; +} |