feat(storm): reorganized error library, assertions are now more detailed across different platforms; added new macros STORM_VALIDATE_STRING and STORM_PANIC

This commit is contained in:
phaneron 2024-08-30 18:04:21 -04:00
parent d149081f4e
commit 4f32840fdf
13 changed files with 164 additions and 103 deletions

View file

@ -77,12 +77,14 @@ pub fn build(b: *std.Build) void {
};
const storm_macos_sources = [_][]const u8{
"storm/error/unix/Display.cpp",
"storm/thread/mac/S_Thread.mm",
"storm/thread/mac/SThreadRunner.mm",
"storm/thread/mac/Thread.mm"
};
const storm_linux_sources = [_][]const u8{
"storm/error/unix/Display.cpp",
"storm/thread/linux/S_Thread.cpp",
"storm/thread/linux/Thread.cpp"
};

View file

@ -25,6 +25,7 @@ if(WHOA_SYSTEM_MAC)
file(GLOB STORM_MAC_SOURCES
"mac/*.cpp"
"mac/*.mm"
"error/unix/*.cpp"
"thread/mac/*.cpp"
"thread/mac/*.mm"
)
@ -34,6 +35,7 @@ endif()
if(WHOA_SYSTEM_LINUX)
file(GLOB STORM_LINUX_SOURCES
"linux/*.cpp"
"error/unix/*.cpp"
"thread/linux/*.cpp"
)
list(APPEND STORM_SOURCES ${STORM_LINUX_SOURCES})

View file

