Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2012-2020 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 <cstring> 30 : #include <cstdlib> 31 : 32 : using namespace std; 33 : namespace PLMD { 34 : 35 52 : Exception::Exception(): 36 208 : note(true) 37 : { 38 : #ifdef __PLUMED_HAS_EXECINFO 39 : { 40 : void* callstack[128]; 41 52 : int frames = backtrace(callstack, 128); 42 52 : char** strs = backtrace_symbols(callstack, frames); 43 1514 : for (int i = 0; i < frames; ++i) {stackString+=strs[i]; stackString+="\n";} 44 52 : free(strs); 45 : } 46 : #endif 47 52 : const char* env=getenv("PLUMED_STACK_TRACE"); 48 52 : if(stackString.length()>0 && env && !strcmp(env,"yes")) { 49 : msg+="\n\n********** STACK DUMP **********\n"; 50 : msg+=stackString; 51 : msg+="\n********** END STACK DUMP **********\n"; 52 : } 53 : msg+="\n+++ PLUMED error"; 54 52 : } 55 : 56 53 : Exception& Exception::operator<<(const std::string&msg) 57 : { 58 53 : if(msg.length()>0) { 59 53 : if(note) this->msg +="\n+++ message follows +++\n"; 60 53 : this->msg +=msg; 61 53 : note=false; 62 : } 63 53 : return *this; 64 : } 65 : 66 49 : Exception& Exception::operator<<(const Location&loc) 67 : { 68 49 : if(loc.file) { 69 : char cline[1000]; 70 49 : sprintf(cline,"%u",loc.line); 71 49 : this->msg += "\n+++ at "; 72 49 : this->msg += loc.file; 73 : this->msg += ":"; 74 : this->msg += cline; 75 49 : if(loc.pretty && loc.pretty[0]) { 76 : this->msg += ", function "; 77 49 : this->msg += loc.pretty; 78 : } 79 : } 80 49 : note=true; 81 49 : return *this; 82 : } 83 : 84 12 : Exception& Exception::operator<<(const Assertion&as) 85 : { 86 12 : if(as.assertion) { 87 12 : this->msg += "\n+++ assertion failed: "; 88 12 : this->msg += as.assertion; 89 : } 90 12 : note=true; 91 12 : return *this; 92 : } 93 : 94 : } 95 : 96 :