ba418d01f15a8037e04ac2e9494f74800dc09440
[alexxy/gromacs.git] / include / gmx_fatal.h
1
2 /*
3  * 
4  *                This source code is part of
5  * 
6  *                 G   R   O   M   A   C   S
7  * 
8  *          GROningen MAchine for Chemical Simulations
9  * 
10  *                        VERSION 3.2.0
11  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13  * Copyright (c) 2001-2004, The GROMACS development team,
14  * check out http://www.gromacs.org for more information.
15
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  * 
21  * If you want to redistribute modifications, please consider that
22  * scientific software is very special. Version control is crucial -
23  * bugs must be traceable. We will be happy to consider code for
24  * inclusion in the official distribution, but derived work must not
25  * be called official GROMACS. Details are found in the README & COPYING
26  * files - if they are missing, get the official version at www.gromacs.org.
27  * 
28  * To help us fund GROMACS development, we humbly ask that you cite
29  * the papers on the package - you can find them in the top README file.
30  * 
31  * For more info, check our website at http://www.gromacs.org
32  * 
33  * And Hey:
34  * Gromacs Runs On Most of All Computer Systems
35  */
36
37 #ifndef _fatal_h
38 #define _fatal_h
39 #include "visibility.h"
40
41 #include <stdio.h>
42 #include <stdarg.h>
43 #include <errno.h>
44 #include "types/simple.h"
45
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49
50 #ifndef __has_feature      // Optional.
51 #define __has_feature(x) 0 // Compatibility with non-clang compilers.
52 #endif
53
54 /** \def GMX_ATTRIBUTE_NORETURN \brief Indicate that a function is not
55  * expected to return.
56  * WARNING: In general this flag should not be used for compiler
57  * optimizations, since set_gmx_error_handler can be set to a
58  * handler which does not quit.
59  */
60 #ifndef GMX_ATTRIBUTE_NORETURN
61 #if __has_feature(attribute_analyzer_noreturn)
62 #define GMX_ATTRIBUTE_NORETURN __attribute__((analyzer_noreturn))
63 #else
64 #define GMX_ATTRIBUTE_NORETURN
65 #endif
66 #endif
67   
68 GMX_LIBGMX_EXPORT
69 void 
70 _where(const char *file,int line);
71 #define where() _where(__FILE__,__LINE__)
72 /* Prints filename and line to stdlog and only on amba memvail */
73   
74 void 
75 _set_fatal_tmp_file(const char *fn, const char *file, int line);
76 #define set_fatal_tmp_file(fn) _set_fatal_tmp_file(fn,__FILE__,__LINE__)
77 /* set filename to be removed when fatal_error is called */
78
79 void 
80 _unset_fatal_tmp_file(const char *fn, const char *file, int line);
81 #define unset_fatal_tmp_file(fn) _unset_fatal_tmp_file(fn,__FILE__,__LINE__)
82 /* unsets filename to be removed */
83
84 GMX_LIBGMX_EXPORT
85 void 
86 gmx_fatal(int fatal_errno,const char *file,int line,const char *fmt,...) GMX_ATTRIBUTE_NORETURN;
87 #define FARGS 0,__FILE__,__LINE__
88 /*
89  * Routine gmx_fatal prints 
90  *
91  *      "fatal error file %s line %s \n\t " 
92  *
93  * followed by the string specified by fmt and supplied parameters. If 
94  * errno is 0, only the message and arguments are printed. If errno is 
95  * a legal system errno or -1, a perror like message is printed after the
96  * first message, if errno is -1, the last system errno will be used.
97  * The format of fmt is that like printf etc, only %d, %x, %c, %f, %g and %s
98  * are allowed as format specifiers.
99  *
100  * In case all MPI processes want to stop with the same fatal error,
101  * use gmx_fatal_collective, declared in gmx_fatal_collective.h,
102  * to avoid having as many error messages as processes.
103  *
104  * Tip of the week:
105  * call this function using the FARGS macro:
106  * gmx_fatal(FARGS,fmt,...)
107  *
108  */
109
110 void
111 gmx_fatal_set_log_file(FILE *fp);
112 /* Set the log file for printing error messages */
113   
114 GMX_LIBGMX_EXPORT
115 void 
116 _invalid_case(const char *fn,int line);
117 #define invalid_case() _invalid_case(__FILE__,__LINE__)
118 /* Issue a warning stating 'Invalid case in switch' */
119   
120 void _unexpected_eof(const char *fn,int line,const char *srcfn,int srcline);
121 #define unexpected_eof(fn,line) _unexpected_eof(fn,line,__FILE__,__LINE__)
122
123 /* 
124  * Functions can write to this file for debug info
125  * Before writing to it, it should be checked whether
126  * the file is not NULL:
127  * if (debug) fprintf(debug,"%s","Hallo");
128  */
129 GMX_LIBGMX_EXPORT
130 extern FILE *debug;
131 GMX_LIBGMX_EXPORT
132 extern gmx_bool gmx_debug_at;
133
134 void init_debug (const int dbglevel,const char *dbgfile);
135   
136 GMX_LIBGMX_EXPORT
137 gmx_bool bDebugMode(void);
138 /* Return TRUE when the program was started in debug mode */
139   
140 #if (defined __sgi && defined USE_SGI_FPE)
141 void doexceptions(void);
142 /* Set exception handlers for debugging */
143 #endif
144
145   /* warn_str is allowed to be NULL.
146    */
147 GMX_LIBGMX_EXPORT
148   void _range_check(int n,int n_min,int n_max,const char *warn_str,
149                            const char *var,
150                            const char *file,int line);
151
152 #define range_check_mesg(n,n_min,n_max,str) _range_check(n,n_min,n_max,str,#n,__FILE__,__LINE__)
153   /* Range check will terminate with an error message if not
154    * n E [ n_min, n_max >
155    * That is n_min is inclusive but not n_max.
156    */
157
158 #define range_check(n,n_min,n_max) _range_check(n,n_min,n_max,NULL,#n,__FILE__,__LINE__)
159   /* Range check will terminate with an error message if not
160    * n E [ n_min, n_max >
161    * That is n_min is inclusive but not n_max.
162    */
163
164   char *gmx_strerror(const char *key);
165   /* Return error message corresponding to the key.
166    * Maybe a multi-line message.
167    * The messages are stored in src/gmxlib/fatal.c
168    */
169   
170 GMX_LIBGMX_EXPORT
171   void _gmx_error(const char *key,const char *msg,const char *file,int line) GMX_ATTRIBUTE_NORETURN;
172 #define gmx_error(key,msg) _gmx_error(key,msg,__FILE__,__LINE__)
173   /* Error msg of type key is generated and the program is 
174    * terminated unless and error handle is set (see below)
175    */
176
177   /* Some common error types */
178 #define gmx_bug(msg)    gmx_error("bug",msg)
179 #define gmx_call(msg)   gmx_error("call",msg)
180 #define gmx_comm(msg)   gmx_error("comm",msg)
181 #define gmx_file(msg)   gmx_error("file",msg)
182 #define gmx_cmd(msg)    gmx_error("cmd",msg)
183 #define gmx_impl(msg)   gmx_error("impl",msg)
184 #define gmx_incons(msg) gmx_error("incons",msg)
185 #define gmx_input(msg)  gmx_error("input",msg)
186 #define gmx_mem(msg)    gmx_error("mem",msg)
187 #define gmx_open(fn)    gmx_error("open",fn) 
188   
189 void 
190 set_gmx_error_handler(void (*func)(const char *msg));
191 /* An error function will be called that terminates the program 
192    * with a fatal error, unless you override it with another function.
193    * i.e.:
194    * set_gmx_error_handler(my_func);
195    * where my_func is a function that takes a string as an argument.
196    * The string may be a multi-line string.
197    */
198
199 GMX_LIBGMX_EXPORT
200 void gmx_warning(const char *fmt,...);
201 /* Print a warning message to stderr.
202  * The format of fmt is that like printf etc, only %d, %x, %c, %f, %g and %s
203  * are allowed as format specifiers.
204  * The message string should NOT start with "WARNING"
205  * and should NOT end with a newline.
206  */
207
208
209 #ifdef __cplusplus
210            }
211 #endif
212
213 #endif  /* _fatal_h */