refactor the PRINT_DEBUG, only one definition, and make the function name macro public

This commit is contained in:
otavepto 2024-06-07 20:20:41 +03:00
parent 2cd5f15376
commit f2e9062242

View File

@ -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 <inttypes.h>
#if defined(__WINDOWS__)
#define PRINT_DEBUG_TID() (long long)GetCurrentThreadId()
#define PRINT_DEBUG_CLEANUP() WSASetLastError(0)
#elif defined(__LINUX__)
#include <sys/syscall.h> // 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<std::chrono::high_resolution_clock> 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<std::chrono::duration<unsigned long long, std::micro>>(__prnt_dbg_duration); \
auto __prnt_dbg_ms = std::chrono::duration_cast<std::chrono::duration<unsigned long long, std::milli>>(__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<std::chrono::duration<unsigned long long, std::micro>>(__prnt_dbg_duration); \
auto __prnt_dbg_ms = std::chrono::duration_cast<std::chrono::duration<unsigned long long, std::milli>>(__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 <sys/syscall.h>
#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<std::chrono::duration<unsigned long long, std::micro>>(__prnt_dbg_duration); \
auto __prnt_dbg_ms = std::chrono::duration_cast<std::chrono::duration<unsigned long long, std::milli>>(__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