diff --git a/build.zig b/build.zig index a5fc27c..449ccf2 100644 --- a/build.zig +++ b/build.zig @@ -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" }; diff --git a/storm/CMakeLists.txt b/storm/CMakeLists.txt index ca2433d..1c63451 100644 --- a/storm/CMakeLists.txt +++ b/storm/CMakeLists.txt @@ -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}) diff --git a/storm/Error.cpp b/storm/Error.cpp index c7fae26..712353c 100644 --- a/storm/Error.cpp +++ b/storm/Error.cpp @@ -1,68 +1,24 @@ #include "storm/Error.hpp" +#include "storm/error/Error.hpp" + +#include "storm/Thread.hpp" + #include #include #include -#if defined(WHOA_SYSTEM_WIN) -#include -#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() { diff --git a/storm/Error.hpp b/storm/Error.hpp index 75e8b9b..542c826 100644 --- a/storm/Error.hpp +++ b/storm/Error.hpp @@ -3,33 +3,8 @@ #include -#if defined(WHOA_SYSTEM_WIN) -#include -#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, ...); diff --git a/storm/error/Codes.hpp b/storm/error/Codes.hpp new file mode 100644 index 0000000..0071a63 --- /dev/null +++ b/storm/error/Codes.hpp @@ -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 diff --git a/storm/error/Error.cpp b/storm/error/Error.cpp new file mode 100644 index 0000000..5760617 --- /dev/null +++ b/storm/error/Error.cpp @@ -0,0 +1,6 @@ +#include "storm/error/Error.hpp" +#include "storm/error/Codes.hpp" + +uint32_t s_lasterror = ERROR_SUCCESS; +APPFATINFO s_appFatInfo = {}; + diff --git a/storm/error/Error.hpp b/storm/error/Error.hpp new file mode 100644 index 0000000..e301aba --- /dev/null +++ b/storm/error/Error.hpp @@ -0,0 +1,15 @@ +#ifndef STORM_ERROR_ERROR_HPP +#define STORM_ERROR_ERROR_HPP + +#include + +struct APPFATINFO { + const char *filename; + int32_t linenumber; + uintptr_t threadId; +}; + +extern uint32_t s_lasterror; +extern APPFATINFO s_appFatInfo; + +#endif diff --git a/storm/error/Macro.hpp b/storm/error/Macro.hpp new file mode 100644 index 0000000..c3cd704 --- /dev/null +++ b/storm/error/Macro.hpp @@ -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 diff --git a/storm/error/unix/Codes.hpp b/storm/error/unix/Codes.hpp new file mode 100644 index 0000000..0a68df8 --- /dev/null +++ b/storm/error/unix/Codes.hpp @@ -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 diff --git a/storm/error/unix/Display.cpp b/storm/error/unix/Display.cpp new file mode 100644 index 0000000..6e9d0ea --- /dev/null +++ b/storm/error/unix/Display.cpp @@ -0,0 +1,55 @@ +#include "storm/Error.hpp" + +#include +#include +#include + +[[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); + } +} diff --git a/storm/error/win/Codes.hpp b/storm/error/win/Codes.hpp new file mode 100644 index 0000000..8ba26e3 --- /dev/null +++ b/storm/error/win/Codes.hpp @@ -0,0 +1,6 @@ +#ifndef STORM_ERROR_WIN_CODES_HPP +#define STORM_ERROR_WIN_CODES_HPP + +#include + +#endif diff --git a/storm/error/win/Display.cpp b/storm/error/win/Display.cpp index 0101db7..b982cea 100644 --- a/storm/error/win/Display.cpp +++ b/storm/error/win/Display.cpp @@ -1,9 +1,11 @@ #include "storm/Error.hpp" +#include "storm/error/Error.cpp" + +#include #include #include #include -#include 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); diff --git a/storm/error/win/Error.cpp b/storm/error/win/Error.cpp index 7dabc28..f3b6714 100644 --- a/storm/error/win/Error.cpp +++ b/storm/error/win/Error.cpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include void GetExceptionNameWin32(DWORD dwMessageId, char* lpBuffer, DWORD nSize) { switch (dwMessageId) {