ifw-daq  2.1.0-pre1
IFW Data Acquisition modules
report.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @ingroup daq_error
4  * @copyright ESO - European Southern Observatory
5  */
6 #include <daq/error/report.hpp>
7 
8 #include <ostream>
9 #include <sstream>
10 #include <string>
11 
12 namespace daq::error {
13 
14 namespace {
15 
16 void PrintNestedException(std::ostream& os, const std::nested_exception& e, unsigned level = 0);
17 
18 void 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 
31 void 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 
46 void ReportNestedExceptions(std::ostream& os, std::exception const& e) noexcept {
47  PrintNestedException(os, e);
48 }
49 
50 void 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 
65 void 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 
79 void 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 
91 std::string FormatException(std::exception_ptr ptr) {
92  std::stringstream ss;
93  FormatException(ss, std::move(ptr));
94  return ss.str();
95 }
96 
97 std::string NestedExceptionReporter::Str() const {
98  std::stringstream ss;
99  ss << *this;
100  return ss.str();
101 }
102 } // namespace daq::error
daq::error
Definition: json.hpp:15
daq::error::ReportNestedExceptions
void ReportNestedExceptions(std::ostream &os) noexcept
Definition: report.cpp:50
daq::error::NestedExceptionReporter::Str
std::string Str() const
Convenience function for constructing a std::string from the exception.
Definition: report.cpp:97
report.hpp
daq::error::FormatException
void FormatException(std::ostream &os, std::exception_ptr ptr)
Report without nesting.
Definition: report.cpp:79