1
0

trace.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "./trace.h"
  2. #include <strsafe.h>
  3. #ifdef _DEBUG
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif /*__cplusplus*/
  7. void DebugPrintfW(LPCWSTR format, ...)
  8. {
  9. va_list argList;
  10. wchar_t *pstr(NULL);
  11. void *buffer;
  12. size_t allocated(0), remaining(0);
  13. int attempt(0);
  14. va_start(argList, format);
  15. do
  16. {
  17. attempt++;
  18. if(attempt)
  19. {
  20. allocated += (512 * attempt);
  21. buffer = realloc(pstr, allocated * sizeof(wchar_t));
  22. if (NULL == buffer)
  23. break;
  24. pstr = (wchar_t*)buffer;
  25. }
  26. }
  27. while (STRSAFE_E_INSUFFICIENT_BUFFER == StringCchVPrintfExW(pstr, allocated, NULL, &remaining,
  28. STRSAFE_NULL_ON_FAILURE, format, argList));
  29. OutputDebugStringW(pstr);
  30. if (NULL != pstr)
  31. free(pstr);
  32. va_end(argList);
  33. }
  34. void DebugPrintfA(LPCSTR format, ...)
  35. {
  36. va_list argList;
  37. char *pstr(NULL);
  38. void *buffer;
  39. size_t allocated(0), remaining(0);
  40. int attempt(0);
  41. va_start(argList, format);
  42. do
  43. {
  44. attempt++;
  45. if(attempt)
  46. {
  47. allocated += (512 * attempt);
  48. buffer = realloc(pstr, allocated * sizeof(char));
  49. if (NULL == buffer)
  50. break;
  51. pstr = (char*)buffer;
  52. }
  53. }
  54. while (STRSAFE_E_INSUFFICIENT_BUFFER == StringCchVPrintfExA(pstr, allocated, NULL, &remaining,
  55. STRSAFE_NULL_ON_FAILURE, format, argList));
  56. OutputDebugStringA(pstr);
  57. if (NULL != pstr)
  58. free(pstr);
  59. va_end(argList);
  60. }
  61. #ifdef __cplusplus
  62. }
  63. #endif /*__cplusplus*/
  64. #endif /*_DEBUG*/