DebugPrint.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. ** Command & Conquer Renegade(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: /Commando/Code/Launcher/Toolkit/Debug/DebugPrint.cpp $
  22. *
  23. * DESCRIPTION
  24. * Debug printing mechanism
  25. *
  26. * PROGRAMMER
  27. * Denzil E. Long, Jr.
  28. * $Author: Denzil_l $
  29. *
  30. * VERSION INFO
  31. * $Modtime: 9/26/00 2:28a $
  32. * $Revision: 1 $
  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. /******************************************************************************
  42. *
  43. * NAME
  44. * DebugPrint(String, ArgList...)
  45. *
  46. * DESCRIPTION
  47. * Ouput debug print messages to the debugger and log file.
  48. *
  49. * INPUTS
  50. * String - String to output.
  51. * ArgList - Argument list
  52. *
  53. * RESULT
  54. * NONE
  55. *
  56. ******************************************************************************/
  57. void __cdecl DebugPrint(const char* string, ...)
  58. {
  59. static char _buffer[1024];
  60. static char _filename[512] = "";
  61. if (string != NULL)
  62. {
  63. // Format string
  64. va_list va;
  65. va_start(va, string);
  66. vsprintf(&_buffer[0], string, va);
  67. va_end(va);
  68. // Open log file
  69. HANDLE file = INVALID_HANDLE_VALUE;
  70. if (strlen(_filename) == 0)
  71. {
  72. char path[_MAX_PATH];
  73. char drive[_MAX_DRIVE];
  74. char dir[_MAX_DIR];
  75. GetModuleFileName(GetModuleHandle(NULL), &path[0], sizeof(path));
  76. _splitpath(path, drive, dir, NULL, NULL);
  77. _makepath(_filename, drive, dir, "LauncherLog", "txt");
  78. OutputDebugString("Creating ");
  79. OutputDebugString(_filename);
  80. OutputDebugString("\n");
  81. file = CreateFile(_filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
  82. FILE_ATTRIBUTE_NORMAL, NULL);
  83. }
  84. else
  85. {
  86. file = CreateFile(_filename, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS,
  87. FILE_ATTRIBUTE_NORMAL, NULL);
  88. }
  89. // Send string to debugger
  90. OutputDebugString(_buffer);
  91. // Insert carriage return after newlines
  92. int i = 0;
  93. while (_buffer[i] != '\0')
  94. {
  95. if (_buffer[i] == '\n')
  96. {
  97. int end = strlen(_buffer);
  98. assert((end + 1) <= sizeof(_buffer));
  99. while (end >= i)
  100. {
  101. _buffer[end + 1] = _buffer[end];
  102. end--;
  103. }
  104. _buffer[i] = '\r';
  105. i++;
  106. }
  107. i++;
  108. }
  109. // Send string to log file
  110. assert(file != INVALID_HANDLE_VALUE);
  111. if (file != INVALID_HANDLE_VALUE)
  112. {
  113. SetFilePointer(file, 0, NULL, FILE_END);
  114. DWORD written;
  115. WriteFile(file, &_buffer[0], strlen(_buffer), &written, NULL);
  116. CloseHandle(file);
  117. }
  118. }
  119. }
  120. /******************************************************************************
  121. *
  122. * NAME
  123. * PrintWin32Error
  124. *
  125. * DESCRIPTION
  126. * Display Win32 error message (Error retrieved from GetLastError())
  127. *
  128. * INPUTS
  129. * Message string
  130. *
  131. * RESULT
  132. * NONE
  133. *
  134. ******************************************************************************/
  135. void __cdecl PrintWin32Error(const char* string, ...)
  136. {
  137. static char _buffer[1024];
  138. if (string != NULL)
  139. {
  140. // Format string
  141. va_list va;
  142. va_start(va, string);
  143. vsprintf(&_buffer[0], string, va);
  144. va_end(va);
  145. LPVOID lpMsgBuf;
  146. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
  147. FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(),
  148. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL);
  149. DebugPrint("***** Win32 Error: %s\n", _buffer);
  150. DebugPrint(" Reason: %s\n", (char*)lpMsgBuf);
  151. LocalFree(lpMsgBuf);
  152. }
  153. }
  154. #endif // _DEBUG