AsmGrader 0.0.0
Loading...
Searching...
No Matches
trace_exception.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "common/extra_formatters.hpp" // IWYU pragma: keep
4
5#include <boost/stacktrace/stacktrace.hpp>
6#include <fmt/base.h>
7#include <fmt/format.h>
8#include <fmt/ostream.h>
9
10#include <concepts>
11#include <exception>
12#include <functional>
13#include <iostream>
14#include <optional>
15#include <string>
16#include <type_traits>
17#include <utility>
18
19namespace asmgrader {
20
21template <typename T>
22void trace_exception(const T& exception) {
23 // FIXME: strange bug where boost::stacktrace enters an infinite fork loop @
24 // boost::stacktrace::detail::addr2line_pipe::addr2line_pipe
25
26 // boost::stacktrace::stacktrace trace = boost::stacktrace::stacktrace::from_current_exception();
27 std::string except_str = fmt::format("Unhandled exception: {}", exception);
28 fmt::println(std::cerr, "{}", except_str);
29 fmt::println(std::cerr, "{}", std::string(except_str.size(), '='));
30
31 // std::string stacktrace_str = fmt::to_string(fmt::streamed(trace));
32 // fmt::println(std::cerr, "Stacktrace:\n{}", stacktrace_str.empty() ? " <unavailable>" : stacktrace_str);
33}
34
35template <typename Func, typename... Args>
36 requires(std::invocable<Func, Args...>)
37std::optional<std::invoke_result_t<Func, Args...>> wrap_throwable_fn(Func&& fn, Args&&... args) {
38 try {
39 return std::invoke(std::forward<Func>(fn), std::forward<Args>(args)...);
40 } catch (const std::exception& ex) {
42 } catch (...) {
43 trace_exception("<unknown - not derived from std::exception>");
44 }
45
46 return std::nullopt;
47}
48
49} // namespace asmgrader
Definition asm_buffer.hpp:20
void trace_exception(const T &exception)
Definition trace_exception.hpp:22
std::optional< std::invoke_result_t< Func, Args... > > wrap_throwable_fn(Func &&fn, Args &&... args)
Definition trace_exception.hpp:37