3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
9 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11 * Copyright (c) 2001-2009, The GROMACS development team,
12 * check out http://www.gromacs.org for more information.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * If you want to redistribute modifications, please consider that
20 * scientific software is very special. Version control is crucial -
21 * bugs must be traceable. We will be happy to consider code for
22 * inclusion in the official distribution, but derived work must not
23 * be called official GROMACS. Details are found in the README & COPYING
24 * files - if they are missing, get the official version at www.gromacs.org.
26 * To help us fund GROMACS development, we humbly ask that you cite
27 * the papers on the package - you can find them in the top README file.
29 * For more info, check our website at http://www.gromacs.org
33 * Implements functions in errorcodes.h.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36 * \ingroup module_utility
38 #include "gromacs/utility/errorcodes.h"
42 #include "gromacs/legacyheaders/thread_mpi/mutex.h"
44 #include "errorformat.h"
53 * Strings corresponding to gmx::ErrorCode values.
55 * This has to match the enum in errorcodes.h!
57 const char *const error_names[] =
63 "Error in user input",
64 "Inconsistency in user input",
65 "Simulation instability detected",
67 "Feature not implemented",
68 "Invalid value (bug)",
70 "Internal error (bug)",
72 "Range checking error (possible bug)",
73 "Communication error (possible bug)",
79 * The default error handler if setFatalErrorHandler() is not called.
81 void standardErrorHandler(int retcode, const char *msg,
82 const char *file, int line)
84 const char *title = getErrorCodeString(retcode);
85 internal::printFatalErrorHeader(stderr, title, NULL, file, line);
86 internal::printFatalErrorMessageLine(stderr, msg, 0);
87 internal::printFatalErrorFooter(stderr);
91 //! Global error handler set with setFatalErrorHandler().
92 ErrorHandlerFunc g_errorHandler = standardErrorHandler;
93 //! Mutex for protecting access to g_errorHandler.
94 tMPI::mutex handler_mutex;
98 const char *getErrorCodeString(int errorcode)
100 if (errorcode < 0 || errorcode >= eeUnknownError)
102 errorcode = eeUnknownError;
104 return error_names[errorcode];
107 ErrorHandlerFunc setFatalErrorHandler(ErrorHandlerFunc handler)
109 tMPI::lock_guard<tMPI::mutex> lock(handler_mutex);
110 ErrorHandlerFunc oldHandler = g_errorHandler;
111 g_errorHandler = handler;
115 /*! \cond internal */
119 void fatalError(int retcode, const char *msg, const char *file, int line)
121 ErrorHandlerFunc handler = NULL;
123 tMPI::lock_guard<tMPI::mutex> lock(handler_mutex);
124 handler = g_errorHandler;
128 handler(retcode, msg, file, line);
132 } // namespace internal