Small fix

This commit is contained in:
itqop 2024-05-15 13:29:16 +03:00
parent 9339e572a0
commit 9019f9c299
2 changed files with 140 additions and 154 deletions

View File

@ -6,42 +6,42 @@
class LogType class LogType
{ {
public: public:
virtual void log(std::stringstream& ss) const = 0; virtual void log(std::stringstream &ss) const = 0;
}; };
class DefaultLog : public LogType class DefaultLog : public LogType
{ {
public: public:
void log(std::stringstream& ss) const override void log(std::stringstream &ss) const override
{ {
std::cout << ss.str() << "\n"; std::cout << ss.str() << "\n";
}; };
}; };
class ErrorLog : public LogType class ErrorLog: public LogType
{ {
public: public:
void log(std::stringstream& ss) const override void log(std::stringstream &ss) const override
{ {
#ifdef PLATFORM_WIN32 #ifdef PLATFORM_WIN32
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
std::cout << ss.str() << "\n"; std::cout << ss.str() << "\n";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
#else #else
std::cout << "Error: " << ss << "\n"; std::cout << "Error: " << ss << "\n";
#endif #endif
}; };
}; };
class LogToAFile : public LogType class LogToAFile: public LogType
{ {
std::string name; std::string name;
public: public:
LogToAFile(std::string name) :name(name) LogToAFile(std::string name):name(name)
{} {}
void log(std::stringstream& ss) const override void log(std::stringstream &ss) const override
{ {
std::ofstream f(name); std::ofstream f(name);
f << ss.str() << "\n"; f << ss.str() << "\n";
@ -51,20 +51,20 @@ public:
}; };
inline void llog(const LogType& l, std::stringstream& ss) inline void llog(const LogType &l, std::stringstream &ss)
{ {
l.log(ss); l.log(ss);
} }
template<class T, class ...T2> template<class T, class ...T2>
inline void llog(const LogType& l, std::stringstream& ss, const T& t, const T2 &...t2) inline void llog(const LogType &l, std::stringstream &ss, const T &t, const T2 &...t2)
{ {
ss << t << " "; ss << t << " ";
llog(l, ss, t2...); llog(l, ss, t2...);
} }
template<class T, class ...T2> template<class T, class ...T2>
inline void llog(const LogType& l, const T& t, const T2 &...t2) inline void llog(const LogType &l, const T &t, const T2 &...t2)
{ {
std::stringstream ss; std::stringstream ss;
ss << t << " "; ss << t << " ";

View File

@ -1,9 +1,7 @@
#ifndef TOOLS_H_INCLUDE #ifndef TOOLS_H_INCLUDE
#define TOOLS_H_INCLUDE #define TOOLS_H_INCLUDE
#define INTERNAL_BUILD 1 //when internal build is true and an assertion is hit, you have the option to debug or ignore #define INTERNAL_BUILD 1
//when internal build if false and an asertion is hit, the error is reported and the program
//closes
#include <csignal> #include <csignal>
@ -17,166 +15,154 @@
#ifdef PLATFORM_WIN32 #ifdef PLATFORM_WIN32
#include <Windows.h> #include <Windows.h>
inline void assertFuncProduction( inline void assertFuncProduction(
const char* expression, const char *expression,
const char* file_name, const char *file_name,
unsigned const line_number, unsigned const line_number,
const char* comment = "---") const char *comment = "---")
{
char c[1024] = {};
sprintf(c,
"Assertion failed\n\n"
"File:\n"
"%s\n\n"
"Line:\n"
"%u\n\n"
"Expresion:\n"
"%s\n\n"
"Comment:\n"
"%s"
"\n\nPlease report this error to the developer.",
file_name,
line_number,
expression,
comment
);
int const action = MessageBox(0, c, "Platform Layer", MB_TASKMODAL
| MB_ICONHAND | MB_OK | MB_SETFOREGROUND);
switch (action)
{ {
case IDOK: // Abort the program:
{
raise(SIGABRT);
// We won't usually get here, but it's possible that a user-registered char c[1024] = {};
// abort handler returns, so exit the program immediately. Note that
// even though we are "aborting," we do not call abort() because we do sprintf(c,
// not want to invoke Watson (the user has already had an opportunity "Assertion failed\n\n"
// to debug the error and chose not to). "File:\n"
_exit(3); "%s\n\n"
} "Line:\n"
default: "%u\n\n"
{ "Expresion:\n"
_exit(3); "%s\n\n"
} "Comment:\n"
"%s"
"\n\nPlease report this error to the developer.",
file_name,
line_number,
expression,
comment
);
int const action = MessageBox(0, c, "Platform Layer", MB_TASKMODAL
| MB_ICONHAND | MB_OK | MB_SETFOREGROUND);
switch (action)
{
case IDOK:
{
raise(SIGABRT);
_exit(3);
}
default:
{
_exit(3);
}
}
} }
} inline void assertFuncInternal(
const char *expression,
inline void assertFuncInternal( const char *file_name,
const char* expression, unsigned const line_number,
const char* file_name, const char *comment = "---")
unsigned const line_number,
const char* comment = "---")
{
char c[1024] = {};
sprintf(c,
"Assertion failed\n\n"
"File:\n"
"%s\n\n"
"Line:\n"
"%u\n\n"
"Expresion:\n"
"%s\n\n"
"Comment:\n"
"%s"
"\n\nPress retry to debug.",
file_name,
line_number,
expression,
comment
);
int const action = MessageBox(0, c, "Platform Layer", MB_TASKMODAL
| MB_ICONHAND | MB_ABORTRETRYIGNORE | MB_SETFOREGROUND);
switch (action)
{ {
case IDABORT: // Abort the program:
{
raise(SIGABRT);
// We won't usually get here, but it's possible that a user-registered char c[1024] = {};
// abort handler returns, so exit the program immediately. Note that
// even though we are "aborting," we do not call abort() because we do sprintf(c,
// not want to invoke Watson (the user has already had an opportunity "Assertion failed\n\n"
// to debug the error and chose not to). "File:\n"
_exit(3); "%s\n\n"
} "Line:\n"
case IDRETRY: // Break into the debugger then return control to caller "%u\n\n"
{ "Expresion:\n"
__debugbreak(); "%s\n\n"
return; "Comment:\n"
} "%s"
case IDIGNORE: // Return control to caller "\n\nPress retry to debug.",
{ file_name,
return; line_number,
} expression,
default: // This should not happen; treat as fatal error: comment
{ );
abort();
} int const action = MessageBox(0, c, "Platform Layer", MB_TASKMODAL
| MB_ICONHAND | MB_ABORTRETRYIGNORE | MB_SETFOREGROUND);
switch (action)
{
case IDABORT:
{
raise(SIGABRT);
_exit(3);
}
case IDRETRY:
{
__debugbreak();
return;
}
case IDIGNORE:
{
return;
}
default:
{
abort();
}
}
} }
} #if INTERNAL_BUILD == 1
#if INTERNAL_BUILD == 1 #define permaAssert(expression) (void)( \
#define permaAssert(expression) (void)( \
(!!(expression)) || \ (!!(expression)) || \
(assertFuncInternal(#expression, __FILE__, (unsigned)(__LINE__)), 0) \ (assertFuncInternal(#expression, __FILE__, (unsigned)(__LINE__)), 0) \
) )
#define permaAssertComment(expression, comment) (void)( \ #define permaAssertComment(expression, comment) (void)( \
(!!(expression)) || \ (!!(expression)) || \
(assertFuncInternal(#expression, __FILE__, (unsigned)(__LINE__), comment), 1) \ (assertFuncInternal(#expression, __FILE__, (unsigned)(__LINE__), comment), 1) \
) )
#else #else
#define permaAssert(expression) (void)( \ #define permaAssert(expression) (void)( \
(!!(expression)) || \ (!!(expression)) || \
(assertFuncProduction(#expression, __FILE__, (unsigned)(__LINE__)), 0) \ (assertFuncProduction(#expression, __FILE__, (unsigned)(__LINE__)), 0) \
) )
#define permaAssertComment(expression, comment) (void)( \ #define permaAssertComment(expression, comment) (void)( \
(!!(expression)) || \ (!!(expression)) || \
(assertFuncProduction(#expression, __FILE__, (unsigned)(__LINE__), comment), 1) \ (assertFuncProduction(#expression, __FILE__, (unsigned)(__LINE__), comment), 1) \
) )
#endif #endif
#else //linux or others #else
inline void assertFuncProduction( inline void assertFuncProduction(
const char* expression, const char *expression,
const char* file_name, const char *file_name,
unsigned const line_number, unsigned const line_number,
const char* comment = "---") const char *comment = "---")
{ {
raise(SIGABRT); raise(SIGABRT);
} }
#define permaAssert(expression) (void)( \ #define permaAssert(expression) (void)( \
(!!(expression)) || \ (!!(expression)) || \
(assertFuncProduction(#expression, __FILE__, (unsigned)(__LINE__)), 0) \ (assertFuncProduction(#expression, __FILE__, (unsigned)(__LINE__)), 0) \
) )
#define permaAssertComment(expression, comment) (void)( \ #define permaAssertComment(expression, comment) (void)( \
(!!(expression)) || \ (!!(expression)) || \
(assertFuncProduction(#expression, __FILE__, (unsigned)(__LINE__)), 0, comment) \ (assertFuncProduction(#expression, __FILE__, (unsigned)(__LINE__)), 0, comment) \
) )