PISM, A Parallel Ice Sheet Model  stable v2.0.4 committed by Constantine Khrulev on 2022-05-25 12:02:27 -0800
error_handling.hh
Go to the documentation of this file.
1 /* Copyright (C) 2014, 2015, 2016, 2018, 2019, 2021 PISM Authors
2  *
3  * This file is part of PISM.
4  *
5  * PISM is free software; you can redistribute it and/or modify it under the
6  * terms of the GNU General Public License as published by the Free Software
7  * Foundation; either version 3 of the License, or (at your option) any later
8  * version.
9  *
10  * PISM is distributed in the hope that it will be useful, but WITHOUT ANY
11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with PISM; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #ifndef _ERROR_HANDLING_H_
21 #define _ERROR_HANDLING_H_
22 
23 #include <mpi.h> // MPI_Comm
24 #include <stdexcept>
25 #include <string>
26 #include <vector>
27 
28 #include "pism/pism_config.hh" // Pism_DEBUG
29 
30 
31 namespace pism {
32 
34 public:
35  ErrorLocation();
36  ErrorLocation(const char *name, int line);
37  const char *filename;
39 };
40 
41 #if Pism_DEBUG==1
42 #define PISM_ERROR_LOCATION pism::ErrorLocation(__FILE__, __LINE__)
43 #else
44 #define PISM_ERROR_LOCATION pism::ErrorLocation()
45 #endif
46 
47 class RuntimeError : public std::runtime_error {
48 public:
49  RuntimeError(const ErrorLocation &location, const std::string &message);
50  ~RuntimeError() throw();
51 
52  typedef void (*Hook)(RuntimeError*);
53 
54  //! @brief build a RuntimeError with a formatted message
55  static RuntimeError formatted(const ErrorLocation &location, const char format[], ...) __attribute__((format(printf, 2, 3)));
56 
57  static void set_hook(Hook new_hook);
58 
59  //! @brief Add a message providing some context. This way we can (sort of)
60  //! get a stack trace even though C++ exceptions do not provide one.
61  void add_context(const std::string &message);
62  void add_context(const char format[], ...) __attribute__((format(printf, 2, 3)));
63  void print(MPI_Comm com);
64  protected:
65  std::vector<std::string> m_context;
66  static Hook sm_hook;
68 };
69 
71 public:
72  ParallelSection(MPI_Comm com);
73  ~ParallelSection() = default;
74  void check();
75  void failed();
76  void reset();
77 private:
78  bool m_failed;
79  MPI_Comm m_com;
80 };
81 
82 void handle_fatal_errors(MPI_Comm com);
83 
84 void check_c_call(int errcode, int success, const char* function_name,
85  const char *file, int line);
86 
87 void check_petsc_call(int errcode, const char* function_name,
88  const char *file, int line);
89 
90 #define PISM_C_CHK(errcode,success,name) do { pism::check_c_call(errcode, success, name, __FILE__, __LINE__); } while (0)
91 #define PISM_CHK(errcode,name) do { pism::check_petsc_call(errcode, name, __FILE__, __LINE__); } while (0)
92 
93 } // end of namespace pism
94 
95 #endif /* _ERROR_HANDLING_H_ */
const char * filename
~ParallelSection()=default
ErrorLocation m_location
void(* Hook)(RuntimeError *)
RuntimeError(const ErrorLocation &location, const std::string &message)
static RuntimeError static void set_hook(Hook new_hook)
void add_context(const std::string &message)
Add a message providing some context. This way we can (sort of) get a stack trace even though C++ exc...
std::vector< std::string > m_context
void void print(MPI_Comm com)
static RuntimeError formatted(const ErrorLocation &location, const char format[],...) __attribute__((format(printf
build a RuntimeError with a formatted message
static void check(const ErrorLocation &where, int return_code)
Prints an error message; for debugging.
Definition: NC4_Par.cc:35
std::string printf(const char *format,...)
void check_c_call(int errcode, int success, const char *function_name, const char *file, int line)
void handle_fatal_errors(MPI_Comm com)
void check_petsc_call(int errcode, const char *function_name, const char *file, int line)
#define __attribute__(x)