Version bump for 5.0-rc1
[alexxy/gromacs.git] / src / gromacs / legacyheaders / gmx_fatal.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2012,2014, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 /*! \file
38  * \brief
39  * Declares fatal error handling and debugging routines for C code.
40  *
41  * \inpublicapi
42  */
43 #ifndef _fatal_h
44 #define _fatal_h
45
46 #include <stdio.h>
47
48 #include "types/simple.h"
49
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53
54 #ifndef __has_feature
55 /** For compatibility with non-clang compilers. */
56 #define __has_feature(x) 0
57 #endif
58
59 /*! \def GMX_ATTRIBUTE_NORETURN
60  * \brief
61  * Indicate that a function is not expected to return.
62  *
63  * \todo
64  * There are functions outside this header that need the same attribute.
65  * This could be moved to a generic header and made it affect also compiler
66  * code generation.
67  */
68 #ifndef GMX_ATTRIBUTE_NORETURN
69 #if __has_feature(attribute_analyzer_noreturn)
70 #define GMX_ATTRIBUTE_NORETURN __attribute__((analyzer_noreturn))
71 #else
72 #define GMX_ATTRIBUTE_NORETURN
73 #endif
74 #endif
75
76 /** Implementation for where(). */
77 void
78 _where(const char *file, int line);
79 /** Prints filename and line to stdlog. */
80 #define where() _where(__FILE__, __LINE__)
81
82 /*! \brief
83  * Fatal error reporting routine for \Gromacs.
84  *
85  * This function prints a fatal error message with a header that contains the
86  * source file and line number of the call, followed by the string specified by
87  * \p fmt and supplied parameters.
88  * If \p fatal_errno is 0, only the message and arguments are printed.
89  * If \p fatal_errno is a legal system errno or -1, a perror()-like message is
90  * printed after the first message; if fatal_errno is -1, the last system errno
91  * will be used.
92  * The format of \p fmt uses printf()-like formatting.
93  *
94  * In case all MPI processes want to stop with the same fatal error,
95  * use gmx_fatal_collective(), declared in gmx_fatal_collective.h,
96  * to avoid having as many error messages as processes.
97  *
98  * The first three parameters can be provided through ::FARGS:
99  * \code
100    gmx_fatal(FARGS, fmt, ...);
101    \endcode
102  */
103 void
104 gmx_fatal(int fatal_errno, const char *file, int line, const char *fmt, ...) GMX_ATTRIBUTE_NORETURN;
105 /** Helper macro to pass first three parameters to gmx_fatal(). */
106 #define FARGS 0, __FILE__, __LINE__
107
108 /** Sets the log file for printing error messages. */
109 void
110 gmx_fatal_set_log_file(FILE *fp);
111
112 /*! \brief
113  * Debug log file.
114  *
115  * Functions can write to this file for debug info.
116  * Before writing to it, it should be checked whether the file is not NULL:
117  * \code
118    if (debug)
119    {
120        fprintf(debug, "%s", "Debug text");
121    }
122    \endcode
123  */
124 extern FILE    *debug;
125 /** Whether extra debugging is enabled. */
126 extern gmx_bool gmx_debug_at;
127
128 /** Initializes debugging variables */
129 void init_debug(const int dbglevel, const char *dbgfile);
130
131 /** Returns TRUE when the program was started in debug mode */
132 gmx_bool bDebugMode(void);
133
134 /*! \brief
135  * Implementation for range_check() and range_check_mesg().
136  *
137  * \p warn_str can be NULL.
138  */
139 void _range_check(int n, int n_min, int n_max, const char *warn_str,
140                   const char *var,
141                   const char *file, int line);
142
143 /*! \brief
144  * Checks that a variable is within a range.
145  *
146  * If \p n is not in range [n_min, n_max), a fatal error is raised.
147  * \p n_min is inclusive, but \p n_max is not.
148  */
149 #define range_check_mesg(n, n_min, n_max, str) _range_check(n, n_min, n_max, str,#n, __FILE__, __LINE__)
150
151 /*! \brief
152  * Checks that a variable is within a range.
153  *
154  * This works as range_check_mesg(), but with a default error message.
155  */
156 #define range_check(n, n_min, n_max) _range_check(n, n_min, n_max, NULL,#n, __FILE__, __LINE__)
157
158 /*! \brief
159  * Returns error message corresponding to a string key.
160  *
161  * This maps the strings used by gmx_error() to actual error messages.
162  * Caller is responsible of freeing the returned string.
163  */
164 char *gmx_strerror(const char *key);
165
166 /** Implementation for gmx_error(). */
167 void _gmx_error(const char *key, const char *msg, const char *file, int line) GMX_ATTRIBUTE_NORETURN;
168 /*! \brief
169  * Alternative fatal error routine with canned messages.
170  *
171  * This works as gmx_fatal(), except that a generic error message is added
172  * based on a string key, and printf-style formatting is not supported.
173  * Should not typically be called directly, but through gmx_bug(), gmx_call()
174  * etc.
175  */
176 #define gmx_error(key, msg) _gmx_error(key, msg, __FILE__, __LINE__)
177
178 /*! \name Fatal error routines for certain types of errors
179  *
180  * These wrap gmx_error() and provide the \p key parameter as one of the
181  * recognized strings.
182  */
183 /*! \{ */
184 #define gmx_bug(msg)    gmx_error("bug", msg)
185 #define gmx_call(msg)   gmx_error("call", msg)
186 #define gmx_comm(msg)   gmx_error("comm", msg)
187 #define gmx_file(msg)   gmx_error("file", msg)
188 #define gmx_cmd(msg)    gmx_error("cmd", msg)
189 #define gmx_impl(msg)   gmx_error("impl", msg)
190 #define gmx_incons(msg) gmx_error("incons", msg)
191 #define gmx_input(msg)  gmx_error("input", msg)
192 #define gmx_mem(msg)    gmx_error("mem", msg)
193 #define gmx_open(fn)    gmx_error("open", fn)
194 /*! \} */
195
196 /*! \brief
197  * Sets an error handler for gmx_fatal() and other fatal error routines.
198  *
199  * The default handler prints the message and aborts the program.
200  * If you set a custom handler, it must also abort the program, otherwise
201  * \Gromacs will behave unpredictably (most likely, it crashes shortly after
202  * the fatal error).
203  * The string passed to the handler may be a multi-line string.
204  *
205  * \see gmx_fatal()
206  */
207 void
208 set_gmx_error_handler(void (*func)(const char *msg));
209
210 /*! \brief
211  * Prints a warning message to stderr.
212  *
213  * The format of \p fmt uses printf()-like formatting.
214  * The message string should NOT start with "WARNING"
215  * and should NOT end with a newline.
216  */
217 void gmx_warning(const char *fmt, ...);
218
219 #ifdef __cplusplus
220 }
221 #endif
222
223 #endif