Porytiles
Loading...
Searching...
No Matches
user_diagnostics.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <ranges>
4#include <string>
5
9
10namespace porytiles2 {
11
31 public:
32 virtual ~UserDiagnostics() = default;
33
45 void note(const std::string &tag, const std::string &msg) const
46 {
47 note(tag, std::vector{msg});
48 }
49
60 virtual void note(const std::string &tag, const std::vector<std::string> &lines) const = 0;
61
72 void warn_note(const std::string &tag, const std::string &msg) const
73 {
74 warn_note(tag, std::vector{msg});
75 }
76
87 virtual void warn_note(const std::string &tag, const std::vector<std::string> &lines) const = 0;
88
99 void warn(const std::string &tag, const std::string &msg) const
100 {
101 warn(tag, std::vector{msg});
102 }
103
114 virtual void warn(const std::string &tag, const std::vector<std::string> &lines) const = 0;
115
116 // /**
117 // * @brief Display a tagged warning message using a formatter-aware builder function.
118 // *
119 // * @details
120 // * This overload allows callers to provide a function that dynamically generates warning messages with access to
121 // * text formatting capabilities. The TextFormatter is provided to enable conditional styling based on TTY output
122 // * settings.
123 // *
124 // * @param tag Categorization tag for the warning
125 // * @param msg_builder Function that receives a TextFormatter reference and returns formatted message lines
126 // */
127 // void warn(const std::string &tag, const FormattedMessageBuilder &msg_builder) const
128 // {
129 // // TODO: inject this formatter
130 // AnsiStyledTextFormatter formatter{};
131 // warn(tag, msg_builder(formatter));
132 // }
133
145 void err(const std::string &tag, const std::string &msg) const
146 {
147 err(tag, std::vector{msg});
148 }
149
160 virtual void err(const std::string &tag, const std::vector<std::string> &lines) const = 0;
161
171 virtual void emit_fatal_proximate(const Error &err) const = 0;
172
182 virtual void emit_fatal_step(const Error &err) const = 0;
183
193 virtual void emit_fatal_root(const Error &err) const = 0;
194
216 template <typename T, typename E>
217 void fatal(const ChainableResult<T, E> &result) const
218 {
219 // Preconditions
220 const auto &chain = result.chain();
221 assert_or_panic(!result.has_value(), "result was not of error type");
222 assert_or_panic(!chain.empty(), "error chain was empty");
223
224 // filter out FormattableErrors with no details
225 auto filtered_view = chain | std::views::filter([](const auto &err) {
226 const auto formattable_err = dynamic_cast<const FormattableError *>(err.get());
227 if (formattable_err == nullptr) {
228 return true;
229 }
230 return formattable_err->has_details();
231 });
232
233 std::vector<const Error *> filtered_chain;
234 for (const auto &err : filtered_view) {
235 filtered_chain.push_back(err.get());
236 }
237
238 // If all errors were blank FormattableErrors (defensive, shouldn't happen), return early
239 if (filtered_chain.empty()) {
240 panic("filtered error chain was empty, there should always be at least one FormattableError with details");
241 }
242
243 emit_fatal_proximate(*filtered_chain.at(0));
244 if (filtered_chain.size() > 1) {
245 // Emit steps for all but the first and last
246 const auto middle_range =
247 std::ranges::views::drop(filtered_chain, 1) | std::ranges::views::take(filtered_chain.size() - 2);
248 for (const auto *err : middle_range) {
250 }
251 // Emit the last one as root
252 emit_fatal_root(*filtered_chain.back());
253 }
254 }
255};
256
257} // namespace porytiles2
A result type that maintains a chainable sequence of errors for debugging and error reporting.
const std::vector< std::unique_ptr< Error > > & chain() const
Returns the complete error chain.
bool has_value() const
Checks whether the result contains a success value.
Abstract interface for all error types used in ChainableResult error chains.
Definition error.hpp:26
General-purpose error implementation with formatted message support.
Definition error.hpp:117
bool has_details() const
Checks whether this FormattableError contains any message content.
Definition error.hpp:270
Abstract interface for structured error reporting and diagnostic output.
void fatal(const ChainableResult< T, E > &result) const
Display a fatal error with complete error chain visualization.
virtual void emit_fatal_proximate(const Error &err) const =0
Emit the proximate (immediate) error in a fatal error chain.
virtual void warn(const std::string &tag, const std::vector< std::string > &lines) const =0
Display a multi-line tagged warning message.
virtual void note(const std::string &tag, const std::vector< std::string > &lines) const =0
Display a multi-line tagged informational note message.
virtual void emit_fatal_root(const Error &err) const =0
Emit the root cause error in a fatal error chain.
virtual void warn_note(const std::string &tag, const std::vector< std::string > &lines) const =0
Display a multi-line tagged warning note message.
void err(const std::string &tag, const std::string &msg) const
Display a tagged error message.
void warn(const std::string &tag, const std::string &msg) const
Display a tagged warning message.
void note(const std::string &tag, const std::string &msg) const
Display a tagged informational note message.
virtual ~UserDiagnostics()=default
virtual void err(const std::string &tag, const std::vector< std::string > &lines) const =0
Display a multi-line tagged error message.
void warn_note(const std::string &tag, const std::string &msg) const
Display a note message with a warning tag.
virtual void emit_fatal_step(const Error &err) const =0
Emit an intermediate step error in a fatal error chain.
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.
Definition panic.hpp:53
void assert_or_panic(const bool condition, const StringViewSourceLoc &s)
Conditionally panics if the given condition is false.
Definition panic.hpp:70