LCOV - code coverage report
Current view: top level - tools - Exception.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 33 33 100.0 %
Date: 2024-10-11 08:09:47 Functions: 5 5 100.0 %

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

Generated by: LCOV version 1.15