ifw-daq 3.1.0
IFW Data Acquisition modules
Loading...
Searching...
No Matches
report.cpp
Go to the documentation of this file.
1/**
2 * @file
3 * @ingroup daq_error
4 * @copyright ESO - European Southern Observatory
5 */
7
8#include <ostream>
9#include <sstream>
10#include <string>
11
12namespace daq::error {
13
14namespace {
15
16void PrintNestedException(std::ostream& os, const std::nested_exception& e, unsigned level = 0);
17
18void PrintNestedException(std::ostream& os, const std::exception& e, unsigned level = 0) {
19 os << '[' << level << "]: " << e.what() << "\n";
20 try {
21 std::rethrow_if_nested(e);
22 } catch (const std::exception& nested) {
23 PrintNestedException(os, nested, level + 1);
24 } catch (std::nested_exception const& nested) {
25 PrintNestedException(os, nested, level + 1);
26 } catch (...) {
27 os << std::string(level + 1, ' ') << "unknown error\n";
28 }
29}
30
31void PrintNestedException(std::ostream& os, const std::nested_exception& e, unsigned level) {
32 try {
33 if (e.nested_ptr()) {
34 e.rethrow_nested();
35 }
36 } catch (const std::exception& e) {
37 PrintNestedException(os, e, level + 1);
38 } catch (std::nested_exception const& e) {
39 PrintNestedException(os, e, level + 1);
40 } catch (...) {
41 os << std::string(level + 1, ' ') << "unknown error\n";
42 }
43}
44} // namespace
45
46void ReportNestedExceptions(std::ostream& os, std::exception const& e) noexcept {
47 PrintNestedException(os, e);
48}
49
50void ReportNestedExceptions(std::ostream& os) noexcept {
51 try {
52 auto ptr = std::current_exception();
53 if (ptr) {
54 std::rethrow_exception(std::move(ptr));
55 }
56 } catch (std::exception const& e) {
57 PrintNestedException(os, e);
58 } catch (std::nested_exception const& e) {
59 PrintNestedException(os, e);
60 } catch (...) {
61 os << "unknown error";
62 }
63}
64
65void ReportNestedExceptions(std::ostream& os, std::exception_ptr ptr) {
66 try {
67 if (ptr) {
68 std::rethrow_exception(std::move(ptr));
69 }
70 } catch (std::exception const& e) {
71 PrintNestedException(os, e);
72 } catch (std::nested_exception const& e) {
73 PrintNestedException(os, e);
74 } catch (...) {
75 os << "unknown error";
76 }
77}
78
79void FormatException(std::ostream& os, std::exception_ptr ptr) {
80 try {
81 if (ptr) {
82 std::rethrow_exception(std::move(ptr));
83 }
84 } catch (std::exception const& e) {
85 os << e.what();
86 } catch (...) {
87 os << "unknown error";
88 }
89}
90
91std::string FormatException(std::exception_ptr ptr) {
92 std::stringstream ss;
93 FormatException(ss, std::move(ptr));
94 return ss.str();
95}
96
97std::string NestedExceptionReporter::Str() const {
98 std::stringstream ss;
99 ss << *this;
100 return ss.str();
101}
102} // namespace daq::error
std::string Str() const
Convenience function for constructing a std::string from the exception.
Definition: report.cpp:97
void ReportNestedExceptions(std::ostream &os) noexcept
Definition: report.cpp:50
void FormatException(std::ostream &os, std::exception_ptr ptr)
Report without nesting.
Definition: report.cpp:79