Chaste  Release::2017.1
Exception.cpp
1 /*
2 
3 Copyright (c) 2005-2017, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #include <cassert>
37 #include <iostream>
38 #include <petsc.h>
39 #include <string> // std::char_traits
40 
41 #include "Exception.hpp"
42 #include "BoostFilesystem.hpp"
43 #include "FileFinder.hpp"
44 #include "PosixPathFixer.hpp"
45 #include "GetCurrentWorkingDirectory.hpp"
46 #include "ChasteBuildRoot.hpp"
47 
48 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR < 2 || PETSC_VERSION_MAJOR<3 ) // Before PETSc 3.2
49 typedef PetscTruth PetscBool;
50 #endif
51 
52 Exception::Exception(const std::string& rMessage,
53  const std::string& rFilename, unsigned lineNumber)
54 {
55  SetMessage(rMessage, rFilename, lineNumber);
61  // std::string log_file_message = "Exception occurred (although possibly handled), error message:\n" + message;
62  // LOG(1, log_file_message);
63 }
64 
65 void Exception::SetMessage(const std::string& rMessage,
66  const std::string& rFilename, unsigned lineNumber)
67 {
68  // Strip off source root dir if exists
69  std::string filename(rFilename);
70  const size_t root_dir_length = std::char_traits<char>::length(ChasteSourceRootDir());
71  if (filename.compare(0,root_dir_length,ChasteSourceRootDir()) == 0)
72  {
73  filename.replace(0,root_dir_length,"./");
74  }
75 
76  std::string posix_filename(ChastePosixPathFixer::ToPosix(fs::path(filename)));
77  mShortMessage = rMessage;
78  std::stringstream line_number_stream;
79  line_number_stream << lineNumber;
80  mMessage = std::string("\nChaste error: ") + posix_filename + ":" + line_number_stream.str() + ": " + mShortMessage;
81 }
82 
83 std::string Exception::GetMessage() const
84 {
85  return mMessage;
86 }
87 
88 std::string Exception::GetShortMessage() const
89 {
90  return mShortMessage;
91 }
92 
93 std::string Exception::CheckShortMessage(std::string expected) const
94 {
95  std::string error;
96  if (mShortMessage != expected && mShortMessage != "Another process threw an exception; bailing out.")
97  {
98  error = "Incorrect exception message thrown: expected (" + expected + "); got (" + mShortMessage + ").";
99  }
100  return error;
101 }
102 
103 std::string Exception::CheckShortMessageContains(std::string expected) const
104 {
105  std::string error;
106  if (mShortMessage.find(expected) == std::string::npos && mShortMessage != "Another process threw an exception; bailing out.")
107  {
108  error = "Incorrect exception message thrown: expected it to contain (" + expected + "); got (" + mShortMessage + ").";
109  }
110  return error;
111 }
112 
113 // LCOV_EXCL_START //Termination NEVER EVER happens under normal testing conditions.
114 void Exception::Terminate(const std::string& rMessage, const std::string& rFilename, unsigned lineNumber)
115 {
116  std::stringstream error_message;
117 
118  error_message << "\nChaste termination: " << rFilename << ":" << lineNumber << ": " << rMessage<<"\n";
119  std::cerr << error_message.str();
120 
121  /*
122  * Check if we're running in parallel.
123  */
124 
125  PetscBool is_there;
126  PetscInitialized(&is_there);
127  if (is_there)
128  {
129  MPI_Abort(PETSC_COMM_WORLD, EXIT_FAILURE);
130  }
131  else
132  {
133  exit(EXIT_FAILURE);
134  }
135 }
136 
137 // LCOV_EXCL_STOP // Termination NEVER EVER happens under normal testing conditions
PetscTruth PetscBool
Definition: PetscTools.hpp:62
const char * ChasteSourceRootDir()
std::string CheckShortMessage(std::string expected) const
Definition: Exception.cpp:93
Exception(const std::string &rMessage, const std::string &rFilename, unsigned lineNumber)
Definition: Exception.cpp:52
void SetMessage(const std::string &rMessage, const std::string &rFilename, unsigned lineNumber)
Definition: Exception.cpp:65
std::string mMessage
Definition: Exception.hpp:134
std::string GetMessage() const
Definition: Exception.cpp:83
static std::string ToPosix(const fs::path path)
static void Terminate(const std::string &rMessage, const std::string &rFilename, unsigned lineNumber)
Definition: Exception.cpp:114
std::string mShortMessage
Definition: Exception.hpp:135
std::string CheckShortMessageContains(std::string expected) const
Definition: Exception.cpp:103
std::string GetShortMessage() const
Definition: Exception.cpp:88