Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
76f083755e9ca73e9467d92194f56f851b6a635f
[simgrid.git] / src / xbt / ex.cpp
1 /* ex - Exception Handling                                                  */
2
3 /* Copyright (c) 2005-2018. The SimGrid Team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include <cstdio>
9 #include <cstdlib>
10
11 #include <xbt/backtrace.hpp>
12 #include "src/internal_config.h"           /* execinfo when available */
13 #include "xbt/ex.h"
14 #include <xbt/ex.hpp>
15 #include "xbt/log.h"
16 #include "xbt/log.hpp"
17 #include "xbt/backtrace.h"
18 #include "xbt/backtrace.hpp"
19 #include "src/xbt_modinter.h"       /* backtrace initialization headers */
20
21 #include "simgrid/sg_config.hpp" /* Configuration mechanism of SimGrid */
22
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_ex, xbt, "Exception mechanism");
24
25 // DO NOT define ~xbt_ex() in ex.hpp.
26 // Defining it here ensures that xbt_ex is defined only in libsimgrid, but not in libsimgrid-java.
27 // Doing otherwise naturally breaks things (at least on freebsd with clang).
28
29 xbt_ex::~xbt_ex() = default;
30
31 void _xbt_throw(char* message, xbt_errcat_t errcat, int value, const char* file, int line, const char* func)
32 {
33   xbt_ex e(simgrid::xbt::ThrowPoint(file, line, func), message);
34   xbt_free(message);
35   e.category = errcat;
36   e.value = value;
37   throw e;
38 }
39
40 /** @brief shows an exception content and the associated stack if available */
41 void xbt_ex_display(xbt_ex_t * e)
42 {
43   simgrid::xbt::log_exception(xbt_log_priority_critical, "UNCAUGHT EXCEPTION", *e);
44 }
45
46 /** @brief returns a short name for the given exception category */
47 const char *xbt_ex_catname(xbt_errcat_t cat)
48 {
49   switch (cat) {
50   case unknown_error:
51     return "unknown error";
52   case arg_error:
53     return "invalid argument";
54   case bound_error:
55     return "out of bounds";
56   case mismatch_error:
57     return "mismatch";
58   case not_found_error:
59     return "not found";
60   case system_error:
61     return "system error";
62   case network_error:
63     return "network error";
64   case timeout_error:
65     return "timeout";
66   case cancel_error:
67     return "action canceled";
68   case thread_error:
69     return "thread error";
70   case host_error:
71     return "host failed";
72   case tracing_error:
73     return "tracing error";
74   case io_error:
75     return "io error";
76   case vm_error:
77     return "vm error";
78   default:
79     return "INVALID ERROR";
80   }
81   return "INVALID ERROR";
82 }