Merge 'origin/release-4-6' into master
[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 <cstdarg>
41 #include <cstdio>
42 #include <cstdlib>
43
44 #include "errorformat.h"
45
46 // This has to match the enum in errorcodes.h
47 static const char *const error_names[] =
48 {
49     "No error",
50     "Out of memory",
51     "File not found",
52     "System I/O error",
53     "Error in user input",
54     "Inconsistency in user input",
55     "Simulation instability detected",
56
57     "Feature not implemented",
58     "Invalid value (bug)",
59     "Invalid call (bug)",
60     "Internal error (bug)",
61     "API error (bug)",
62     "Range checking error (possible bug)",
63     "Communication error (possible bug)",
64
65     "Unknown error",
66 };
67
68 namespace gmx
69 {
70
71 const char *getErrorCodeString(int errorcode)
72 {
73     if (errorcode < 0 || errorcode >= eeUnknownError)
74     {
75         errorcode = eeUnknownError;
76     }
77     return error_names[errorcode];
78 }
79
80 static void standardErrorHandler(int retcode, const char *msg,
81                                  const char *file, int line)
82 {
83     const char *title = getErrorCodeString(retcode);
84     fprintf(stderr, "%s",
85             internal::formatFatalError(title, msg, NULL, file, line).c_str());
86     std::exit(1);
87 }
88
89 static ErrorHandlerFunc error_handler = standardErrorHandler;
90
91 ErrorHandlerFunc setFatalErrorHandler(ErrorHandlerFunc handler)
92 {
93     // TODO: Acquire a mutex here
94     ErrorHandlerFunc old_handler = error_handler;
95     error_handler = handler;
96     // TODO: Release the mutex here
97     return old_handler;
98 }
99
100 /*! \cond internal */
101 namespace internal
102 {
103
104 void fatalError(int retcode, const char *msg, const char *file, int line)
105 {
106     // TODO: Acquire a mutex here
107     ErrorHandlerFunc handler = error_handler;
108     // TODO: Release the mutex here
109     if (handler != NULL)
110     {
111         handler(retcode, msg, file, line);
112     }
113 }
114
115 } // namespace internal
116 //! \endcond
117
118 } // namespace gmx