ifw-daq 3.1.0
IFW Data Acquisition modules
Loading...
Searching...
No Matches
report.hpp
Go to the documentation of this file.
1/**
2 * @file
3 * @ingroup daq_error
4 * @copyright ESO - European Southern Observatory
5 */
6#ifndef DAQ_ERROR_REPORT_HPP
7#define DAQ_ERROR_REPORT_HPP
8
9#include <exception>
10#include <iosfwd>
11
12namespace daq::error {
13
14void ReportNestedExceptions(std::ostream& os) noexcept;
15void ReportNestedExceptions(std::ostream& os, std::exception const& e) noexcept;
16
17
18/**
19 * Report nested exception(s) in @a exception messages to @a os.
20 *
21 * If @a ptr does not hold an exception this function has no effect.
22 *
23 * @param os output stream to report to.
24 * @param ptr exception (possibly nested) to report.
25 */
26void ReportNestedExceptions(std::ostream& os, std::exception_ptr ptr);
27
28/**
29 * Report without nesting.
30 *
31 * @param os output stream to report to.
32 * @param ptr exception (possibly nested) to report.
33 */
34void FormatException(std::ostream& os, std::exception_ptr ptr);
35
36/**
37 * Return formatted exception.
38 *
39 * @param ptr exception (possibly nested) to report.
40 * @return formatted string (mainly std::exception::what())
41 */
42std::string FormatException(std::exception_ptr ptr);
43
44
45/**
46 * Adapter object intended to be used in contexts without direct access to the output-stream object.
47 *
48 * try {
49 * MaybeThrows();
50 * } catch (std::exception const& e) {
51 * LOG4CPLUS_ERROR(logger, "MaybeThrow failed with:\n" << NestedExceptionReporter(e));
52 * }
53 */
55public:
56 /**
57 * Construct from exception derived from std::exception.
58 */
59 explicit NestedExceptionReporter(std::exception const& exception) noexcept
60 : m_ptr(), m_exception(&exception){};
61 /**
62 * Construct from exception_ptr.
63 */
64 explicit NestedExceptionReporter(std::exception_ptr ptr) noexcept
65 : m_ptr(std::move(ptr)), m_exception(nullptr){};
66
67 /**
68 * Convenience function for constructing a std::string from the exception.
69 *
70 * @return Formatted string
71 */
72 std::string Str() const;
73
74 /**
75 * Formats exception from @a reporter using ReportNestedExceptions.
76 *
77 * @param os output stream to output to.
78 * @param reporter Reporter adapter to format.
79 * @returns os
80 */
81 friend std::ostream& operator<<(std::ostream& os, NestedExceptionReporter const& reporter) {
82 if (reporter.m_ptr) {
83 ReportNestedExceptions(os, reporter.m_ptr);
84 } else if (reporter.m_exception) {
85 ReportNestedExceptions(os, *reporter.m_exception);
86 }
87 return os;
88 }
89
90private:
91 // A variant or union would occupy same space so this was preferred over
92 // including <variant>
93 std::exception_ptr m_ptr;
94 std::exception const* m_exception;
95};
96
97
98
99} // namespace daq::error
100#endif // #ifndef DAQ_ERROR_REPORT_HPP
Adapter object intended to be used in contexts without direct access to the output-stream object.
Definition: report.hpp:54
NestedExceptionReporter(std::exception const &exception) noexcept
Construct from exception derived from std::exception.
Definition: report.hpp:59
friend std::ostream & operator<<(std::ostream &os, NestedExceptionReporter const &reporter)
Formats exception from reporter using ReportNestedExceptions.
Definition: report.hpp:81
NestedExceptionReporter(std::exception_ptr ptr) noexcept
Construct from exception_ptr.
Definition: report.hpp:64
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