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
{
public:
virtual void log(std::stringstream& ss) const = 0;
virtual void log(std::stringstream &ss) const = 0;
};
class DefaultLog : public LogType
{
public:
void log(std::stringstream& ss) const override
void log(std::stringstream &ss) const override
{
std::cout << ss.str() << "\n";
};
};
class ErrorLog : public LogType
class ErrorLog: public LogType
{
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);
std::cout << ss.str() << "\n";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
#else
#else
std::cout << "Error: " << ss << "\n";
#endif
#endif
};
};
class LogToAFile : public LogType
class LogToAFile: public LogType
{
std::string name;
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);
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);
}
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 << " ";
llog(l, ss, 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;
ss << t << " ";

View File

@ -1,9 +1,7 @@
#ifndef 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
//when internal build if false and an asertion is hit, the error is reported and the program
//closes
#define INTERNAL_BUILD 1
#include <csignal>
@ -17,166 +15,154 @@
#ifdef PLATFORM_WIN32
#include <Windows.h>
#include <Windows.h>
inline void assertFuncProduction(
const char* expression,
const char* file_name,
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\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)
inline void assertFuncProduction(
const char *expression,
const char *file_name,
unsigned const line_number,
const char *comment = "---")
{
case IDOK: // Abort the program:
{
raise(SIGABRT);
// We won't usually get here, but it's possible that a user-registered
// abort handler returns, so exit the program immediately. Note that
// even though we are "aborting," we do not call abort() because we do
// not want to invoke Watson (the user has already had an opportunity
// to debug the error and chose not to).
_exit(3);
}
default:
{
_exit(3);
}
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:
{
raise(SIGABRT);
_exit(3);
}
default:
{
_exit(3);
}
}
}
}
inline void assertFuncInternal(
const char* expression,
const char* file_name,
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)
inline void assertFuncInternal(
const char *expression,
const char *file_name,
unsigned const line_number,
const char *comment = "---")
{
case IDABORT: // Abort the program:
{
raise(SIGABRT);
// We won't usually get here, but it's possible that a user-registered
// abort handler returns, so exit the program immediately. Note that
// even though we are "aborting," we do not call abort() because we do
// not want to invoke Watson (the user has already had an opportunity
// to debug the error and chose not to).
_exit(3);
}
case IDRETRY: // Break into the debugger then return control to caller
{
__debugbreak();
return;
}
case IDIGNORE: // Return control to caller
{
return;
}
default: // This should not happen; treat as fatal error:
{
abort();
}
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:
{
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)) || \
(assertFuncInternal(#expression, __FILE__, (unsigned)(__LINE__)), 0) \
)
#define permaAssertComment(expression, comment) (void)( \
#define permaAssertComment(expression, comment) (void)( \
(!!(expression)) || \
(assertFuncInternal(#expression, __FILE__, (unsigned)(__LINE__), comment), 1) \
)
#else
#else
#define permaAssert(expression) (void)( \
#define permaAssert(expression) (void)( \
(!!(expression)) || \
(assertFuncProduction(#expression, __FILE__, (unsigned)(__LINE__)), 0) \
)
#define permaAssertComment(expression, comment) (void)( \
#define permaAssertComment(expression, comment) (void)( \
(!!(expression)) || \
(assertFuncProduction(#expression, __FILE__, (unsigned)(__LINE__), comment), 1) \
)
)
#endif
#endif
#else //linux or others
#else
inline void assertFuncProduction(
const char* expression,
const char* file_name,
unsigned const line_number,
const char* comment = "---")
{
inline void assertFuncProduction(
const char *expression,
const char *file_name,
unsigned const line_number,
const char *comment = "---")
{
raise(SIGABRT);
raise(SIGABRT);
}
}
#define permaAssert(expression) (void)( \
#define permaAssert(expression) (void)( \
(!!(expression)) || \
(assertFuncProduction(#expression, __FILE__, (unsigned)(__LINE__)), 0) \
)
#define permaAssertComment(expression, comment) (void)( \
#define permaAssertComment(expression, comment) (void)( \
(!!(expression)) || \
(assertFuncProduction(#expression, __FILE__, (unsigned)(__LINE__)), 0, comment) \
)