Improved Error system
Former-commit-id: ddd08841d30575713f4a28ac02566f92791e5539
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Core/Log.hpp>
|
||||
#include <cstdlib>
|
||||
#include <stdexcept>
|
||||
|
||||
#if defined(NAZARA_PLATFORM_WINDOWS)
|
||||
#include <windows.h>
|
||||
@@ -14,29 +15,76 @@
|
||||
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
void NzError(nzErrorType type, const NzString& error, unsigned int line, const char* file, const char* function)
|
||||
void NzError::Error(nzErrorType type, const NzString& error)
|
||||
{
|
||||
NazaraLog->WriteError(type, error, line, file, function);
|
||||
if ((s_flags & nzErrorFlag_Silent) == 0 || (s_flags & nzErrorFlag_SilentDisabled) != 0)
|
||||
NazaraLog->WriteError(type, error);
|
||||
|
||||
s_lastError = error;
|
||||
s_lastErrorFile = "";
|
||||
s_lastErrorFunction = "";
|
||||
s_lastErrorLine = 0;
|
||||
|
||||
#if NAZARA_CORE_EXIT_ON_ASSERT_FAILURE
|
||||
if (type == nzErrorType_AssertFailed)
|
||||
std::exit(EXIT_FAILURE);
|
||||
#endif
|
||||
|
||||
if ((s_flags & nzErrorFlag_ThrowException) != 0 && (s_flags & nzErrorFlag_ThrowExceptionDisabled) == 0)
|
||||
throw std::runtime_error(error);
|
||||
}
|
||||
|
||||
unsigned int NzGetLastSystemErrorCode()
|
||||
void NzError::Error(nzErrorType type, const NzString& error, unsigned int line, const char* file, const char* function)
|
||||
{
|
||||
if ((s_flags & nzErrorFlag_Silent) == 0 || (s_flags & nzErrorFlag_SilentDisabled) != 0)
|
||||
NazaraLog->WriteError(type, error, line, file, function);
|
||||
|
||||
s_lastError = error;
|
||||
s_lastErrorFile = file;
|
||||
s_lastErrorFunction = function;
|
||||
s_lastErrorLine = line;
|
||||
|
||||
#if NAZARA_CORE_EXIT_ON_ASSERT_FAILURE
|
||||
if (type == nzErrorType_AssertFailed)
|
||||
std::exit(EXIT_FAILURE);
|
||||
#endif
|
||||
|
||||
if ((s_flags & nzErrorFlag_ThrowException) != 0 && (s_flags & nzErrorFlag_ThrowExceptionDisabled) == 0)
|
||||
throw std::runtime_error(error);
|
||||
}
|
||||
|
||||
nzUInt32 NzError::GetFlags()
|
||||
{
|
||||
return s_flags;
|
||||
}
|
||||
|
||||
NzString NzError::GetLastError(const char** file, unsigned int* line, const char** function)
|
||||
{
|
||||
if (file)
|
||||
*file = s_lastErrorFile;
|
||||
|
||||
if (line)
|
||||
*line = s_lastErrorLine;
|
||||
|
||||
if (function)
|
||||
*function = s_lastErrorFunction;
|
||||
|
||||
return s_lastError;
|
||||
}
|
||||
|
||||
unsigned int NzError::GetLastSystemErrorCode()
|
||||
{
|
||||
#if defined(NAZARA_PLATFORM_WINDOWS)
|
||||
return GetLastError();
|
||||
return ::GetLastError();
|
||||
#elif defined(NAZARA_PLATFORM_POSIX)
|
||||
return errno;
|
||||
#else
|
||||
#error GetLastSystemErrorCode is not implemented on this platform
|
||||
#endif
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
NzString NzGetLastSystemError(unsigned int code)
|
||||
NzString NzError::GetLastSystemError(unsigned int code)
|
||||
{
|
||||
#if defined(NAZARA_PLATFORM_WINDOWS)
|
||||
wchar_t* buffer = nullptr;
|
||||
@@ -60,6 +108,18 @@ NzString NzGetLastSystemError(unsigned int code)
|
||||
#else
|
||||
#error GetLastSystemError is not implemented on this platform
|
||||
|
||||
return "GetLastSystemError is not implemented on this platform";
|
||||
return NzString("GetLastSystemError is not implemented on this platform");
|
||||
#endif
|
||||
}
|
||||
|
||||
void NzError::SetFlags(nzUInt32 flags)
|
||||
{
|
||||
s_flags = flags;
|
||||
}
|
||||
|
||||
nzUInt32 NzError::s_flags = nzErrorFlag_None;
|
||||
NzString NzError::s_lastError;
|
||||
const char* NzError::s_lastErrorFunction = "";
|
||||
const char* NzError::s_lastErrorFile = "";
|
||||
unsigned int NzError::s_lastErrorLine = 0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user