Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 63 additions & 10 deletions storm/Error.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,58 @@
#include "storm/Error.hpp"
#include "storm/thread/SCritSect.hpp"
#include <cstdarg>
#include <cstdio>
#include <cstdlib>

#if defined(WHOA_SYSTEM_WIN)
#include <Windows.h>
#define StormGetThreadId() GetCurrentThreadId()
#else
#include <unistd.h>
#define StormGetThreadId() getpid()
#endif

struct APPFATINFO {
const char* filename;
int32_t linenumber;
uint32_t threadId;
};

static APPFATINFO s_appFatInfo;

SCritSect s_critsect;

static uint32_t s_lasterror = ERROR_SUCCESS;
static int32_t s_suppress;
static int32_t s_displaying;

[[noreturn]] void SErrDisplayAppFatalCustomV(uint32_t errorcode, const char* format, va_list va) {
const char* filename = nullptr;
int32_t linenumber = 0;
s_critsect.Enter();
if (s_appFatInfo.threadId == StormGetThreadId()) {
filename = s_appFatInfo.filename;
linenumber = s_appFatInfo.linenumber;
s_appFatInfo = {};
}
s_critsect.Leave();

char buffer[2048];
std::vsnprintf(buffer, sizeof(buffer) - 1, format, va);
buffer[sizeof(buffer) - 1] = '\0';

#ifdef WHOA_DISPLAY_ERR_EXTRA_ARG
SErrDisplayError(errorcode, filename, linenumber, buffer, 0, EXIT_FAILURE, 2);
#else
SErrDisplayError(errorcode, filename, linenumber, buffer, 0, EXIT_FAILURE);
#endif
std::exit(EXIT_FAILURE);
}

[[noreturn]] void STORMCDECL SErrDisplayAppFatal(const char* format, ...) {
va_list args;
va_start(args, format);
vprintf(format, args);
printf("\n");
va_end(args);

exit(EXIT_FAILURE);
SErrDisplayAppFatalCustomV(STORM_ERROR_FATAL_CONDITION, format, args);
}

#ifdef WHOA_DISPLAY_ERR_EXTRA_ARG
Expand Down Expand Up @@ -81,12 +115,35 @@ int32_t STORMCDECL SErrDisplayErrorFmt(uint32_t errorcode, const char* filename,
return SErrDisplayError(errorcode, filename, linenumber, buffer, recoverable, exitcode);
}

int32_t STORMAPI SErrGetErrorStr(uint32_t errorcode, char* buffer, uint32_t bufferchars) {
STORM_VALIDATE_BEGIN;
STORM_VALIDATE(buffer);
STORM_VALIDATE(bufferchars);
STORM_VALIDATE_END;

buffer[0] = '\0';
#if defined(WHOA_SYSTEM_WIN)
// half-measure
return FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, errorcode, LANG_USER_DEFAULT, buffer, bufferchars, nullptr);
#else
return 0;
#endif
}

uint32_t STORMAPI SErrGetLastError() {
return s_lasterror;
}

int32_t STORMAPI SErrIsDisplayingError() {
return s_displaying;
}

void STORMAPI SErrPrepareAppFatal(const char* filename, int32_t linenumber) {
// TODO
s_critsect.Enter();
s_appFatInfo.filename = filename;
s_appFatInfo.linenumber = linenumber;
s_appFatInfo.threadId = StormGetThreadId();
s_critsect.Leave();
}

void STORMAPI SErrSetLastError(uint32_t errorcode) {
Expand All @@ -96,10 +153,6 @@ void STORMAPI SErrSetLastError(uint32_t errorcode) {
#endif
}

uint32_t STORMAPI SErrGetLastError() {
return s_lasterror;
}

void STORMAPI SErrSuppressErrors(int32_t suppress) {
s_suppress = suppress;
}
6 changes: 4 additions & 2 deletions storm/Error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ int32_t STORMAPI SErrDisplayError(uint32_t errorcode, const char* filename, int3

int32_t STORMCDECL SErrDisplayErrorFmt(uint32_t errorcode, const char* filename, int32_t linenumber, int32_t recoverable, uint32_t exitcode, const char* format, ...);

int32_t STORMAPI SErrGetErrorStr(uint32_t errorcode, char* buffer, uint32_t bufferchars);

uint32_t STORMAPI SErrGetLastError();

int32_t STORMAPI SErrIsDisplayingError();

void STORMAPI SErrPrepareAppFatal(const char* filename, int32_t linenumber);

void STORMAPI SErrSetLastError(uint32_t errorcode);

uint32_t STORMAPI SErrGetLastError();

void STORMAPI SErrSuppressErrors(int32_t suppress);

#endif
9 changes: 9 additions & 0 deletions test/Error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ TEST_CASE("SErrDisplayErrorFmt", "[error]") {
}
}

TEST_CASE("SErrGetErrorStr", "[error]") {
SECTION("clears result buffer") {
// Use a fake error to almost guarantee it won't translate
char buffer[32] = "memes";
CHECK(SErrGetErrorStr(0x12345678, buffer, sizeof(buffer)) == 0);
CHECK(std::string(buffer) == "");
}
}

TEST_CASE("SErrIsDisplayingError", "[error]") {
SECTION("returns false by default") {
CHECK_FALSE(SErrIsDisplayingError());
Expand Down
2 changes: 1 addition & 1 deletion test/stormdll/storm.def
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ EXPORTS
; Error
;SErrDestroy @460 NONAME
SErrDisplayError @461 NONAME
;SErrGetErrorStr @462 NONAME
SErrGetErrorStr @462 NONAME
SErrGetLastError @463 NONAME
;SErrRegisterMessageSource @464 NONAME
SErrSetLastError @465 NONAME
Expand Down
3 changes: 2 additions & 1 deletion test/stormdll/stormstubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ void STORMAPI SBigToUnsigned(BigData*, uint32_t*) {}
void STORMCDECL SErrDisplayAppFatal(const char* format, ...) {}
int32_t STORMAPI SErrDisplayError(uint32_t, const char*, int32_t, const char*, int32_t, uint32_t) { return 0; }
int32_t STORMCDECL SErrDisplayErrorFmt(uint32_t, const char*, int32_t, int32_t, uint32_t, const char*, ...) { return 0; }
int32_t STORMAPI SErrGetErrorStr(uint32_t, char*, uint32_t) { return 0; }
uint32_t STORMAPI SErrGetLastError() { return 0; }
int32_t STORMAPI SErrIsDisplayingError() { return 0; }
void STORMAPI SErrPrepareAppFatal(const char*, int32_t) {}
void STORMAPI SErrSetLastError(uint32_t) {}
uint32_t STORMAPI SErrGetLastError() { return 0; }
void STORMAPI SErrSuppressErrors(int32_t) {}

#include <storm/Event.hpp>
Expand Down