debuglog.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #pragma once
  2. #include <windows.h>
  3. #include <sstream>
  4. #include <cstdio>
  5. #define DEBUG_LOG_BUFFER_SIZE 1024
  6. #define DEBUG_LOG(msg) do { \
  7. std::wostringstream __oss; \
  8. __oss << L"[" << __func__ << L"]@" << __FILE__ << L":" << __LINE__ << L": " << msg << L"\n"; \
  9. OutputDebugString(__oss.str().c_str()); \
  10. } while(0)
  11. #define DEBUG_LOG_VAR(msg, val) do { \
  12. std::wostringstream __oss; \
  13. __oss << L"[" << __func__ << L"]@" << __FILE__ << L":" << __LINE__ << L": " << msg << L": " << val << L"\n"; \
  14. OutputDebugString(__oss.str().c_str()); \
  15. } while(0)
  16. #define DEBUG_LOG_FMT(format, ...) do { \
  17. wchar __logbuf[DEBUG_LOG_BUFFER_SIZE]; \
  18. std::swprintf(__logbuf, DEBUG_LOG_BUFFER_SIZE, format, ##__VA_ARGS__); \
  19. wchar __fullmsg[DEBUG_LOG_BUFFER_SIZE + 128]; \
  20. std::swprintf(__fullmsg, sizeof(__fullmsg), L"[%s]@%s:%d: %s\n", __func__, __FILE__, __LINE__, __logbuf); \
  21. OutputDebugString(__fullmsg); \
  22. } while(0)
  23. class DEBUG_LOG
  24. {
  25. public:
  26. static void debug_printf(const char * format, ...)
  27. {
  28. va_list args;
  29. va_start(args, format);
  30. vprintf(format, args); // 原生打印到控制台(或重定向的输出)
  31. char buffer[1024]; // 创建一个足够大的缓冲区来存储格式化后的字符串(可选)
  32. vsnprintf(buffer, sizeof(buffer), format, args); // 将格式化后的字符串存储到buffer中(可选)
  33. va_end(args); // 清理变量参数列表(optional)
  34. DEBUG_LOG(buffer);
  35. }
  36. };