diff options
author | Andrei Karas <akaras@inbox.ru> | 2016-04-30 02:03:32 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2016-04-30 02:03:32 +0300 |
commit | 244991dee7a2e274cb361f5040dd39b0f2997290 (patch) | |
tree | 1561cedb98b1f2df73fb49cc26f48eb14d01e520 | |
parent | 22795c4301cd6b62a8270b45a72a850ed888daf8 (diff) | |
download | ManaVerse-244991dee7a2e274cb361f5040dd39b0f2997290.tar.gz ManaVerse-244991dee7a2e274cb361f5040dd39b0f2997290.tar.bz2 ManaVerse-244991dee7a2e274cb361f5040dd39b0f2997290.tar.xz ManaVerse-244991dee7a2e274cb361f5040dd39b0f2997290.zip |
Extend assert reporting functions with condition text and call stack.
-rw-r--r-- | src/logger.cpp | 39 | ||||
-rw-r--r-- | src/logger.h | 21 | ||||
-rw-r--r-- | src/utils/checkutils.cpp | 20 | ||||
-rw-r--r-- | src/utils/checkutils.h | 28 |
4 files changed, 90 insertions, 18 deletions
diff --git a/src/logger.cpp b/src/logger.cpp index f61cd2464..395daa135 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -264,6 +264,45 @@ void Logger::log(const char *const log_text, ...) delete [] buf; } +void Logger::assert(const char *const log_text, ...) +{ + if (settings.disableLoggingInGame) + return; + + unsigned size = 1024; + if (strlen(log_text) * 3 > size) + size = CAST_U32(strlen(log_text) * 3); + + char* buf = new char[CAST_SIZE(size + 1)]; + va_list ap; + + // Use a temporary buffer to fill in the variables + va_start(ap, log_text); + vsnprintf(buf, size, log_text, ap); + buf[size] = 0; + va_end(ap); + + // Get the current system time + timeval tv; + gettimeofday(&tv, nullptr); + + // Print the log entry + std::stringstream timeStr; + DATESTREAM + SPECIALLOG(buf) + + if (mLogFile.is_open()) + mLogFile << timeStr.str() << buf << std::endl; + + if (mLogToStandardOut) + std::cout << timeStr.str() << buf << std::endl; + + DebugMessageListener::distributeEvent(buf); + + // Delete temporary buffer + delete [] buf; +} + void Logger::log_r(const char *const log_text, ...) { if (settings.disableLoggingInGame) diff --git a/src/logger.h b/src/logger.h index abf2a72c1..0088facc8 100644 --- a/src/logger.h +++ b/src/logger.h @@ -118,6 +118,27 @@ class Logger final ; /** + * Enters a message in the log. The message will be timestamped. + */ + void assert(const char *const log_text, ...) A_NONNULL(2) +#ifdef __GNUC__ +#ifdef __OpenBSD__ + + __attribute__((__format__(printf, 2, 3))) +#else // __OpenBSD__ + +#ifdef ENABLE_CILKPLUS + __attribute__((__format__(gnu_printf, 1, 2))) +#else // ENABLE_CILKPLUS + + __attribute__((__format__(gnu_printf, 2, 3))) +#endif // ENABLE_CILKPLUS + +#endif // __OpenBSD__ +#endif // __GNUC__ + ; + + /** * Enters a message in the log (thread safe). */ void log_r(const char *const log_text, ...) A_NONNULL(2) diff --git a/src/utils/checkutils.cpp b/src/utils/checkutils.cpp index 2fc471d7e..6cc3c751e 100644 --- a/src/utils/checkutils.cpp +++ b/src/utils/checkutils.cpp @@ -30,19 +30,25 @@ #include "debug.h" -bool reportFalseReal(const bool val, const char *const file, - const unsigned line) +bool reportFalseReal(const bool val, + const char *const text, + const char *const file, + const unsigned line, + const char *const func) { if (!val) - logger->log("Debug: false value at %s:%u", file, line); + reportStack(file, line, func, "Detected false value", text); return val; } -bool reportTrueReal(const bool val, const char *const file, - const unsigned line) +bool reportTrueReal(const bool val, + const char *const text, + const char *const file, + const unsigned line, + const char *const func) { if (val) - logger->log("Debug: true value at %s:%u", file, line); + reportStack(file, line, func, "Detected true value", text); return val; } @@ -61,7 +67,7 @@ void reportStack(const char *const file, logger->log("--- %s --------------------------------------------", name); - logger->log("%s:%u: '%s' in function `%s'", + logger->assert("%s:%u: '%s' in function `%s'", file, line, text, diff --git a/src/utils/checkutils.h b/src/utils/checkutils.h index 21f439743..539614a7a 100644 --- a/src/utils/checkutils.h +++ b/src/utils/checkutils.h @@ -23,17 +23,23 @@ #ifdef ENABLE_ASSERTS -#define reportFalse(val) reportFalse1(val, __FILE__, __LINE__) -#define reportFalse1(val, file, line) reportFalseReal(val, file, line) - -#define reportTrue(val) reportTrue1(val, __FILE__, __LINE__) -#define reportTrue1(val, file, line) reportTrueReal(val, file, line) - -bool reportFalseReal(const bool val, const char *const file, - const unsigned line); - -bool reportTrueReal(const bool val, const char *const file, - const unsigned line); +#define reportFalse(val) \ + reportFalseReal(val, #val, __FILE__, __LINE__, __func__) + +#define reportTrue(val) \ + reportTrueReal(val, #val, __FILE__, __LINE__, __func__) + +bool reportFalseReal(const bool val, + const char *const text, + const char *const file, + const unsigned line, + const char *const func); + +bool reportTrueReal(const bool val, + const char *const text, + const char *const file, + const unsigned line, + const char *const func); void reportStack(const char *const file, const unsigned int line, |