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 boost::stacktrace::stacktrace trace = boost::stacktrace::stacktrace::from_current_exception();
24 std::string except_str = fmt::format("Unhandled exception: {}", exception);
25 fmt::println(std::cerr, "{}", except_str);
26 fmt::println(std::cerr, "{}", std::string(except_str.size(), '='));
27
28 std::string stacktrace_str = fmt::to_string(fmt::streamed(trace));
29 fmt::println(std::cerr, "Stacktrace:\n{}", stacktrace_str.empty() ? " <unavailable>" : stacktrace_str);
30}
31
32template <typename Func, typename... Args>
33 requires(std::invocable<Func, Args...>)
34std::optional<std::invoke_result_t<Func, Args...>> wrap_throwable_fn(Func&& fn, Args&&... args) {
35 try {
36 return std::invoke(std::forward<Func>(fn), std::forward<Args>(args)...);
37 } catch (const std::exception& ex) {
39 } catch (...) {
40 trace_exception("<unknown - not derived from std::exception>");
41 }
42
43 return std::nullopt;
44}
45
46} // namespace asmgrader
Definition asm_buffer.hpp:19
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:34