wwdebug.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. ** Command & Conquer Generals(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. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WWDebug *
  23. * *
  24. * $Archive:: /VSS_Sync/wwdebug/wwdebug.cpp $*
  25. * *
  26. * $Author:: Vss_sync $*
  27. * *
  28. * $Modtime:: 10/19/00 2:12p $*
  29. * *
  30. * $Revision:: 13 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * WWDebug_Install_Message_Handler -- install function for handling the debug messages *
  35. * WWDebug_Install_Assert_Handler -- Install a function for handling the assert messages *
  36. * WWDebug_Install_Trigger_Handler -- install a trigger handler function *
  37. * WWDebug_Printf -- Internal function for passing messages to installed handler *
  38. * WWDebug_Assert_Fail -- Internal function for passing assert messages to installed handler *
  39. * WWDebug_Assert_Fail_Print -- Internal function, passes assert message to handler *
  40. * WWDebug_Check_Trigger -- calls the user-installed debug trigger handler *
  41. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  42. #include "wwdebug.h"
  43. #include <windows.h>
  44. //#include "win.h" can use this if allowed to see wwlib
  45. #include <stdlib.h>
  46. #include <stdarg.h>
  47. #include <stdio.h>
  48. #include <assert.h>
  49. #include <string.h>
  50. static PrintFunc _CurMessageHandler = NULL;
  51. static AssertPrintFunc _CurAssertHandler = NULL;
  52. static TriggerFunc _CurTriggerHandler = NULL;
  53. static ProfileFunc _CurProfileStartHandler = NULL;
  54. static ProfileFunc _CurProfileStopHandler = NULL;
  55. // Convert the latest system error into a string and return a pointer to
  56. // a static buffer containing the error string.
  57. void Convert_System_Error_To_String(int id, char* buffer, int buf_len)
  58. {
  59. #ifndef _UNIX
  60. FormatMessage(
  61. FORMAT_MESSAGE_FROM_SYSTEM,
  62. NULL,
  63. id,
  64. 0,
  65. buffer,
  66. buf_len,
  67. NULL);
  68. #endif
  69. }
  70. int Get_Last_System_Error()
  71. {
  72. return GetLastError();
  73. }
  74. /***********************************************************************************************
  75. * WWDebug_Install_Message_Handler -- install function for handling the debug messages *
  76. * *
  77. * INPUT: *
  78. * *
  79. * OUTPUT: *
  80. * *
  81. * WARNINGS: *
  82. * *
  83. * HISTORY: *
  84. * 2/19/98 GTH : Created. *
  85. *=============================================================================================*/
  86. PrintFunc WWDebug_Install_Message_Handler(PrintFunc func)
  87. {
  88. PrintFunc tmp = _CurMessageHandler;
  89. _CurMessageHandler = func;
  90. return tmp;
  91. }
  92. /***********************************************************************************************
  93. * WWDebug_Install_Assert_Handler -- Install a function for handling the assert messages *
  94. * *
  95. * INPUT: *
  96. * *
  97. * OUTPUT: *
  98. * *
  99. * WARNINGS: *
  100. * *
  101. * HISTORY: *
  102. * 2/19/98 GTH : Created. *
  103. *=============================================================================================*/
  104. AssertPrintFunc WWDebug_Install_Assert_Handler(AssertPrintFunc func)
  105. {
  106. AssertPrintFunc tmp = _CurAssertHandler;
  107. _CurAssertHandler = func;
  108. return tmp;
  109. }
  110. /***********************************************************************************************
  111. * WWDebug_Install_Trigger_Handler -- install a trigger handler function *
  112. * *
  113. * INPUT: *
  114. * *
  115. * OUTPUT: *
  116. * *
  117. * WARNINGS: *
  118. * *
  119. * HISTORY: *
  120. * 2/24/98 GTH : Created. *
  121. *=============================================================================================*/
  122. TriggerFunc WWDebug_Install_Trigger_Handler(TriggerFunc func)
  123. {
  124. TriggerFunc tmp = _CurTriggerHandler;
  125. _CurTriggerHandler = func;
  126. return tmp;
  127. }
  128. /***********************************************************************************************
  129. * WWDebug_Install_Profile_Start_Handler -- install a profile handler function *
  130. * *
  131. * INPUT: *
  132. * *
  133. * OUTPUT: *
  134. * *
  135. * WARNINGS: *
  136. * *
  137. * HISTORY: *
  138. * 2/24/98 GTH : Created. *
  139. *=============================================================================================*/
  140. ProfileFunc WWDebug_Install_Profile_Start_Handler(ProfileFunc func)
  141. {
  142. ProfileFunc tmp = _CurProfileStartHandler;
  143. _CurProfileStartHandler = func;
  144. return tmp;
  145. }
  146. /***********************************************************************************************
  147. * WWDebug_Install_Profile_Stop_Handler -- install a profile handler function *
  148. * *
  149. * INPUT: *
  150. * *
  151. * OUTPUT: *
  152. * *
  153. * WARNINGS: *
  154. * *
  155. * HISTORY: *
  156. * 2/24/98 GTH : Created. *
  157. *=============================================================================================*/
  158. ProfileFunc WWDebug_Install_Profile_Stop_Handler(ProfileFunc func)
  159. {
  160. ProfileFunc tmp = _CurProfileStopHandler;
  161. _CurProfileStopHandler = func;
  162. return tmp;
  163. }
  164. /***********************************************************************************************
  165. * WWDebug_Printf -- Internal function for passing messages to installed handler *
  166. * *
  167. * INPUT: *
  168. * *
  169. * OUTPUT: *
  170. * *
  171. * WARNINGS: *
  172. * *
  173. * HISTORY: *
  174. * 2/19/98 GTH : Created. *
  175. *=============================================================================================*/
  176. #ifdef WWDEBUG
  177. void WWDebug_Printf(const char * format,...)
  178. {
  179. if (_CurMessageHandler != NULL) {
  180. va_list va;
  181. char buffer[1024];
  182. va_start(va, format);
  183. vsprintf(buffer, format, va);
  184. WWASSERT((strlen(buffer) < sizeof(buffer)));
  185. _CurMessageHandler(WWDEBUG_TYPE_INFORMATION, buffer);
  186. va_end(va);
  187. }
  188. }
  189. #endif
  190. /***********************************************************************************************
  191. * WWDebug_Printf_Warning -- Internal function for passing messages to installed handler *
  192. * *
  193. * INPUT: *
  194. * *
  195. * OUTPUT: *
  196. * *
  197. * WARNINGS: *
  198. * *
  199. * HISTORY: *
  200. * 2/19/98 GTH : Created. *
  201. *=============================================================================================*/
  202. #ifdef WWDEBUG
  203. void WWDebug_Printf_Warning(const char * format,...)
  204. {
  205. if (_CurMessageHandler != NULL) {
  206. va_list va;
  207. char buffer[1024];
  208. va_start(va, format);
  209. vsprintf(buffer, format, va);
  210. WWASSERT((strlen(buffer) < sizeof(buffer)));
  211. _CurMessageHandler(WWDEBUG_TYPE_WARNING, buffer);
  212. va_end(va);
  213. }
  214. }
  215. #endif
  216. /***********************************************************************************************
  217. * WWDebug_Printf_Error -- Internal function for passing messages to installed handler *
  218. * *
  219. * INPUT: *
  220. * *
  221. * OUTPUT: *
  222. * *
  223. * WARNINGS: *
  224. * *
  225. * HISTORY: *
  226. * 2/19/98 GTH : Created. *
  227. *=============================================================================================*/
  228. #ifdef WWDEBUG
  229. void WWDebug_Printf_Error(const char * format,...)
  230. {
  231. if (_CurMessageHandler != NULL) {
  232. va_list va;
  233. char buffer[1024];
  234. va_start(va, format);
  235. vsprintf(buffer, format, va);
  236. WWASSERT((strlen(buffer) < sizeof(buffer)));
  237. _CurMessageHandler(WWDEBUG_TYPE_ERROR, buffer);
  238. va_end(va);
  239. }
  240. }
  241. #endif
  242. /***********************************************************************************************
  243. * WWDebug_Assert_Fail -- Internal function for passing assert messages to installed handler *
  244. * *
  245. * INPUT: *
  246. * *
  247. * OUTPUT: *
  248. * *
  249. * WARNINGS: *
  250. * *
  251. * HISTORY: *
  252. * 2/19/98 GTH : Created. *
  253. *=============================================================================================*/
  254. #ifdef WWDEBUG
  255. void WWDebug_Assert_Fail(const char * expr,const char * file, int line)
  256. {
  257. if (_CurAssertHandler != NULL) {
  258. char buffer[1024];
  259. sprintf(buffer,"%s (%d) Assert: %s\n",file,line,expr);
  260. _CurAssertHandler(buffer);
  261. } else {
  262. assert(0);
  263. }
  264. }
  265. #endif
  266. /***********************************************************************************************
  267. * WWDebug_Assert_Fail_Print -- Internal function, passes assert message to handler *
  268. * *
  269. * INPUT: *
  270. * *
  271. * OUTPUT: *
  272. * *
  273. * WARNINGS: *
  274. * *
  275. * HISTORY: *
  276. * 2/19/98 GTH : Created. *
  277. *=============================================================================================*/
  278. #ifdef WWDEBUG
  279. void WWDebug_Assert_Fail_Print(const char * expr,const char * file, int line,const char * string)
  280. {
  281. if (_CurAssertHandler != NULL) {
  282. char buffer[1024];
  283. sprintf(buffer,"%s (%d) Assert: %s %s\n",file,line,expr, string);
  284. _CurAssertHandler(buffer);
  285. } else {
  286. assert(0);
  287. }
  288. }
  289. #endif
  290. /***********************************************************************************************
  291. * WWDebug_Check_Trigger -- calls the user-installed debug trigger handler *
  292. * *
  293. * INPUT: *
  294. * *
  295. * OUTPUT: *
  296. * *
  297. * WARNINGS: *
  298. * *
  299. * HISTORY: *
  300. * 2/24/98 GTH : Created. *
  301. *=============================================================================================*/
  302. bool WWDebug_Check_Trigger(int trigger_num)
  303. {
  304. if (_CurTriggerHandler != NULL) {
  305. return _CurTriggerHandler(trigger_num);
  306. } else {
  307. return false;
  308. }
  309. }
  310. /***********************************************************************************************
  311. * WWDebug_Profile_Start -- calls the user-installed profile start handler *
  312. * *
  313. * INPUT: *
  314. * *
  315. * OUTPUT: *
  316. * *
  317. * WARNINGS: *
  318. * *
  319. * HISTORY: *
  320. * 2/24/98 GTH : Created. *
  321. *=============================================================================================*/
  322. void WWDebug_Profile_Start( const char * title)
  323. {
  324. if (_CurProfileStartHandler != NULL) {
  325. _CurProfileStartHandler( title );
  326. }
  327. }
  328. /***********************************************************************************************
  329. * WWDebug_Profile_Stop -- calls the user-installed profile start handler *
  330. * *
  331. * INPUT: *
  332. * *
  333. * OUTPUT: *
  334. * *
  335. * WARNINGS: *
  336. * *
  337. * HISTORY: *
  338. * 2/24/98 GTH : Created. *
  339. *=============================================================================================*/
  340. void WWDebug_Profile_Stop( const char * title)
  341. {
  342. if (_CurProfileStopHandler != NULL) {
  343. _CurProfileStopHandler( title );
  344. }
  345. }
  346. #ifdef WWDEBUG
  347. /***********************************************************************************************
  348. * WWDebug_DBWin32_Message_Handler -- *
  349. * *
  350. * INPUT: *
  351. * *
  352. * OUTPUT: *
  353. * *
  354. * WARNINGS: *
  355. * *
  356. * HISTORY: *
  357. * 10/30/98 BMG : Created. *
  358. *=============================================================================================*/
  359. void WWDebug_DBWin32_Message_Handler( const char * str )
  360. {
  361. HANDLE heventDBWIN; /* DBWIN32 synchronization object */
  362. HANDLE heventData; /* data passing synch object */
  363. HANDLE hSharedFile; /* memory mapped file shared data */
  364. LPSTR lpszSharedMem;
  365. /* make sure DBWIN is open and waiting */
  366. heventDBWIN = OpenEvent(EVENT_MODIFY_STATE, FALSE, "DBWIN_BUFFER_READY");
  367. if ( !heventDBWIN )
  368. {
  369. //MessageBox(NULL, "DBWIN_BUFFER_READY nonexistent", NULL, MB_OK);
  370. return;
  371. }
  372. /* get a handle to the data synch object */
  373. heventData = OpenEvent(EVENT_MODIFY_STATE, FALSE, "DBWIN_DATA_READY");
  374. if ( !heventData )
  375. {
  376. // MessageBox(NULL, "DBWIN_DATA_READY nonexistent", NULL, MB_OK);
  377. CloseHandle(heventDBWIN);
  378. return;
  379. }
  380. hSharedFile = CreateFileMapping((HANDLE)-1, NULL, PAGE_READWRITE, 0, 4096, "DBWIN_BUFFER");
  381. if (!hSharedFile)
  382. {
  383. //MessageBox(NULL, "DebugTrace: Unable to create file mapping object DBWIN_BUFFER", "Error", MB_OK);
  384. CloseHandle(heventDBWIN);
  385. CloseHandle(heventData);
  386. return;
  387. }
  388. lpszSharedMem = (LPSTR)MapViewOfFile(hSharedFile, FILE_MAP_WRITE, 0, 0, 512);
  389. if (!lpszSharedMem)
  390. {
  391. //MessageBox(NULL, "DebugTrace: Unable to map shared memory", "Error", MB_OK);
  392. CloseHandle(heventDBWIN);
  393. CloseHandle(heventData);
  394. return;
  395. }
  396. /* wait for buffer event */
  397. WaitForSingleObject(heventDBWIN, INFINITE);
  398. /* write it to the shared memory */
  399. *((LPDWORD)lpszSharedMem) = 0;
  400. wsprintf(lpszSharedMem + sizeof(DWORD), "%s", str);
  401. /* signal data ready event */
  402. SetEvent(heventData);
  403. /* clean up handles */
  404. CloseHandle(hSharedFile);
  405. CloseHandle(heventData);
  406. CloseHandle(heventDBWIN);
  407. return;
  408. }
  409. #endif // WWDEBUG