refactor(error): use a helper class to format error popup+debug log on windows

This commit is contained in:
phaneron 2024-11-22 02:21:19 -05:00
parent e9df378771
commit 87a09b67a9

View file

@ -1,76 +1,94 @@
#include "storm/error/Error.hpp" #include "storm/error/Error.hpp"
#include "storm/Array.hpp"
#include <windows.h> #include <windows.h>
#include <cstdarg> #include <cstdarg>
#include <cstdlib> #include <cstdlib>
#include <cstdint>
#include <string> #include <string>
std::string errorf(const char *format, ...) { class error_report {
private:
TSGrowableArray<uint8_t> m_text;
uint32_t m_errorcode;
const char* m_filename;
int32_t m_linenumber;
const char* m_description;
int32_t m_recoverable;
void printf(const char *format, ...) {
va_list args; va_list args;
va_start(args, format); va_start(args, format);
constexpr size_t size = 1024; constexpr size_t size = 1024;
char buf[size] = {0}; char buf[size] = {0};
auto n = vsnprintf(buf, size, format, args); auto n = vsnprintf(buf, size, format, args);
va_end(args); va_end(args);
// Remove trailing zero
if (n < 0) { this->m_text.SetCount(this->m_text.Count()-1);
return ""; // Add formatted bytes plus trailing zero
} else if (n >= size) { this->m_text.Add(n+1, reinterpret_cast<uint8_t*>(buf));
std::string result(buf, n); }
return result; public:
} else { error_report(uint32_t errorcode, const char* filename, int32_t linenumber, const char* description, int32_t recoverable) {
return std::string(buf); this->m_errorcode = errorcode;
this->m_filename = filename;
this->m_linenumber = linenumber;
this->m_description = description;
this->m_recoverable = recoverable;
} }
}
int32_t SErrDisplayError(uint32_t errorcode, const char* filename, int32_t linenumber, const char* description, int32_t recoverable, uint32_t exitcode, uint32_t a7) { void Format() {
std::string s = ""; this->printf("\n=========================================================\n");
s.reserve(1024);
s += errorf("\n=========================================================\n");
if (linenumber == -5) { if (linenumber == -5) {
s += errorf("Exception Raised!\n\n"); this->printf("Exception Raised!\n\n");
s += errorf(" App: %s\n", "GenericBlizzardApp"); this->printf(" App: %s\n", "GenericBlizzardApp");
if (errorcode != 0x85100000) { if (errorcode != 0x85100000) {
s += errorf(" Error Code: 0x%08X\n", errorcode); this->printf(" Error Code: 0x%08X\n", errorcode);
} }
// TODO output time // TODO output time
s += errorf(" Error: %s\n\n", description); this->printf(" Error: %s\n\n", description);
} else { } else {
s += errorf("Assertion Failed!\n\n"); this->printf("Assertion Failed!\n\n");
s += errorf(" App: %s\n", "GenericBlizzardApp"); this->printf(" App: %s\n", "GenericBlizzardApp");
s += errorf(" File: %s\n", filename); this->printf(" File: %s\n", filename);
s += errorf(" Line: %d\n", linenumber); this->printf(" Line: %d\n", linenumber);
if (errorcode != 0x85100000) { if (errorcode != 0x85100000) {
s += errorf(" Error Code: 0x%08X\n", errorcode); this->printf(" Error Code: 0x%08X\n", errorcode);
} }
// TODO output time // TODO output time
this->printf(" Assertion: %s\n", description);
s += errorf(" Assertion: %s\n", description); }
} }
// Print error in debugger void Display() {
OutputDebugString(s.c_str()); // Output text to debugger
OutputDebugString(reinterpret_cast<LPCSTR>(this->m_text.Ptr()));
// Title // Title
const char* caption = recoverable ? "Error" : "Unrecoverable error"; const char* caption = recoverable ? "Error" : "Unrecoverable error";
// Icon/type // Icon/type
UINT icon = recoverable ? MB_ICONWARNING : MB_ICONERROR; UINT icon = this->m_recoverable ? MB_ICONWARNING : MB_ICONERROR;
MessageBox(nullptr, reinterpret_cast<LPCSTR>(this->m_text.Ptr()), caption, icon);
}
};
MessageBox(nullptr, s.c_str(), caption, icon); int32_t SErrDisplayError(uint32_t errorcode, const char* filename, int32_t linenumber, const char* description, int32_t recoverable, uint32_t exitcode, uint32_t a7) {
error_report report(errorcode, filename, linenumber, description, recoverable);
// Format error string and write to debug console
report.Format();
// Show MessageBox (blocks until user response)
report.Display();
if (recoverable) { if (recoverable) {
return 1; return 1;