Skip to content

Commit

Permalink
add getReasonLiteral() on TPanic
Browse files Browse the repository at this point in the history
currently it only returns the format string.
  • Loading branch information
pixelflinger committed May 17, 2024
1 parent 813e6f8 commit 7d80975
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
15 changes: 9 additions & 6 deletions libs/utils/include/utils/Panic.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ class UTILS_PUBLIC Panic {
*/
virtual const char* getReason() const noexcept = 0;

/**
* You know who you are.
*/
virtual const char* getReasonLiteral() const noexcept = 0;

/**
* Get the function name where the panic was detected. On debug build the fully qualified
* function name is returned; on release builds only the function name is.
Expand Down Expand Up @@ -335,6 +340,7 @@ class UTILS_PUBLIC TPanic : public Panic {

// Panic interface
const char* getReason() const noexcept override;
const char* getReasonLiteral() const noexcept override;
const char* getFunction() const noexcept override;
const char* getFile() const noexcept override;
int getLine() const noexcept override;
Expand Down Expand Up @@ -374,11 +380,6 @@ class UTILS_PUBLIC TPanic : public Panic {
}

protected:
/**
* Creates a Panic.
* @param reason a description of the cause of the error
*/
explicit TPanic(std::string reason);

/**
* Creates a Panic with extra information about the error-site.
Expand All @@ -387,7 +388,8 @@ class UTILS_PUBLIC TPanic : public Panic {
* @param line the line in the above file where the error was detected
* @param reason a description of the cause of the error
*/
TPanic(char const* function, char const* file, int line, std::string reason);
TPanic(char const* function, char const* file, int line,
std::string reason, std::string reasonLiteral);

~TPanic() override;

Expand All @@ -396,6 +398,7 @@ class UTILS_PUBLIC TPanic : public Panic {

CallStack m_callstack;
std::string m_reason;
std::string m_reason_literal;
char const* const m_function = nullptr;
char const* const m_file = nullptr;
const int m_line = -1;
Expand Down
25 changes: 12 additions & 13 deletions libs/utils/src/Panic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,16 @@ void Panic::setPanicHandler(PanicHandlerCallback handler, void* user) noexcept {
// ------------------------------------------------------------------------------------------------

template<typename T>
TPanic<T>::TPanic(std::string reason) :
m_reason(std::move(reason)) {
TPanic<T>::TPanic(const char* function, const char* file, int line,
std::string reason, std::string reasonLiteral)
: m_reason(std::move(reason)), m_reason_literal(std::move(reasonLiteral)),
m_function(function), m_file(file), m_line(line) {
m_callstack.update(1);
buildMessage();
}

template<typename T>
TPanic<T>::TPanic(const char* function, const char* file, int line, std::string reason)
: m_reason(std::move(reason)), m_function(function), m_file(file), m_line(line) {
m_callstack.update(1);
buildMessage();
}

template<typename T>
TPanic<T>::~TPanic() {
}
TPanic<T>::~TPanic() = default;

template<typename T>
const char* TPanic<T>::what() const noexcept {
Expand All @@ -150,6 +144,11 @@ const char* TPanic<T>::getReason() const noexcept {
return m_reason.c_str();
}

template<typename T>
const char* TPanic<T>::getReasonLiteral() const noexcept {
return m_reason_literal.c_str();
}

template<typename T>
const char* TPanic<T>::getFunction() const noexcept {
return m_function;
Expand Down Expand Up @@ -197,9 +196,9 @@ template<typename T>
void TPanic<T>::panic(char const* function, char const* file, int line, const char* format, ...) {
va_list args;
va_start(args, format);
std::string const reason(formatString(format, args));
std::string reason(formatString(format, args));
va_end(args);
T e(function, formatFile(file), line, reason);
T e(function, formatFile(file), line, std::move(reason), format);

// always log the Panic at the point it is detected
e.log();
Expand Down

0 comments on commit 7d80975

Please sign in to comment.