Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge the content of xbt/exception.hpp into simgrid/Exception.hpp
[simgrid.git] / include / simgrid / Exception.hpp
1 /* Copyright (c) 2018. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_EXCEPTIONS_HPP
7 #define SIMGRID_EXCEPTIONS_HPP
8
9 /** @file exception.hpp SimGrid-specific Exceptions
10  *
11  *  Defines all possible exception that could occur in a SimGrid library.
12  */
13
14 #include <exception>
15 #include <stdexcept>
16 #include <string>
17 #include <type_traits>
18 #include <vector>
19
20 #include <xbt/backtrace.h>
21 #include <xbt/backtrace.hpp>
22 #include <xbt/base.h>
23 #include <xbt/ex.h>
24 #include <xbt/log.h>
25 #include <xbt/misc.h>  // xbt_procname
26 #include <xbt/virtu.h> // xbt_getpid
27
28 namespace simgrid {
29 namespace xbt {
30
31 /** The location of where an exception has been thrown
32  *
33  *  This is a tuple (__FILE__, __LINE__, __func__), created with @ref XBT_THROW_POINT.
34  *
35  *  @ingroup XBT_ex
36  */
37 class ThrowPoint {
38 public:
39   ThrowPoint() = default;
40   explicit ThrowPoint(const char* file, int line, const char* function) : file_(file), line_(line), function_(function)
41   {
42   }
43   const char* file_     = nullptr;
44   int line_             = 0;
45   const char* function_ = nullptr;
46 };
47
48 /** Create a ThrowPoint with (__FILE__, __LINE__, __func__) */
49 #define XBT_THROW_POINT ::simgrid::xbt::ThrowPoint(__FILE__, __LINE__, __func__)
50
51 /** A base class for exceptions with context
52  *
53  *  This is a base class for exceptions which store additional contextual
54  *  information: backtrace, throw point, simulated process name, PID, etc.
55  */
56 class XBT_PUBLIC ContextedException {
57 public:
58   ContextedException() : backtrace_(simgrid::xbt::backtrace()), procname_(xbt_procname()), pid_(xbt_getpid()) {}
59   explicit ContextedException(Backtrace bt) : backtrace_(std::move(bt)), procname_(xbt_procname()), pid_(xbt_getpid())
60   {
61   }
62   explicit ContextedException(ThrowPoint throwpoint, Backtrace bt)
63       : backtrace_(std::move(bt)), procname_(xbt_procname()), pid_(xbt_getpid()), throwpoint_(throwpoint)
64   {
65   }
66   virtual ~ContextedException();
67   Backtrace const& backtrace() const { return backtrace_; }
68   int pid() const { return pid_; }
69   std::string const& process_name() const { return procname_; }
70   ThrowPoint& throw_point() { return throwpoint_; }
71
72   // deprecated
73   XBT_ATTRIB_DEPRECATED_v323("Please use WithContextException::process_name()") std::string const& processName() const
74   {
75     return process_name();
76   }
77   XBT_ATTRIB_DEPRECATED_v323("Please use WithContextException::throw_point()") ThrowPoint& throwPoint()
78   {
79     return throw_point();
80   }
81
82 private:
83   Backtrace backtrace_;
84   std::string procname_; /**< Name of the process who thrown this */
85   int pid_;              /**< PID of the process who thrown this */
86   ThrowPoint throwpoint_;
87 };
88 } // namespace xbt
89
90 /** Ancestor class of all SimGrid exception */
91 class Exception : public std::runtime_error {
92 public:
93   Exception() : std::runtime_error("") {}
94   Exception(const char* message) : std::runtime_error(message) {}
95 };
96
97 /** Exception raised when a timeout elapsed */
98 class timeout_error : public simgrid::Exception {
99 };
100
101 /** Exception raised when an host fails */
102 class host_failure : public simgrid::Exception {
103 };
104
105 /** Exception raised when a communication fails because of the network */
106 class network_failure : public simgrid::Exception {
107 };
108
109 /** Exception raised when something got canceled before completion */
110 class canceled_error : public simgrid::Exception {
111 };
112
113 } // namespace simgrid
114
115 /** A legacy exception
116  *
117  *  It is defined by a category and a value within that category (as well as
118  *  an optional error message).
119  *
120  *  This used to be a structure for C exceptions but it has been retrofitted
121  *  as a C++ exception and some of its data has been moved in the
122  *  @ref WithContextException base class. We should deprecate it and replace it
123  *  with either C++ different exceptions or `std::system_error` which already
124  *  provides this (category + error code) logic.
125  *
126  *  @ingroup XBT_ex_c
127  */
128 class XBT_PUBLIC xbt_ex : public simgrid::Exception, public simgrid::xbt::ContextedException {
129 public:
130   xbt_ex() : simgrid::Exception() {}
131
132   /**
133    *
134    * @param throwpoint Throw point (use XBT_THROW_POINT)
135    * @param message    Exception message
136    */
137   xbt_ex(simgrid::xbt::ThrowPoint throwpoint, const char* message)
138       : simgrid::Exception(message), simgrid::xbt::ContextedException(throwpoint, simgrid::xbt::backtrace())
139   {
140   }
141
142   ~xbt_ex(); // DO NOT define it here -- see ex.cpp for a rationale
143
144   /** Category (what went wrong) */
145   xbt_errcat_t category = unknown_error;
146
147   /** Why did it went wrong */
148   int value = 0;
149 };
150
151 #endif