Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
wwdebug.cpp
Go to the documentation of this file.
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 *** 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 ***
21 ***********************************************************************************************
22 * *
23 * Project Name : WWDebug *
24 * *
25 * $Archive:: /Commando/Code/wwdebug/wwdebug.cpp $*
26 * *
27 * $Author:: Greg_h $*
28 * *
29 * $Modtime:: 1/13/02 1:46p $*
30 * *
31 * $Revision:: 16 $*
32 * *
33 *---------------------------------------------------------------------------------------------*
34 * Functions: *
35 * WWDebug_Install_Message_Handler -- install function for handling the debug messages *
36 * WWDebug_Install_Assert_Handler -- Install a function for handling the assert messages *
37 * WWDebug_Install_Trigger_Handler -- install a trigger handler function *
38 * WWDebug_Printf -- Internal function for passing messages to installed handler *
39 * WWDebug_Assert_Fail -- Internal function for passing assert messages to installed handler *
40 * WWDebug_Assert_Fail_Print -- Internal function, passes assert message to handler *
41 * WWDebug_Check_Trigger -- calls the user-installed debug trigger handler *
42 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
43
44
45#include "wwdebug.h"
46#include <windows.h>
47//#include "win.h" can use this if allowed to see wwlib
48#include <stdlib.h>
49#include <stdarg.h>
50#include <stdio.h>
51#include <assert.h>
52#include <string.h>
53#include <signal.h>
54#include "except.h"
55
56
57static PrintFunc _CurMessageHandler = NULL;
58static AssertPrintFunc _CurAssertHandler = NULL;
59static TriggerFunc _CurTriggerHandler = NULL;
60static ProfileFunc _CurProfileStartHandler = NULL;
61static ProfileFunc _CurProfileStopHandler = NULL;
62
63// Convert the latest system error into a string and return a pointer to
64// a static buffer containing the error string.
65
66void Convert_System_Error_To_String(int id, char* buffer, int buf_len)
67{
68#ifndef _UNIX
69 FormatMessage(
70 FORMAT_MESSAGE_FROM_SYSTEM,
71 NULL,
72 id,
73 0,
74 buffer,
75 buf_len,
76 NULL);
77#endif
78}
79
81{
82 return GetLastError();
83}
84
85/***********************************************************************************************
86 * WWDebug_Install_Message_Handler -- install function for handling the debug messages *
87 * *
88 * INPUT: *
89 * *
90 * OUTPUT: *
91 * *
92 * WARNINGS: *
93 * *
94 * HISTORY: *
95 * 2/19/98 GTH : Created. *
96 *=============================================================================================*/
98{
99 PrintFunc tmp = _CurMessageHandler;
100 _CurMessageHandler = func;
101 return tmp;
102}
103
104
105/***********************************************************************************************
106 * WWDebug_Install_Assert_Handler -- Install a function for handling the assert messages *
107 * *
108 * INPUT: *
109 * *
110 * OUTPUT: *
111 * *
112 * WARNINGS: *
113 * *
114 * HISTORY: *
115 * 2/19/98 GTH : Created. *
116 *=============================================================================================*/
118{
119 AssertPrintFunc tmp = _CurAssertHandler;
120 _CurAssertHandler = func;
121 return tmp;
122}
123
124
125/***********************************************************************************************
126 * WWDebug_Install_Trigger_Handler -- install a trigger handler function *
127 * *
128 * INPUT: *
129 * *
130 * OUTPUT: *
131 * *
132 * WARNINGS: *
133 * *
134 * HISTORY: *
135 * 2/24/98 GTH : Created. *
136 *=============================================================================================*/
138{
139 TriggerFunc tmp = _CurTriggerHandler;
140 _CurTriggerHandler = func;
141 return tmp;
142}
143
144
145/***********************************************************************************************
146 * WWDebug_Install_Profile_Start_Handler -- install a profile handler function *
147 * *
148 * INPUT: *
149 * *
150 * OUTPUT: *
151 * *
152 * WARNINGS: *
153 * *
154 * HISTORY: *
155 * 2/24/98 GTH : Created. *
156 *=============================================================================================*/
158{
159 ProfileFunc tmp = _CurProfileStartHandler;
160 _CurProfileStartHandler = func;
161 return tmp;
162}
163
164
165/***********************************************************************************************
166 * WWDebug_Install_Profile_Stop_Handler -- install a profile handler function *
167 * *
168 * INPUT: *
169 * *
170 * OUTPUT: *
171 * *
172 * WARNINGS: *
173 * *
174 * HISTORY: *
175 * 2/24/98 GTH : Created. *
176 *=============================================================================================*/
178{
179 ProfileFunc tmp = _CurProfileStopHandler;
180 _CurProfileStopHandler = func;
181 return tmp;
182}
183
184
185/***********************************************************************************************
186 * WWDebug_Printf -- Internal function for passing messages to installed handler *
187 * *
188 * INPUT: *
189 * *
190 * OUTPUT: *
191 * *
192 * WARNINGS: *
193 * *
194 * HISTORY: *
195 * 2/19/98 GTH : Created. *
196 *=============================================================================================*/
197
198void WWDebug_Printf(const char * format,...)
199{
200 if (_CurMessageHandler != NULL) {
201
202 va_list va;
203 char buffer[4096];
204
205 va_start(va, format);
206 vsprintf(buffer, format, va);
207 WWASSERT((strlen(buffer) < sizeof(buffer)));
208
209 _CurMessageHandler(WWDEBUG_TYPE_INFORMATION, buffer);
210 va_end(va);
211
212 }
213}
214
215/***********************************************************************************************
216 * WWDebug_Printf_Warning -- Internal function for passing messages to installed handler *
217 * *
218 * INPUT: *
219 * *
220 * OUTPUT: *
221 * *
222 * WARNINGS: *
223 * *
224 * HISTORY: *
225 * 2/19/98 GTH : Created. *
226 *=============================================================================================*/
227
228void WWDebug_Printf_Warning(const char * format,...)
229{
230 if (_CurMessageHandler != NULL) {
231
232 va_list va;
233 char buffer[4096];
234
235 va_start(va, format);
236 vsprintf(buffer, format, va);
237 WWASSERT((strlen(buffer) < sizeof(buffer)));
238
239 _CurMessageHandler(WWDEBUG_TYPE_WARNING, buffer);
240 va_end(va);
241
242 }
243}
244
245/***********************************************************************************************
246 * WWDebug_Printf_Error -- Internal function for passing messages to installed handler *
247 * *
248 * INPUT: *
249 * *
250 * OUTPUT: *
251 * *
252 * WARNINGS: *
253 * *
254 * HISTORY: *
255 * 2/19/98 GTH : Created. *
256 *=============================================================================================*/
257
258void WWDebug_Printf_Error(const char * format,...)
259{
260 if (_CurMessageHandler != NULL) {
261
262 va_list va;
263 char buffer[4096];
264
265 va_start(va, format);
266 vsprintf(buffer, format, va);
267 WWASSERT((strlen(buffer) < sizeof(buffer)));
268
269 _CurMessageHandler(WWDEBUG_TYPE_ERROR, buffer);
270 va_end(va);
271
272 }
273}
274
275/***********************************************************************************************
276 * WWDebug_Assert_Fail -- Internal function for passing assert messages to installed handler *
277 * *
278 * INPUT: *
279 * *
280 * OUTPUT: *
281 * *
282 * WARNINGS: *
283 * *
284 * HISTORY: *
285 * 2/19/98 GTH : Created. *
286 *=============================================================================================*/
287#ifdef WWDEBUG
288void WWDebug_Assert_Fail(const char * expr,const char * file, int line)
289{
290 if (_CurAssertHandler != NULL) {
291
292 char buffer[4096];
293 sprintf(buffer,"%s (%d) Assert: %s\n",file,line,expr);
294 _CurAssertHandler(buffer);
295
296 } else {
297
298 /*
299 // If the exception handler is try to quit the game then don't show an assert.
300 */
301 if (Is_Trying_To_Exit()) {
302 ExitProcess(0);
303 }
304
305 char assertbuf[4096];
306 sprintf(assertbuf, "Assert failed\n\n. File %s Line %d", file, line);
307
308 int code = MessageBoxA(NULL, assertbuf, "WWDebug_Assert_Fail", MB_ABORTRETRYIGNORE|MB_ICONHAND|MB_SETFOREGROUND|MB_TASKMODAL);
309
310 if (code == IDABORT) {
311 raise(SIGABRT);
312 _exit(3);
313 }
314
315 if (code == IDRETRY) {
316 _asm int 3;
317 return;
318 }
319 }
320}
321#endif
322
323
324
325
326/***********************************************************************************************
327 * _assert -- Catch all asserts by overriding lib function *
328 * *
329 * *
330 * *
331 * INPUT: Assert stuff *
332 * *
333 * OUTPUT: Nothing *
334 * *
335 * WARNINGS: None *
336 * *
337 * HISTORY: *
338 * 12/11/2001 3:56PM ST : Created *
339 *=============================================================================================*/
340#if 0 //(gth) this is giving me link errors for some reason...
341
342#ifndef W3D_MAX4
343#ifdef WWDEBUG
344void __cdecl _assert(void *expr, void *filename, unsigned lineno)
345{
346 WWDebug_Assert_Fail((const char*)expr, (const char*)filename, lineno);
347}
348#endif //WWDEBUG
349#endif
350
351#endif
352
353
354
355/***********************************************************************************************
356 * WWDebug_Assert_Fail_Print -- Internal function, passes assert message to handler *
357 * *
358 * INPUT: *
359 * *
360 * OUTPUT: *
361 * *
362 * WARNINGS: *
363 * *
364 * HISTORY: *
365 * 2/19/98 GTH : Created. *
366 *=============================================================================================*/
367#ifdef WWDEBUG
368void WWDebug_Assert_Fail_Print(const char * expr,const char * file, int line,const char * string)
369{
370 if (_CurAssertHandler != NULL) {
371
372 char buffer[4096];
373 sprintf(buffer,"%s (%d) Assert: %s %s\n",file,line,expr, string);
374 _CurAssertHandler(buffer);
375
376 } else {
377
378 assert(0);
379
380 }
381}
382#endif
383
384
385/***********************************************************************************************
386 * WWDebug_Check_Trigger -- calls the user-installed debug trigger handler *
387 * *
388 * INPUT: *
389 * *
390 * OUTPUT: *
391 * *
392 * WARNINGS: *
393 * *
394 * HISTORY: *
395 * 2/24/98 GTH : Created. *
396 *=============================================================================================*/
397bool WWDebug_Check_Trigger(int trigger_num)
398{
399 if (_CurTriggerHandler != NULL) {
400 return _CurTriggerHandler(trigger_num);
401 } else {
402 return false;
403 }
404}
405
406
407/***********************************************************************************************
408 * WWDebug_Profile_Start -- calls the user-installed profile start handler *
409 * *
410 * INPUT: *
411 * *
412 * OUTPUT: *
413 * *
414 * WARNINGS: *
415 * *
416 * HISTORY: *
417 * 2/24/98 GTH : Created. *
418 *=============================================================================================*/
419void WWDebug_Profile_Start( const char * title)
420{
421 if (_CurProfileStartHandler != NULL) {
422 _CurProfileStartHandler( title );
423 }
424}
425
426
427/***********************************************************************************************
428 * WWDebug_Profile_Stop -- calls the user-installed profile start handler *
429 * *
430 * INPUT: *
431 * *
432 * OUTPUT: *
433 * *
434 * WARNINGS: *
435 * *
436 * HISTORY: *
437 * 2/24/98 GTH : Created. *
438 *=============================================================================================*/
439void WWDebug_Profile_Stop( const char * title)
440{
441 if (_CurProfileStopHandler != NULL) {
442 _CurProfileStopHandler( title );
443 }
444}
445
446
447
448#ifdef WWDEBUG
449/***********************************************************************************************
450 * WWDebug_DBWin32_Message_Handler -- *
451 * *
452 * INPUT: *
453 * *
454 * OUTPUT: *
455 * *
456 * WARNINGS: *
457 * *
458 * HISTORY: *
459 * 10/30/98 BMG : Created. *
460 *=============================================================================================*/
461void WWDebug_DBWin32_Message_Handler( const char * str )
462{
463
464 HANDLE heventDBWIN; /* DBWIN32 synchronization object */
465 HANDLE heventData; /* data passing synch object */
466 HANDLE hSharedFile; /* memory mapped file shared data */
467 LPSTR lpszSharedMem;
468
469 /* make sure DBWIN is open and waiting */
470 heventDBWIN = OpenEvent(EVENT_MODIFY_STATE, FALSE, "DBWIN_BUFFER_READY");
471 if ( !heventDBWIN )
472 {
473 //MessageBox(NULL, "DBWIN_BUFFER_READY nonexistent", NULL, MB_OK);
474 return;
475 }
476
477 /* get a handle to the data synch object */
478 heventData = OpenEvent(EVENT_MODIFY_STATE, FALSE, "DBWIN_DATA_READY");
479 if ( !heventData )
480 {
481 // MessageBox(NULL, "DBWIN_DATA_READY nonexistent", NULL, MB_OK);
482 CloseHandle(heventDBWIN);
483 return;
484 }
485
486 hSharedFile = CreateFileMapping((HANDLE)-1, NULL, PAGE_READWRITE, 0, 4096, "DBWIN_BUFFER");
487 if (!hSharedFile)
488 {
489 //MessageBox(NULL, "DebugTrace: Unable to create file mapping object DBWIN_BUFFER", "Error", MB_OK);
490 CloseHandle(heventDBWIN);
491 CloseHandle(heventData);
492 return;
493 }
494
495 lpszSharedMem = (LPSTR)MapViewOfFile(hSharedFile, FILE_MAP_WRITE, 0, 0, 512);
496 if (!lpszSharedMem)
497 {
498 //MessageBox(NULL, "DebugTrace: Unable to map shared memory", "Error", MB_OK);
499 CloseHandle(heventDBWIN);
500 CloseHandle(heventData);
501 return;
502 }
503
504 /* wait for buffer event */
505 WaitForSingleObject(heventDBWIN, INFINITE);
506
507 /* write it to the shared memory */
508 *((LPDWORD)lpszSharedMem) = 0;
509 wsprintf(lpszSharedMem + sizeof(DWORD), "%s", str);
510
511 /* signal data ready event */
512 SetEvent(heventData);
513
514 /* clean up handles */
515 CloseHandle(hSharedFile);
516 CloseHandle(heventData);
517 CloseHandle(heventDBWIN);
518
519 return;
520}
521#endif // WWDEBUG
#define NULL
Definition BaseType.h:92
#define FALSE
Definition BaseType.h:113
#define WWASSERT
#define __cdecl
Definition IFF.H:44
unsigned long DWORD
Definition bittype.h:57
ProfileFunc WWDebug_Install_Profile_Stop_Handler(ProfileFunc func)
Definition wwdebug.cpp:177
TriggerFunc WWDebug_Install_Trigger_Handler(TriggerFunc func)
Definition wwdebug.cpp:137
void WWDebug_Printf_Warning(const char *format,...)
Definition wwdebug.cpp:228
ProfileFunc WWDebug_Install_Profile_Start_Handler(ProfileFunc func)
Definition wwdebug.cpp:157
void Convert_System_Error_To_String(int id, char *buffer, int buf_len)
Definition wwdebug.cpp:66
PrintFunc WWDebug_Install_Message_Handler(PrintFunc func)
Definition wwdebug.cpp:97
void WWDebug_Profile_Stop(const char *title)
Definition wwdebug.cpp:439
AssertPrintFunc WWDebug_Install_Assert_Handler(AssertPrintFunc func)
Definition wwdebug.cpp:117
bool WWDebug_Check_Trigger(int trigger_num)
Definition wwdebug.cpp:397
void WWDebug_Profile_Start(const char *title)
Definition wwdebug.cpp:419
void WWDebug_Printf(const char *format,...)
Definition wwdebug.cpp:198
int Get_Last_System_Error()
Definition wwdebug.cpp:80
void WWDebug_Printf_Error(const char *format,...)
Definition wwdebug.cpp:258
@ WWDEBUG_TYPE_ERROR
Definition wwdebug.h:66
@ WWDEBUG_TYPE_INFORMATION
Definition wwdebug.h:64
@ WWDEBUG_TYPE_WARNING
Definition wwdebug.h:65
void(* ProfileFunc)(const char *title)
Definition wwdebug.h:73
void(* PrintFunc)(DebugType type, const char *message)
Definition wwdebug.h:70
bool(* TriggerFunc)(int trigger_num)
Definition wwdebug.h:72
void(* AssertPrintFunc)(const char *message)
Definition wwdebug.h:71