Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2012-2018 The plumed team
3 : (see the PEOPLE file at the root of the distribution for a list of names)
4 :
5 : See http://www.plumed.org for more information.
6 :
7 : This file is part of plumed, version 2.
8 :
9 : plumed is free software: you can redistribute it and/or modify
10 : it under the terms of the GNU Lesser General Public License as published by
11 : the Free Software Foundation, either version 3 of the License, or
12 : (at your option) any later version.
13 :
14 : plumed is distributed in the hope that it will be useful,
15 : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : GNU Lesser General Public License for more details.
18 :
19 : You should have received a copy of the GNU Lesser General Public License
20 : along with plumed. If not, see <http://www.gnu.org/licenses/>.
21 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 : #include "Exception.h"
23 :
24 : #if defined(__PLUMED_HAS_EXECINFO)
25 : #include <execinfo.h>
26 : #endif
27 :
28 : #include <cstdio>
29 : #include <cstdlib>
30 :
31 : using namespace std;
32 : namespace PLMD {
33 :
34 0 : std::string Exception::format(const std::string&msg,const std::string&file,unsigned line,const std::string&function) {
35 0 : std::string message;
36 0 : message="\n+++ Internal PLUMED error";
37 0 : if(file.length()>0) {
38 : char cline[1000];
39 0 : sprintf(cline,"%u",line);
40 0 : message += "\n+++ file "+file+", line "+cline;
41 0 : if(function.length()>0) message +=", function "+function;
42 : }
43 0 : if(msg.length()>0) message +="\n+++ message: "+msg;
44 0 : return message;
45 : }
46 :
47 :
48 0 : Exception::Exception():
49 0 : msg(format("","",0,""))
50 : {
51 0 : abortIfExceptionsAreDisabled();
52 0 : }
53 :
54 0 : Exception::Exception(const std::string&msg):
55 0 : msg(format(msg,"",0,""))
56 : {
57 0 : abortIfExceptionsAreDisabled();
58 0 : }
59 :
60 0 : Exception::Exception(const std::string&msg,const std::string&file,unsigned line,const std::string&function):
61 0 : msg(format(msg,file,line,function))
62 : {
63 0 : abortIfExceptionsAreDisabled();
64 0 : }
65 :
66 0 : void Exception::abortIfExceptionsAreDisabled() {
67 : #if ! defined(__PLUMED_HAS_EXCEPTIONS)
68 :
69 : #ifdef __PLUMED_HAS_EXECINFO
70 0 : fprintf(stderr,"\n\n********** STACK DUMP **********\n");
71 : void* callstack[128];
72 0 : int i, frames = backtrace(callstack, 128);
73 0 : char** strs = backtrace_symbols(callstack, frames);
74 0 : for (i = 0; i < frames; ++i) {
75 0 : fprintf(stderr,"%s\n", strs[i]);
76 : }
77 0 : fprintf(stderr,"******** END STACK DUMP ********\n");
78 0 : free(strs);
79 : #endif
80 :
81 0 : fprintf(stderr,"%s",what());
82 0 : fprintf(stderr,"\n");
83 :
84 0 : std::abort();
85 : #endif
86 : }
87 :
88 : }
89 :
90 :
|