DebugPrint.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /******************************************************************************
  19. *
  20. * FILE
  21. * $Archive: $
  22. *
  23. * DESCRIPTION
  24. * Debug printing mechanism
  25. *
  26. * PROGRAMMER
  27. * Denzil E. Long, Jr.
  28. * $Author: $
  29. *
  30. * VERSION INFO
  31. * $Modtime: $
  32. * $Revision: $
  33. *
  34. ******************************************************************************/
  35. #ifdef _DEBUG
  36. #include "DebugPrint.h"
  37. #include <windows.h>
  38. #include <stdio.h>
  39. #include <stdarg.h>
  40. #include <assert.h>
  41. char debugLogName[_MAX_PATH];
  42. /******************************************************************************
  43. *
  44. * NAME
  45. * DebugPrint(String, ArgList...)
  46. *
  47. * DESCRIPTION
  48. * Ouput debug print messages to the debugger and log file.
  49. *
  50. * INPUTS
  51. * String - String to output.
  52. * ArgList - Argument list
  53. *
  54. * RESULT
  55. * NONE
  56. *
  57. ******************************************************************************/
  58. void __cdecl DebugPrint(const char* string, ...)
  59. {
  60. static char _buffer[1024];
  61. static char _filename[512] = "";
  62. if (string != NULL)
  63. {
  64. // Format string
  65. va_list va;
  66. va_start(va, string);
  67. vsprintf(&_buffer[0], string, va);
  68. va_end(va);
  69. // Open log file
  70. HANDLE file = INVALID_HANDLE_VALUE;
  71. if (strlen(_filename) == 0)
  72. {
  73. char path[_MAX_PATH];
  74. char drive[_MAX_DRIVE];
  75. char dir[_MAX_DIR];
  76. GetModuleFileName(GetModuleHandle(NULL), &path[0], sizeof(path));
  77. _splitpath(path, drive, dir, NULL, NULL);
  78. _makepath(_filename, drive, dir, debugLogName, "txt");
  79. OutputDebugString("Creating ");
  80. OutputDebugString(_filename);
  81. OutputDebugString("\n");
  82. file = CreateFile(_filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
  83. FILE_ATTRIBUTE_NORMAL, NULL);
  84. }
  85. else
  86. {
  87. file = CreateFile(_filename, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS,
  88. FILE_ATTRIBUTE_NORMAL, NULL);
  89. }
  90. // Send string to debugger
  91. OutputDebugString(_buffer);
  92. // Insert carriage return after newlines
  93. int i = 0;
  94. while (_buffer[i] != '\0')
  95. {
  96. if (_buffer[i] == '\n')
  97. {
  98. int end = strlen(_buffer);
  99. assert((end + 1) <= sizeof(_buffer));
  100. while (end >= i)
  101. {
  102. _buffer[end + 1] = _buffer[end];
  103. end--;
  104. }
  105. _buffer[i] = '\r';
  106. i++;
  107. }
  108. i++;
  109. }
  110. // Send string to log file
  111. assert(file != INVALID_HANDLE_VALUE);
  112. if (file != INVALID_HANDLE_VALUE)
  113. {
  114. SetFilePointer(file, 0, NULL, FILE_END);
  115. DWORD written;
  116. WriteFile(file, &_buffer[0], strlen(_buffer), &written, NULL);
  117. CloseHandle(file);
  118. }
  119. }
  120. }
  121. /******************************************************************************
  122. *
  123. * NAME
  124. * PrintWin32Error
  125. *
  126. * DESCRIPTION
  127. * Display Win32 error message (Error retrieved from GetLastError())
  128. *
  129. * INPUTS
  130. * Message string
  131. *
  132. * RESULT
  133. * NONE
  134. *
  135. ******************************************************************************/
  136. void __cdecl PrintWin32Error(const char* string, ...)
  137. {
  138. static char _buffer[1024];
  139. if (string != NULL)
  140. {
  141. // Format string
  142. va_list va;
  143. va_start(va, string);
  144. vsprintf(&_buffer[0], string, va);
  145. va_end(va);
  146. LPVOID lpMsgBuf;
  147. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
  148. FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(),
  149. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL);
  150. DebugPrint("***** Win32 Error: %s\n", _buffer);
  151. DebugPrint(" Reason: %s\n", (char*)lpMsgBuf);
  152. LocalFree(lpMsgBuf);
  153. }
  154. }
  155. #endif // _DEBUG