1 /*============================================================================
2  * BFT Examples
3  *============================================================================*/
4 
5 /*
6   This file is part of Code_Saturne, a general-purpose CFD tool.
7 
8   Copyright (C) 1998-2021 EDF S.A.
9 
10   This program is free software; you can redistribute it and/or modify it under
11   the terms of the GNU General Public License as published by the Free Software
12   Foundation; either version 2 of the License, or (at your option) any later
13   version.
14 
15   This program is distributed in the hope that it will be useful, but WITHOUT
16   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17   FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
18   details.
19 
20   You should have received a copy of the GNU General Public License along with
21   this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
22   Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 */
24 
25 /*-----------------------------------------------------------------------------*/
26 
27 /*
28  * Standard C library and BFT headers
29  */
30 
31 /*! [my_error_handler_headers] */
32 #include <assert.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 
38 #if defined(HAVE_MPI)
39 # include <mpi.h>
40 #endif
41 
42 #include "bft_intl.h"
43 /*! [my_error_handler_headers] */
44 #include "bft_error.h"
45 
46 /*-----------------------------------------------------------------------------*/
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #if 0
51 } /* Fake brace to force back Emacs auto-indentation back to column 0 */
52 #endif
53 #endif /* __cplusplus */
54 
55 /*============================================================================
56  * Public function definitions
57  *============================================================================*/
58 
59 /*
60  * MPI-aware error handler.
61  *
62  * An error message is output to stderr (after bft_print_flush() is called),
63  * and the current process (or process group) is terminated.
64  *
65  * parameters:
66  *   file_name:      --> name of source file from which error handler called.
67  *   line_num:       --> line of source file from which error handler called.
68  *   sys_error_code: --> error code if error in system or libc call, 0 otherwise.
69  *   format:         --> format string, as printf() and family.
70  *   arg_ptr:        --> variable argument list based on format string.
71  */
72 
73 /*! [my_error_handler_body] */
74 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)75 my_error_handler(const char     *const file_name,
76                  const int             line_num,
77                  const int             sys_error_code,
78                  const char     *const format,
79                  const va_list         arg_ptr)
80 {
81   bft_printf_flush();
82 
83   fprintf(stderr, "\n");
84 
85   if (sys_error_code != 0)
86     fprintf(stderr, _("\nSystem error: %s\n"), strerror(sys_error_code));
87 
88   fprintf(stderr, _("\n%s:%d: Fatal error.\n\n"), file_name, line_num);
89 
90   vfprintf(stderr, format, arg_ptr);
91 
92   fprintf(stderr, "\n\n");
93 
94   assert(0);   /* Use assert to avoit exiting under debugger */
95 
96 #if defined(HAVE_MPI)
97   {
98     int mpi_flag;
99 
100     MPI_Initialized(&mpi_flag);
101 
102     if (mpi_flag != 0) {
103       MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
104     }
105   }
106 #endif /* HAVE_MPI */
107 
108   exit(EXIT_FAILURE);
109 }
110 /*! [my_error_handler_body] */
111 
112 /*-----------------------------------------------------------------------------*/
113 
114 #ifdef __cplusplus
115 }
116 #endif /* __cplusplus */
117