1 /* Copyright (c) 2005-2016. The SimGrid Team.
2 * All rights reserved. */
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. */
11 #include <xbt/backtrace.hpp>
12 #include <xbt/exception.hpp>
14 #include <xbt/log.hpp>
17 XBT_LOG_EXTERNAL_CATEGORY(xbt);
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_exception, xbt, "Exceptions");
24 WithContextException::~WithContextException() {}
27 e_xbt_log_priority_t prio,
28 const char* context, std::exception const& exception)
31 auto name = simgrid::xbt::demangle(typeid(exception).name());
34 dynamic_cast<const simgrid::xbt::WithContextException*>(&exception);
35 if (with_context != nullptr)
36 XBT_LOG(prio, "%s %s by %s/%d: %s",
38 with_context->processName().c_str(), with_context->pid(),
41 XBT_LOG(prio, "%s %s: %s", context, name.get(), exception.what());
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());
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)
56 with_nested->rethrow_nested();
58 catch (std::exception& nested_exception) {
59 logException(prio, "Caused by", nested_exception);
61 // We could catch nested_exception or WithContextException but we don't bother:
63 XBT_LOG(prio, "Caused by an unknown exception");
67 // Don't log exceptions we got when trying to log exception