Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / src / xbt / exception.cpp
1 /* Copyright (c) 2005-2016. The SimGrid Team.
2  * All rights reserved. */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <exception>
8 #include <typeinfo>
9 #include <memory>
10
11 #include <xbt/backtrace.hpp>
12 #include <xbt/exception.hpp>
13 #include <xbt/log.h>
14 #include <xbt/log.hpp>
15
16 extern "C" {
17 XBT_LOG_EXTERNAL_CATEGORY(xbt);
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_exception, xbt, "Exceptions");
19 }
20
21 namespace simgrid {
22 namespace xbt {
23
24 WithContextException::~WithContextException() {}
25
26 void logException(
27   e_xbt_log_priority_t prio,
28   const char* context, std::exception const& exception)
29 {
30   try {
31     auto name = simgrid::xbt::demangle(typeid(exception).name());
32
33     auto with_context =
34       dynamic_cast<const simgrid::xbt::WithContextException*>(&exception);
35     if (with_context != nullptr)
36       XBT_LOG(prio, "%s %s by %s/%d: %s",
37         context, name.get(),
38         with_context->processName().c_str(), with_context->pid(),
39         exception.what());
40     else
41       XBT_LOG(prio, "%s %s: %s", context, name.get(), exception.what());
42
43     // Do we have a backtrace?
44     if (with_context != nullptr) {
45       auto backtrace = simgrid::xbt::resolveBacktrace(
46         with_context->backtrace().data(), with_context->backtrace().size());
47       for (std::string const& s : backtrace)
48         XBT_LOG(prio, "  -> %s", s.c_str());
49     }
50
51     // Do we have a nested exception?
52     auto with_nested = dynamic_cast<const std::nested_exception*>(&exception);
53     if (with_nested == nullptr ||  with_nested->nested_ptr() == nullptr)
54       return;
55     try {
56       with_nested->rethrow_nested();
57     }
58     catch (std::exception& nested_exception) {
59       logException(prio, "Caused by", nested_exception);
60     }
61     // We could catch nested_exception or WithContextException but we don't bother:
62     catch (...) {
63       XBT_LOG(prio, "Caused by an unknown exception");
64     }
65   }
66   catch (...) {
67     // Don't log exceptions we got when trying to log exception
68   }
69 }
70
71 }
72 }