@ -1,68 +1,24 @@
#include "storm/Error.hpp"
#include "storm/error/Error.hpp"
#include "storm/Thread.hpp"
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#if defined(WHOA_SYSTEM_WIN)
#include <Windows.h>
#endif
static uint32_t s_lasterror = ERROR_SUCCESS;
#if !defined(WHOA_SYSTEM_WIN)
[[noreturn]] void SErrDisplayAppFatal(const char* format, ...) {
// Format arguments
constexpr size_t size = 1024;
char buffer[size] = {0};
va_list args;
va_start(args, format);
vprintf(format, args);
printf("\n");
vsnprintf(buffer, size, format, args);
va_end(args);
exit(EXIT_FAILURE);
SErrDisplayError(STORM_ERROR_APPLICATION_FATAL, s_appFatInfo.filename, s_appFatInfo.linenumber, buffer, 0, 1, 0);
}
int32_t SErrDisplayError(uint32_t errorcode, const char* filename, int32_t linenumber, const char* description, int32_t recoverable, uint32_t exitcode, uint32_t a7) {
// TODO
printf("\n=========================================================\n");
if (linenumber == -5) {
printf("Exception Raised!\n\n");
printf(" App: %s\n", "GenericBlizzardApp");
if (errorcode != 0x85100000) {
printf(" Error Code: 0x%08X\n", errorcode);
}
// TODO output time
printf(" Error: %s\n\n", description);
} else {
printf("Assertion Failed!\n\n");
printf(" App: %s\n", "GenericBlizzardApp");
printf(" File: %s\n", filename);
printf(" Line: %d\n", linenumber);
if (errorcode != 0x85100000) {
printf(" Error Code: 0x%08X\n", errorcode);
}
// TODO output time
printf(" Assertion: %s\n", description);
}
if (recoverable) {
return 1;
} else {
exit(exitcode);
}
}
#endif
int32_t SErrDisplayErrorFmt(uint32_t errorcode, const char* filename, int32_t linenumber, int32_t recoverable, uint32_t exitcode, const char* format, ...) {
char buffer[2048];
@ -76,14 +32,16 @@ int32_t SErrDisplayErrorFmt(uint32_t errorcode, const char* filename, int32_t li
}
void SErrPrepareAppFatal(const char* filename, int32_t linenumber) {
// TODO
s_appFatInfo.filename = filename;
s_appFatInfo.linenumber = linenumber;
s_appFatInfo.threadId = SGetCurrentThreadId();
}
void SErrSetLastError(uint32_t errorcode) {
s_lasterror = errorcode;
#if defined(WHOA_SYSTEM_WIN)
#if defined(WHOA_SYSTEM_WIN)
SetLastError(errorcode);
#endif
#endif
}
uint32_t SErrGetLastError() {

View file

@ -3,33 +3,8 @@
#include <cstdint>
#if defined(WHOA_SYSTEM_WIN)
#include <WinError.h>
#endif
#if defined(WHOA_SYSTEM_MAC) || defined(WHOA_SYSTEM_LINUX)
#define ERROR_SUCCESS 0x0
#define ERROR_INVALID_PARAMETER 0x57
#endif
#if defined(NDEBUG)
#define STORM_ASSERT(x) \
(void)0
#else
#define STORM_ASSERT(x) \
if (!(x)) { \
SErrPrepareAppFatal(__FILE__, __LINE__); \
SErrDisplayAppFatal(#x); \
} \
(void)0
#endif
#define STORM_VALIDATE(x, y, ...) \
if (!(x)) { \
SErrSetLastError(y); \
return __VA_ARGS__; \
} \
(void)0
#include "storm/error/Macro.hpp"
#include "storm/error/Codes.hpp"
[[noreturn]] void SErrDisplayAppFatal(const char* format, ...);

16
storm/error/Codes.hpp Normal file
View file

@ -0,0 +1,16 @@
#ifndef STORM_ERROR_CODES_HPP
#define STORM_ERROR_CODES_HPP
#if defined(WHOA_SYSTEM_WIN)
#include "storm/error/win/Codes.hpp"
#endif
#if defined(WHOA_SYSTEM_MAC) || defined(WHOA_SYSTEM_LINUX)
#include "storm/error/unix/Codes.hpp"
#endif
#include "storm/error/Macro.hpp"
#define STORM_ERROR_APPLICATION_FATAL STORM_ERROR(0x84)
#endif

6
storm/error/Error.cpp Normal file
View file

@ -0,0 +1,6 @@
#include "storm/error/Error.hpp"
#include "storm/error/Codes.hpp"
uint32_t s_lasterror = ERROR_SUCCESS;
APPFATINFO s_appFatInfo = {};

15
storm/error/Error.hpp Normal file
View file

@ -0,0 +1,15 @@
#ifndef STORM_ERROR_ERROR_HPP
#define STORM_ERROR_ERROR_HPP
#include <cstdint>
struct APPFATINFO {
const char *filename;
int32_t linenumber;
uintptr_t threadId;
};
extern uint32_t s_lasterror;
extern APPFATINFO s_appFatInfo;
#endif

35
storm/error/Macro.hpp Normal file
View file

@ -0,0 +1,35 @@
#ifndef STORM_ERROR_MACRO_HPP
#define STORM_ERROR_MACRO_HPP
#define STORM_NO_ERROR 0x85100000
#define STORM_ERROR(id) (STORM_NO_ERROR | (id & 0xFFFF))
// assertions
#if defined(NDEBUG)
#define STORM_ASSERT(x) \
(void)0
#else
#define STORM_ASSERT(x) \
if (!(x)) { \
SErrPrepareAppFatal(__FILE__, __LINE__); \
SErrDisplayAppFatal(#x); \
} \
(void)0
#endif
#define STORM_VALIDATE(x, y, ...) \
if (!(x)) { \
SErrSetLastError(y); \
return __VA_ARGS__; \
} \
(void)0
#define STORM_VALIDATE_STRING(x, y, ...) STORM_VALIDATE(x && *x, y, __VA_ARGS__)
#define STORM_PANIC(...) \
SErrPrepareAppFatal(__FILE__, __LINE__); \
SErrDisplayAppFatal(__VA_ARGS__); \
(void)0
#endif

View file

@ -0,0 +1,7 @@
#ifndef STORM_ERROR_UNIX_CODES_HPP
#define STORM_ERROR_UNIX_CODES_HPP
#define ERROR_INVALID_PARAMETER 0x57
#define ERROR_SUCCESS 0x0
#endif

View file

@ -0,0 +1,55 @@
#include "storm/Error.hpp"
#include <cstdio>
#include <cstdarg>
#include <cstdlib>
[[noreturn]] void SErrDisplayAppFatal(const char* format, ...) {
va_list args;
va_start(args, format);
vprintf(format, args);
printf("\n");
va_end(args);
exit(EXIT_FAILURE);
}
int32_t SErrDisplayError(uint32_t errorcode, const char* filename, int32_t linenumber, const char* description, int32_t recoverable, uint32_t exitcode, uint32_t a7) {
// TODO
printf("\n=========================================================\n");
if (linenumber == -5) {
printf("Exception Raised!\n\n");
printf(" App: %s\n", "GenericBlizzardApp");
if (errorcode != 0x85100000) {
printf(" Error Code: 0x%08X\n", errorcode);
}
// TODO output time
printf(" Error: %s\n\n", description);
} else {
printf("Assertion Failed!\n\n");
printf(" App: %s\n", "GenericBlizzardApp");
printf(" File: %s\n", filename);
printf(" Line: %d\n", linenumber);
if (errorcode != 0x85100000) {
printf(" Error Code: 0x%08X\n", errorcode);
}
// TODO output time
printf(" Assertion: %s\n", description);
}
if (recoverable) {
return 1;
} else {
exit(exitcode);
}
}

View file

@ -0,0 +1,6 @@
#ifndef STORM_ERROR_WIN_CODES_HPP
#define STORM_ERROR_WIN_CODES_HPP
#include <winerror.h>
#endif

View file

@ -1,9 +1,11 @@
#include "storm/Error.hpp"
#include "storm/error/Error.cpp"
#include <windows.h>
#include <cstdarg>
#include <cstdlib>
#include <string>
#include <windows.h>
std::string errorf(const char *format, ...) {
va_list args;
@ -26,24 +28,6 @@ std::string errorf(const char *format, ...) {
}
}
[[noreturn]] void SErrDisplayAppFatal(const char* format, ...) {
// Format arguments
constexpr size_t size = 1024;
char buffer[size] = {0};
va_list args;
va_start(args, format);
vsnprintf(buffer, size, format, args);
va_end(args);
// Print formatted message to debugger
OutputDebugString(buffer);
// Display error to GUI
MessageBox(nullptr, buffer, "Fatal error!", MB_ICONERROR);
exit(EXIT_FAILURE);
}
int32_t SErrDisplayError(uint32_t errorcode, const char* filename, int32_t linenumber, const char* description, int32_t recoverable, uint32_t exitcode, uint32_t a7) {
std::string s = "";
s.reserve(1024);

View file

@ -3,7 +3,7 @@
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <Windows.h>
#include <windows.h>
void GetExceptionNameWin32(DWORD dwMessageId, char* lpBuffer, DWORD nSize) {
switch (dwMessageId) {