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
index 66c12de..37acd8f 100644 (file)
 
 #include <exception>
 #include <stdexcept>
-#include <xbt/exception.hpp>
+#include <string>
+#include <type_traits>
+#include <vector>
 
+#include <xbt/backtrace.h>
+#include <xbt/backtrace.hpp>
+#include <xbt/base.h>
 #include <xbt/ex.h>
+#include <xbt/log.h>
+#include <xbt/misc.h>  // xbt_procname
+#include <xbt/virtu.h> // xbt_getpid
 
 namespace simgrid {
+namespace xbt {
+
+/** The location of where an exception has been thrown
+ *
+ *  This is a tuple (__FILE__, __LINE__, __func__), created with @ref XBT_THROW_POINT.
+ *
+ *  @ingroup XBT_ex
+ */
+class ThrowPoint {
+public:
+  ThrowPoint() = default;
+  explicit ThrowPoint(const char* file, int line, const char* function) : file_(file), line_(line), function_(function)
+  {
+  }
+  const char* file_     = nullptr;
+  int line_             = 0;
+  const char* function_ = nullptr;
+};
+
+/** Create a ThrowPoint with (__FILE__, __LINE__, __func__) */
+#define XBT_THROW_POINT ::simgrid::xbt::ThrowPoint(__FILE__, __LINE__, __func__)
+
+/** A base class for exceptions with context
+ *
+ *  This is a base class for exceptions which store additional contextual
+ *  information: backtrace, throw point, simulated process name, PID, etc.
+ */
+class XBT_PUBLIC ContextedException {
+public:
+  ContextedException() : backtrace_(simgrid::xbt::backtrace()), procname_(xbt_procname()), pid_(xbt_getpid()) {}
+  explicit ContextedException(Backtrace bt) : backtrace_(std::move(bt)), procname_(xbt_procname()), pid_(xbt_getpid())
+  {
+  }
+  explicit ContextedException(ThrowPoint throwpoint, Backtrace bt)
+      : backtrace_(std::move(bt)), procname_(xbt_procname()), pid_(xbt_getpid()), throwpoint_(throwpoint)
+  {
+  }
+  virtual ~ContextedException();
+  Backtrace const& backtrace() const { return backtrace_; }
+  int pid() const { return pid_; }
+  std::string const& process_name() const { return procname_; }
+  ThrowPoint& throw_point() { return throwpoint_; }
+
+  // deprecated
+  XBT_ATTRIB_DEPRECATED_v323("Please use WithContextException::process_name()") std::string const& processName() const
+  {
+    return process_name();
+  }
+  XBT_ATTRIB_DEPRECATED_v323("Please use WithContextException::throw_point()") ThrowPoint& throwPoint()
+  {
+    return throw_point();
+  }
+
+private:
+  Backtrace backtrace_;
+  std::string procname_; /**< Name of the process who thrown this */
+  int pid_;              /**< PID of the process who thrown this */
+  ThrowPoint throwpoint_;
+};
+} // namespace xbt
 
 /** Ancestor class of all SimGrid exception */
 class Exception : public std::runtime_error {