Modernize syntax of enable_if and traits to use _t helpers
[gromacs.git] / src / gromacs / utility / fatalerror.cpp
blob37234b54b4576c4ca22c824de49aed023ecef7ba
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014,2015,2016,2017,2018, 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.
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.
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.
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.
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.
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.
37 #include "gmxpre.h"
39 #include "fatalerror.h"
41 #include "config.h"
43 #include <cerrno>
44 #include <cstddef>
45 #include <cstdlib>
46 #include <cstring>
48 #include <exception>
50 #include "gromacs/utility/basedefinitions.h"
51 #include "gromacs/utility/baseversion.h"
52 #include "gromacs/utility/cstringutil.h"
53 #include "gromacs/utility/errorcodes.h"
54 #include "gromacs/utility/futil.h"
55 #include "gromacs/utility/mutex.h"
56 #include "gromacs/utility/programcontext.h"
57 #include "gromacs/utility/stringutil.h"
59 #if GMX_MPI
60 #include "gromacs/utility/basenetwork.h"
61 #include "gromacs/utility/gmxmpi.h"
62 #endif
64 #include "errorformat.h"
66 static bool bDebug = false;
67 static gmx::Mutex where_mutex;
69 FILE *debug = nullptr;
70 gmx_bool gmx_debug_at = FALSE;
72 static FILE *log_file = nullptr;
73 static gmx::Mutex error_mutex;
75 using Lock = gmx::lock_guard<gmx::Mutex>;
77 void gmx_init_debug(const int dbglevel, const char *dbgfile)
79 if (!bDebug)
81 gmx_disable_file_buffering();
82 debug = gmx_ffopen(dbgfile, "w+");
83 bDebug = true;
84 if (dbglevel >= 2)
86 gmx_debug_at = TRUE;
91 gmx_bool bDebugMode()
93 return bDebug;
96 void gmx_fatal_set_log_file(FILE *fp)
98 log_file = fp;
101 static void default_error_handler(const char *title, const std::string &msg,
102 const char *file, int line)
104 if (log_file)
106 gmx::internal::printFatalErrorHeader(log_file, title, nullptr, file, line);
107 gmx::internal::printFatalErrorMessageLine(log_file, msg.c_str(), 0);
108 gmx::internal::printFatalErrorFooter(log_file);
110 gmx::internal::printFatalErrorHeader(stderr, title, nullptr, file, line);
111 gmx::internal::printFatalErrorMessageLine(stderr, msg.c_str(), 0);
112 gmx::internal::printFatalErrorFooter(stderr);
115 static gmx_error_handler_t gmx_error_handler = default_error_handler;
117 void gmx_set_error_handler(gmx_error_handler_t func)
119 Lock lock(error_mutex);
120 gmx_error_handler = func;
123 static const char *gmx_strerror(const char *key)
125 struct ErrorKeyEntry {
126 const char *key;
127 const char *msg;
129 ErrorKeyEntry map[] = {
130 { "call", "Routine should not have been called" },
131 { "comm", "Communication (parallel processing) problem" },
132 { "fatal", "Fatal error" },
133 { "file", "File input/output error" },
134 { "impl", "Implementation restriction" },
135 { "incons", "Software inconsistency error" },
136 { "input", "Input error or input inconsistency" },
137 { "mem", "Memory allocation/freeing error" },
138 { "open", "Cannot open file" },
139 { "range", "Range checking error" }
142 if (key == nullptr)
144 return "NULL error type (should not occur)";
146 for (const ErrorKeyEntry &entry : map)
148 if (std::strcmp(key, entry.key) == 0)
150 return entry.msg;
153 return gmx::getErrorCodeString(gmx::eeUnknownError);
156 static void call_error_handler(const char *key, const char *file, int line, const std::string &msg)
158 Lock lock(error_mutex);
159 gmx_error_handler(gmx_strerror(key),
160 msg.empty() ? "Empty gmx_fatal message (bug)." : msg,
161 file, line);
164 void gmx_exit_on_fatal_error(ExitType exitType, int returnValue)
166 if (log_file)
168 std::fflush(log_file);
170 if (debug)
172 std::fflush(debug);
174 std::fflush(stdout);
175 std::fflush(stderr);
177 #if GMX_MPI
178 if (gmx_mpi_initialized())
180 switch (exitType)
182 case ExitType_CleanExit:
183 MPI_Finalize();
184 break;
185 case ExitType_Abort:
186 #if GMX_LIB_MPI
187 gmx_abort(returnValue);
188 #else
189 break;
190 #endif
191 case ExitType_NonMasterAbort:
192 // Let all other processes wait till the master has printed
193 // the error message and issued MPI_Abort.
194 MPI_Barrier(MPI_COMM_WORLD);
195 break;
198 #endif
200 if (exitType == ExitType_CleanExit)
202 std::exit(returnValue);
204 // We cannot use std::exit() if other threads may still be executing, since that would cause destructors to be
205 // called for global objects that may still be in use elsewhere.
206 std::_Exit(returnValue);
209 void gmx_fatal_mpi_va(int /*f_errno*/, const char *file, int line,
210 gmx_bool bMaster, gmx_bool bFinalize,
211 const char *fmt, va_list ap)
213 if (bMaster)
215 std::string msg = gmx::formatStringV(fmt, ap);
216 call_error_handler("fatal", file, line, msg);
219 ExitType exitType = ExitType_CleanExit;
220 if (!bFinalize)
222 exitType = bMaster ? ExitType_Abort : ExitType_NonMasterAbort;
224 gmx_exit_on_fatal_error(exitType, 1);
227 void gmx_fatal(int f_errno, const char *file, int line, gmx_fmtstr const char *fmt, ...)
229 va_list ap;
230 va_start(ap, fmt);
231 gmx_fatal_mpi_va(f_errno, file, line, TRUE, FALSE, fmt, ap);
232 va_end(ap);
235 void _gmx_error(const char *key, const std::string &msg, const char *file, int line)
237 call_error_handler(key, file, line, msg);
238 gmx_exit_on_fatal_error(ExitType_Abort, 1);
241 void _range_check(int n, int n_min, int n_max, const char *warn_str,
242 const char *var, const char *file, int line)
244 char buf[1024];
246 if ((n < n_min) || (n >= n_max))
248 if (warn_str != nullptr)
250 strcpy(buf, warn_str);
251 strcat(buf, "\n");
253 else
255 buf[0] = '\0';
258 sprintf(buf+strlen(buf), "Variable %s has value %d. It should have been "
259 "within [ %d .. %d ]\n", var, n, n_min, n_max);
261 _gmx_error("range", buf, file, line);
265 void gmx_warning(gmx_fmtstr const char *fmt, ...)
267 va_list ap;
268 char msg[STRLEN];
270 va_start(ap, fmt);
271 vsprintf(msg, fmt, ap);
272 va_end(ap);
274 fprintf(stderr, "\nWARNING: %s\n\n", msg);