From 0bf6bd9175d34d4b6b1805064a3f7166d338c201 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 16 Aug 2020 14:11:56 +0200 Subject: [PATCH] More Log.h cleanup. --- Common/CommonFuncs.h | 1 - Common/ConsoleListener.cpp | 4 +- Common/ConsoleListener.h | 4 +- Common/Log.cpp | 39 ++++++++++---------- Common/Log.h | 75 ++++++++++++-------------------------- Common/LogManager.h | 1 + GPU/D3D11/D3D11Util.cpp | 4 +- GPU/GeDisasm.cpp | 5 ++- UI/NativeApp.cpp | 2 + UI/TextureUtil.cpp | 4 +- 10 files changed, 55 insertions(+), 84 deletions(-) diff --git a/Common/CommonFuncs.h b/Common/CommonFuncs.h index d9b4ea6d74..4e5adc8beb 100644 --- a/Common/CommonFuncs.h +++ b/Common/CommonFuncs.h @@ -25,7 +25,6 @@ #if !defined(_WIN32) - #include #include diff --git a/Common/ConsoleListener.cpp b/Common/ConsoleListener.cpp index 5b7217827a..7eab9980c9 100644 --- a/Common/ConsoleListener.cpp +++ b/Common/ConsoleListener.cpp @@ -49,8 +49,8 @@ HANDLE ConsoleListener::hTriggerEvent = NULL; CRITICAL_SECTION ConsoleListener::criticalSection; char *ConsoleListener::logPending = NULL; -std::atomic ConsoleListener::logPendingReadPos; -std::atomic ConsoleListener::logPendingWritePos; +std::atomic ConsoleListener::logPendingReadPos; +std::atomic ConsoleListener::logPendingWritePos; #endif ConsoleListener::ConsoleListener() : bHidden(true) diff --git a/Common/ConsoleListener.h b/Common/ConsoleListener.h index 66a612a849..976e7531cd 100644 --- a/Common/ConsoleListener.h +++ b/Common/ConsoleListener.h @@ -63,8 +63,8 @@ private: static CRITICAL_SECTION criticalSection; static char *logPending; - static std::atomic logPendingReadPos; - static std::atomic logPendingWritePos; + static std::atomic logPendingReadPos; + static std::atomic logPendingWritePos; int openWidth_; int openHeight_; diff --git a/Common/Log.cpp b/Common/Log.cpp index 4aff42ce37..dd0802ad86 100644 --- a/Common/Log.cpp +++ b/Common/Log.cpp @@ -30,41 +30,40 @@ #include "CommonWindows.h" #endif -#if defined(__ANDROID__) +#define LOG_BUF_SIZE 2048 -#define LOG_BUF_SIZE 1024 - -void AndroidAssert(const char *func, const char *file, int line, const char *condition, const char *fmt, ...) { - char buf[LOG_BUF_SIZE]; - va_list args; - va_start(args, fmt); - vsnprintf(buf, sizeof(buf), fmt, args); - __android_log_assert(condition, "PPSSPP", "%s:%d (%s): [%s] %s", file, line, func, condition, buf); - va_end(args); -} - -#endif - -bool ShowAssertDialog(const char *function, const char *file, int line, const char *expression, const char* format, ...) { +bool HandleAssert(const char *function, const char *file, int line, const char *expression, const char* format, ...) { // Read message and write it to the log - char text[2048]; + char text[LOG_BUF_SIZE]; const char *caption = "Critical"; va_list args; va_start(args, format); vsnprintf(text, sizeof(text), format, args); va_end(args); + // Secondary formatting. Wonder if this can be combined into the vsnprintf somehow. + char formatted[LOG_BUF_SIZE]; + snprintf(formatted, sizeof(formatted), "(%s:%s:%d) %s: [%s] %s", file, function, line, caption, expression, text); + // Normal logging (will also log to Android log) - ERROR_LOG(SYSTEM, "(%s:%d) %s: %s", file, line, caption, text); + ERROR_LOG(SYSTEM, "%s", formatted); // Also do a simple printf for good measure, in case logging of SYSTEM is disabled (should we disallow that?) - printf("(%s: %d) %s: %s\n", file, line, caption, text); + printf("%s\n", formatted); #if defined(USING_WIN_UI) int msgBoxStyle = MB_ICONINFORMATION | MB_YESNO; - std::wstring wtext = ConvertUTF8ToWString(text) + L"\n\nTry to continue?"; + std::wstring wtext = ConvertUTF8ToWString(formatted) + L"\n\nTry to continue?"; std::wstring wcaption = ConvertUTF8ToWString(caption); OutputDebugString(wtext.c_str()); - return IDYES == MessageBox(0, wtext.c_str(), wcaption.c_str(), msgBoxStyle); + if (IDYES != MessageBox(0, wtext.c_str(), wcaption.c_str(), msgBoxStyle)) { + return false; + } else { + return true; + } +#elif PPSSPP_PLATFORM(ANDROID) + __android_log_assert(expression, "PPSSPP", "%s", formatted); + // Doesn't matter what we return here. + return false; #else OutputDebugStringUTF8(text); return false; diff --git a/Common/Log.h b/Common/Log.h index 74dec3fc65..f8a56730a6 100644 --- a/Common/Log.h +++ b/Common/Log.h @@ -17,8 +17,6 @@ #pragma once -#include - #include "CommonFuncs.h" #define NOTICE_LEVEL 1 // VERY important information that is NOT errors. Like startup and debugprintfs from the game itself. @@ -111,30 +109,34 @@ bool GenericLogEnabled(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type); #define DEBUG_LOG(t,...) do { GENERIC_LOG(LogTypes::t, LogTypes::LDEBUG, __VA_ARGS__) } while (false) #define VERBOSE_LOG(t,...) do { GENERIC_LOG(LogTypes::t, LogTypes::LVERBOSE, __VA_ARGS__) } while (false) +// Currently only actually shows a dialog box on Windows. +bool HandleAssert(const char *function, const char *file, int line, const char *expression, const char* format, ...) +#ifdef __GNUC__ +__attribute__((format(printf, 5, 6))) +#endif +; + +#if defined(__ANDROID__) +// Tricky macro to get the basename, that also works if *built* on Win32. +// Doesn't mean this macro can be used on Win32 though. +#define __FILENAME__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : (__builtin_strrchr(__FILE__, '\\') ? __builtin_strrchr(__FILE__, '\\') + 1 : __FILE__)) +#else +#define __FILENAME__ __FILE__ +#endif + // If we're in "debug" assert mode #if MAX_LOGLEVEL >= DEBUG_LEVEL #define _dbg_assert_(_a_) \ if (!(_a_)) {\ - if (!ShowAssertDialog(__FUNCTION__, __FILE__, __LINE__, #_a_, "*** Assertion ***\n")) { Crash(); } \ + if (!HandleAssert(__FUNCTION__, __FILENAME__, __LINE__, #_a_, "*** Assertion ***\n")) Crash(); \ } -#if defined(__ANDROID__) - -#define _dbg_assert_msg_(_a_, ...)\ - if (!(_a_)) {\ - if (!ShowAssertDialog(__FUNCTION__, __FILE__, __LINE__, #_a_, __VA_ARGS__)) AndroidAssert(__FUNCTION__, __FILENAME__, __LINE__, #_a_, __VA_ARGS__); \ +#define _dbg_assert_msg_(_a_, ...) \ + if (!(_a_)) { \ + if (!HandleAssert(__FUNCTION__, __FILENAME__, __LINE__, #_a_, __VA_ARGS__)) Crash(); \ } -#else // !defined(__ANDROID__) - -#define _dbg_assert_msg_(_a_, ...)\ - if (!(_a_)) {\ - if (!ShowAssertDialog(__FUNCTION__, __FILE__, __LINE__, #_a_, __VA_ARGS__)) { Crash();} \ - } - -#endif // __ANDROID__ - #else // not debug #ifndef _dbg_assert_ @@ -144,46 +146,15 @@ bool GenericLogEnabled(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type); #endif // MAX_LOGLEVEL DEBUG -#if defined(__ANDROID__) - #define _assert_(_a_) \ if (!(_a_)) {\ - AndroidAssert(__FUNCTION__, __FILENAME__, __LINE__, #_a_, "Assertion failed!"); \ + if (!HandleAssert(__FUNCTION__, __FILENAME__, __LINE__, #_a_, "*** Assertion ***\n")) Crash(); \ } -#define _assert_msg_(_a_, ...) \ - if (!(_a_) && !ShowAssertDialog(__FUNCTION__,__FILENAME__, __LINE__, #_a_, __VA_ARGS__)) { \ - AndroidAssert(__FUNCTION__, __FILENAME__, __LINE__, #_a_, __VA_ARGS__); \ +#define _assert_msg_(_a_, ...) \ + if (!(_a_)) { \ + if (!HandleAssert(__FUNCTION__, __FILENAME__, __LINE__, #_a_, __VA_ARGS__)) Crash(); \ } -#else // __ANDROID__ - -#define _assert_(_a_) \ - if (!(_a_)) {\ - if (!ShowAssertDialog(__FUNCTION__, __FILE__, __LINE__, #_a_, "*** Assertion ***\n")) { Crash(); } \ - } - -#define _assert_msg_(_a_, ...) \ - if (!(_a_) && !ShowAssertDialog(__FUNCTION__, __FILE__, __LINE__, #_a_, __VA_ARGS__)) { \ - Crash(); \ - } - -#endif // __ANDROID__ - // Just INFO_LOGs on nonWindows. On Windows it outputs to the VS output console. void OutputDebugStringUTF8(const char *p); - -// Currently only actually shows a dialog box on Windows. -bool ShowAssertDialog(const char *function, const char *file, int line, const char *expression, const char* format, ...) -#ifdef __GNUC__ -__attribute__((format(printf, 5, 6))) -#endif -; - -#if defined(__ANDROID__) - -// Tricky macro to get the basename, that also works if *built* on Win32. -#define __FILENAME__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : (__builtin_strrchr(__FILE__, '\\') ? __builtin_strrchr(__FILE__, '\\') + 1 : __FILE__)) -void AndroidAssert(const char *func, const char *file, int line, const char *condition, const char *fmt, ...); - -#endif diff --git a/Common/LogManager.h b/Common/LogManager.h index 6aaa69b462..346561430f 100644 --- a/Common/LogManager.h +++ b/Common/LogManager.h @@ -24,6 +24,7 @@ #include #include "file/ini_file.h" +#include "Common/CommonFuncs.h" #include "Common/Log.h" #define MAX_MESSAGES 8000 diff --git a/GPU/D3D11/D3D11Util.cpp b/GPU/D3D11/D3D11Util.cpp index 7a7b0aac25..add974eb75 100644 --- a/GPU/D3D11/D3D11Util.cpp +++ b/GPU/D3D11/D3D11Util.cpp @@ -6,7 +6,6 @@ #include #include - #if PPSSPP_PLATFORM(UWP) #define ptr_D3DCompile D3DCompile #else @@ -15,8 +14,9 @@ #include "base/stringutil.h" -#include "D3D11Util.h" +#include "Common/CommonFuncs.h" #include "Common/Log.h" +#include "D3D11Util.h" static std::vector CompileShaderToBytecode(const char *code, size_t codeSize, const char *target, UINT flags) { ID3DBlob *compiledCode = nullptr; diff --git a/GPU/GeDisasm.cpp b/GPU/GeDisasm.cpp index fda2d77890..ef04d7c606 100644 --- a/GPU/GeDisasm.cpp +++ b/GPU/GeDisasm.cpp @@ -15,9 +15,10 @@ // Official git repository and contact information can be found at // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. -#include "Core/MemMap.h" +#include -#include "ge_constants.h" +#include "Core/MemMap.h" +#include "GPU/ge_constants.h" #include "GPU/GPU.h" #include "GPU/GPUState.h" diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index 91d0b7c86e..22b6ca4cad 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -762,6 +762,8 @@ void NativeInit(int argc, const char *argv[], const char *savegame_dir, const ch // Must be done restarting by now. restarting = false; + + _assert_msg_(false, "assert test"); } static UI::Style MakeStyle(uint32_t fg, uint32_t bg) { diff --git a/UI/TextureUtil.cpp b/UI/TextureUtil.cpp index 284a9b019d..eaf2b82faf 100644 --- a/UI/TextureUtil.cpp +++ b/UI/TextureUtil.cpp @@ -107,9 +107,7 @@ bool ManagedTexture::LoadFromFileData(const uint8_t *data, size_t dataSize, Imag return false; } - if (!image[0]) { - Crash(); - } + _assert_(image[0] != nullptr); if (num_levels < 0 || num_levels >= 16) { ERROR_LOG(IO, "Invalid num_levels: %d. Falling back to one. Image: %dx%d", num_levels, width[0], height[0]);