From f2e906224246c8ce4b3275e68a315d1d585ece86 Mon Sep 17 00:00:00 2001 From: otavepto <153766569+otavepto@users.noreply.github.com> Date: Fri, 7 Jun 2024 20:20:41 +0300 Subject: [PATCH] refactor the PRINT_DEBUG, only one definition, and make the function name macro public --- dll/dll/common_includes.h | 69 +++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 36 deletions(-) diff --git a/dll/dll/common_includes.h b/dll/dll/common_includes.h index 32cb5252..05ed2356 100644 --- a/dll/dll/common_includes.h +++ b/dll/dll/common_includes.h @@ -183,52 +183,49 @@ static inline void reset_LastError() #define AS_STR(x) #x #define EXPAND_AS_STR(x) AS_STR(x) +#if defined(__LINUX__) || defined(GNUC) || defined(__MINGW32__) || defined(__MINGW64__) // MinGw + #define EMU_FUNC_NAME __PRETTY_FUNCTION__ +#else + #define EMU_FUNC_NAME __FUNCTION__##"()" +#endif + // PRINT_DEBUG definition -// notice the extra call to WSASetLastError(0) in Windows def #ifndef EMU_RELEASE_BUILD // we need this for printf specifiers for intptr_t such as PRIdPTR #include + #if defined(__WINDOWS__) + #define PRINT_DEBUG_TID() (long long)GetCurrentThreadId() + #define PRINT_DEBUG_CLEANUP() WSASetLastError(0) + #elif defined(__LINUX__) + #include // syscall + + #define PRINT_DEBUG_TID() (long long)syscall(SYS_gettid) + #define PRINT_DEBUG_CLEANUP() (void)0 + #else + #warning "Unrecognized OS" + + #define PRINT_DEBUG_TID() (long long)0 + #define PRINT_DEBUG_CLEANUP() (void)0 + #endif + //#define PRINT_DEBUG(...) fprintf(stdout, __VA_ARGS__) extern const std::string dbg_log_file; extern const std::chrono::time_point startup_counter; - #if defined(__LINUX__) || defined(GNUC) || defined(__MINGW32__) || defined(__MINGW64__) // MinGw - #define EMU_FUN_NAME __PRETTY_FUNCTION__ - #else - #define EMU_FUN_NAME __FUNCTION__##"()" - #endif + #define PRINT_DEBUG(a, ...) do { \ + auto __prnt_dbg_ctr = std::chrono::high_resolution_clock::now(); \ + auto __prnt_dbg_duration = __prnt_dbg_ctr - startup_counter; \ + auto __prnt_dbg_micro = std::chrono::duration_cast>(__prnt_dbg_duration); \ + auto __prnt_dbg_ms = std::chrono::duration_cast>(__prnt_dbg_duration); \ + auto __prnt_dbg_f = fopen(dbg_log_file.c_str(), "a"); \ + if (!__prnt_dbg_f) break; \ + fprintf(__prnt_dbg_f, "[%llu ms, %lld us] [tid %lld] %s " a "\n", \ + __prnt_dbg_ms.count(), __prnt_dbg_micro.count(), PRINT_DEBUG_TID(), EMU_FUNC_NAME, ##__VA_ARGS__); \ + fclose(__prnt_dbg_f); \ + PRINT_DEBUG_CLEANUP(); \ + } while (0) - #if defined(__WINDOWS__) - #define PRINT_DEBUG(a, ...) do { \ - auto __prnt_dbg_ctr = std::chrono::high_resolution_clock::now(); \ - auto __prnt_dbg_duration = __prnt_dbg_ctr - startup_counter; \ - auto __prnt_dbg_micro = std::chrono::duration_cast>(__prnt_dbg_duration); \ - auto __prnt_dbg_ms = std::chrono::duration_cast>(__prnt_dbg_duration); \ - auto __prnt_dbg_f = fopen(dbg_log_file.c_str(), "a"); \ - if (!__prnt_dbg_f) break; \ - fprintf(__prnt_dbg_f, "[%llu ms, %llu us] [tid %lu] %s " a "\n", \ - __prnt_dbg_ms.count(), __prnt_dbg_micro.count(), GetCurrentThreadId(), EMU_FUN_NAME, ##__VA_ARGS__); \ - fclose(__prnt_dbg_f); \ - WSASetLastError(0); \ - } while (0) - #elif defined(__LINUX__) - #include - #define PRINT_DEBUG(a, ...) do { \ - auto __prnt_dbg_ctr = std::chrono::high_resolution_clock::now(); \ - auto __prnt_dbg_duration = __prnt_dbg_ctr - startup_counter; \ - auto __prnt_dbg_micro = std::chrono::duration_cast>(__prnt_dbg_duration); \ - auto __prnt_dbg_ms = std::chrono::duration_cast>(__prnt_dbg_duration); \ - auto __prnt_dbg_f = fopen(dbg_log_file.c_str(), "a"); \ - if (!__prnt_dbg_f) break; \ - fprintf(__prnt_dbg_f, "[%llu ms, %llu us] [tid %ld] %s " a "\n", \ - __prnt_dbg_ms.count(), __prnt_dbg_micro.count(), syscall(SYS_gettid), EMU_FUN_NAME, ##__VA_ARGS__); \ - fclose(__prnt_dbg_f); \ - } while (0) - #else - #warning "Unrecognized OS, cannot set debug print" - #define PRINT_DEBUG(...) - #endif #else // EMU_RELEASE_BUILD #define PRINT_DEBUG(...) #endif // EMU_RELEASE_BUILD