7.1
general documentation
bft_error.h File Reference
#include "cs_defs.h"
#include <stdarg.h>
+ Include dependency graph for bft_error.h:

Go to the source code of this file.

Typedefs

typedef void() bft_error_handler_t(const char *const file_name, const int line_num, const int sys_error_code, const char *const format, va_list arg_ptr)
 Function pointer to opaque error handler. More...
 

Functions

void bft_error (const char *const file_name, const int line_num, const int sys_error_code, const char *const format,...)
 Calls the error handler (set by bft_error_handler_set() or default). More...
 
bft_error_handler_tbft_error_handler_get (void)
 Returns the error handler associated with the bft_error() function. More...
 
void bft_error_handler_set (bft_error_handler_t *const handler)
 Associates an error handler with the bft_error() function. More...
 

Typedef Documentation

◆ bft_error_handler_t

bft_error_handler_t

Function pointer to opaque error handler.

Parameters
[in]file_namename of source file from which error handler called.
[in]line_numline of source file from which error handler called.
[in]sys_error_codeerror code if error in system or libc call, 0 otherwise.
[in]formatformat string, as printf() and family.
[in,out]arg_ptrpointer to variable argument list based on format string.

In an MPI environment, it is recommended to replace the default error handler. This requires using the following headers:

#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(HAVE_MPI)
# include <mpi.h>
#endif
#include "bft_intl.h"

An error handler function similar to the BFT default with MPI-awareness added looks like:

void
my_error_handler(const char *const file_name,
const int line_num,
const int sys_error_code,
const char *const format,
const va_list arg_ptr)
{
fprintf(stderr, "\n");
if (sys_error_code != 0)
fprintf(stderr, _("\nSystem error: %s\n"), strerror(sys_error_code));
fprintf(stderr, _("\n%s:%d: Fatal error.\n\n"), file_name, line_num);
vfprintf(stderr, format, arg_ptr);
fprintf(stderr, "\n\n");
assert(0); /* Use assert to avoit exiting under debugger */
#if defined(HAVE_MPI)
{
int mpi_flag;
MPI_Initialized(&mpi_flag);
if (mpi_flag != 0) {
MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
}
}
#endif /* HAVE_MPI */
exit(EXIT_FAILURE);
}

In a more complex environment, MPI_COMM_WORLD could be replaced by another communicator.

Function Documentation

◆ bft_error()

void bft_error ( const char *const  file_name,
const int  line_num,
const int  sys_error_code,
const char *const  format,
  ... 
)

Calls the error handler (set by bft_error_handler_set() or default).

With the default error handler, bft_print_flush() is called, an error message is output to stderr, and the current process exits with an EXIT_FAILURE code.

Parameters
[in]file_namename of source file from which error handler called.
[in]line_numline of source file from which error handler called.
[in]sys_error_codeerror code if error in system or libc call, 0 otherwise.
[in]formatformat string, as printf() and family.
[in]...variable arguments based on format string.

◆ bft_error_handler_get()

bft_error_handler_t* bft_error_handler_get ( void  )

Returns the error handler associated with the bft_error() function.

Returns
pointer to the error handler function.

◆ bft_error_handler_set()

void bft_error_handler_set ( bft_error_handler_t *const  handler)

Associates an error handler with the bft_error() function.

Parameters
[in]handlerpointer to the error handler function.