PISM, A Parallel Ice Sheet Model  stable v2.1.1 committed by Constantine Khrulev on 2024-12-04 13:36:58 -0900
error_handling.hh
Go to the documentation of this file.
1 /* Copyright (C) 2014, 2015, 2016, 2018, 2019, 2021, 2023 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 PISM_ERROR_HANDLING_H
21 #define PISM_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 namespace pism {
31 
33 public:
34  ErrorLocation();
35  ErrorLocation(const char *filename, int line);
36  const char *filename;
38 };
39 
40 #if (Pism_DEBUG==1)
41 #define PISM_ERROR_LOCATION pism::ErrorLocation(__FILE__, __LINE__)
42 #else
43 #define PISM_ERROR_LOCATION pism::ErrorLocation()
44 #endif
45 
46 class RuntimeError : public std::runtime_error {
47 public:
48  RuntimeError(const ErrorLocation &location, const std::string &message);
49  ~RuntimeError() throw();
50 
51  typedef void (*Hook)(RuntimeError*);
52 
53  //! @brief build a RuntimeError with a formatted message
54  static RuntimeError formatted(const ErrorLocation &location, const char format[], ...) __attribute__((format(printf, 2, 3)));
55 
56  static void set_hook(Hook new_hook);
57 
58  //! @brief Add a message providing some context. This way we can (sort of)
59  //! get a stack trace even though C++ exceptions do not provide one.
60  void add_context(const std::string &message);
61  void add_context(const char format[], ...) __attribute__((format(printf, 2, 3)));
62  void print(MPI_Comm com);
63  protected:
64  std::vector<std::string> m_context;
65  static Hook sm_hook;
67 };
68 
70 public:
71  ParallelSection(MPI_Comm com);
72  ~ParallelSection() = default;
73  void check();
74  void failed();
75  void reset();
76 private:
77  bool m_failed;
78  MPI_Comm m_com;
79 };
80 
81 void handle_fatal_errors(MPI_Comm com);
82 
83 void check_c_call(int errcode, int success, const char* function_name,
84  const char *file, int line);
85 
86 void check_petsc_call(int errcode, const char* function_name,
87  const char *file, int line);
88 
89 #define PISM_C_CHK(errcode,success,name) do { pism::check_c_call(errcode, success, name, __FILE__, __LINE__); } while (0)
90 #define PISM_CHK(errcode,name) do { pism::check_petsc_call(errcode, name, __FILE__, __LINE__); } while (0)
91 
92 } // end of namespace pism
93 
94 #endif /* PISM_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:36
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)