Merge remote-tracking branch 'origin/release-4-6' into HEAD
[alexxy/gromacs.git] / src / gromacs / utility / errorcodes.cpp
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
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.
13  *
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.
18  *
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.
25  *
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.
28  *
29  * For more info, check our website at http://www.gromacs.org
30  */
31 /*! \internal \file
32  * \brief
33  * Implements functions in errorcodes.h.
34  *
35  * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36  * \ingroup module_utility
37  */
38 #include "gromacs/utility/errorcodes.h"
39
40 #include <cstdlib>
41
42 #include "gromacs/legacyheaders/thread_mpi/mutex.h"
43
44 #include "errorformat.h"
45
46 namespace gmx
47 {
48
49 namespace
50 {
51
52 /*! \brief
53  * Strings corresponding to gmx::ErrorCode values.
54  *
55  * This has to match the enum in errorcodes.h!
56  */
57 const char *const error_names[] =
58 {
59     "No error",
60     "Out of memory",
61     "File not found",
62     "System I/O error",
63     "Error in user input",
64     "Inconsistency in user input",
65     "Simulation instability detected",
66
67     "Feature not implemented",
68     "Invalid value (bug)",
69     "Invalid call (bug)",
70     "Internal error (bug)",
71     "API error (bug)",
72     "Range checking error (possible bug)",
73     "Communication error (possible bug)",
74
75     "Unknown error",
76 };
77
78 /*! \brief
79  * The default error handler if setFatalErrorHandler() is not called.
80  */
81 void standardErrorHandler(int retcode, const char *msg,
82                           const char *file, int line)
83 {
84     const char *title = getErrorCodeString(retcode);
85     internal::printFatalError(stderr, title, msg, NULL, file, line);
86     std::exit(1);
87 }
88
89 //! Global error handler set with setFatalErrorHandler().
90 ErrorHandlerFunc g_errorHandler = standardErrorHandler;
91 //! Mutex for protecting access to g_errorHandler.
92 tMPI::mutex handler_mutex;
93
94 } // namespace
95
96 const char *getErrorCodeString(int errorcode)
97 {
98     if (errorcode < 0 || errorcode >= eeUnknownError)
99     {
100         errorcode = eeUnknownError;
101     }
102     return error_names[errorcode];
103 }
104
105 ErrorHandlerFunc setFatalErrorHandler(ErrorHandlerFunc handler)
106 {
107     tMPI::lock_guard<tMPI::mutex> lock(handler_mutex);
108     ErrorHandlerFunc oldHandler = g_errorHandler;
109     g_errorHandler = handler;
110     return oldHandler;
111 }
112
113 /*! \cond internal */
114 namespace internal
115 {
116
117 void fatalError(int retcode, const char *msg, const char *file, int line)
118 {
119     ErrorHandlerFunc handler = NULL;
120     {
121         tMPI::lock_guard<tMPI::mutex> lock(handler_mutex);
122         handler = g_errorHandler;
123     }
124     if (handler != NULL)
125     {
126         handler(retcode, msg, file, line);
127     }
128 }
129
130 } // namespace internal
131 //! \endcond
132
133 } // namespace gmx