Debug.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: Debug.h
  24. //-----------------------------------------------------------------------------
  25. //
  26. // Westwood Studios Pacific.
  27. //
  28. // Confidential Information
  29. // Copyright (C) 2001 - All Rights Reserved
  30. //
  31. //-----------------------------------------------------------------------------
  32. //
  33. // Project: RTS3
  34. //
  35. // File name: Debug.h
  36. //
  37. // Created: Steven Johnson, August 2001
  38. //
  39. // Desc: Debug Utilities
  40. //
  41. //-----------------------------------------------------------------------------
  42. ///////////////////////////////////////////////////////////////////////////////
  43. #pragma once
  44. #ifndef __DEBUG_H_
  45. #define __DEBUG_H_
  46. class AsciiString;
  47. #if defined(_DEBUG) && defined(_INTERNAL)
  48. #error "Only one at a time of these should ever be defined"
  49. #endif
  50. #define NO_RELEASE_DEBUG_LOGGING
  51. #ifdef RELEASE_DEBUG_LOGGING ///< Creates a DebugLogFile.txt (No I or D) with all the debug log goodness. Good for startup problems.
  52. #define ALLOW_DEBUG_UTILS 1
  53. #define DEBUG_LOGGING 1
  54. #define DISABLE_DEBUG_CRASHING 1
  55. #define DISABLE_DEBUG_STACKTRACE 1
  56. #define DISABLE_DEBUG_PROFILE 1
  57. #endif
  58. // These are stolen from the WW3D Debug file. REALLY useful. :-)
  59. #define STRING_IT(a) #a
  60. #define TOKEN_IT(a) STRING_IT(,##a)
  61. #define MESSAGE(a) message (__FILE__ "(" TOKEN_IT(__LINE__) ") : " a)
  62. // by default, turn on ALLOW_DEBUG_UTILS if _DEBUG is turned on.
  63. #if (defined(_DEBUG) || defined(_INTERNAL)) && !defined(ALLOW_DEBUG_UTILS) && !defined(DISABLE_ALLOW_DEBUG_UTILS)
  64. #define ALLOW_DEBUG_UTILS 1
  65. #endif
  66. // these are predicated on ALLOW_DEBUG_UTILS, not _DEBUG, and allow you to selectively disable
  67. // bits of the debug stuff for special builds.
  68. #if defined(ALLOW_DEBUG_UTILS) && !defined(DEBUG_LOGGING) && !defined(DISABLE_DEBUG_LOGGING)
  69. #define DEBUG_LOGGING 1
  70. #endif
  71. #if defined(ALLOW_DEBUG_UTILS) && !defined(DEBUG_CRASHING) && !defined(DISABLE_DEBUG_CRASHING)
  72. #define DEBUG_CRASHING 1
  73. #endif
  74. #if defined(ALLOW_DEBUG_UTILS) && !defined(DEBUG_STACKTRACE) && !defined(DISABLE_DEBUG_STACKTRACE)
  75. #define DEBUG_STACKTRACE 1
  76. #endif
  77. #if defined(ALLOW_DEBUG_UTILS) && !defined(DEBUG_PROFILE) && !defined(DISABLE_DEBUG_PROFILE)
  78. #define DEBUG_PROFILE 1
  79. #endif
  80. #ifdef __cplusplus
  81. #define DEBUG_EXTERN_C extern "C"
  82. #else
  83. #define DEBUG_EXTERN_C extern
  84. #endif
  85. // SYSTEM INCLUDES ////////////////////////////////////////////////////////////
  86. // USER INCLUDES //////////////////////////////////////////////////////////////
  87. // FORWARD REFERENCES /////////////////////////////////////////////////////////
  88. // TYPE DEFINES ///////////////////////////////////////////////////////////////
  89. // INLINING ///////////////////////////////////////////////////////////////////
  90. // EXTERNALS //////////////////////////////////////////////////////////////////
  91. /// @todo: the standard line-to-string trick isn't working correctly in vc6; figure out why
  92. #define DEBUG_STRING_IT(b) #b
  93. #define DEBUG_TOKEN_IT(a) DEBUG_STRING_IT(a)
  94. #define DEBUG_FILENLINE __FILE__ ":" DEBUG_TOKEN_IT(__LINE__)
  95. #ifdef ALLOW_DEBUG_UTILS
  96. enum
  97. {
  98. DEBUG_FLAG_LOG_TO_FILE = 0x01,
  99. DEBUG_FLAG_LOG_TO_CONSOLE = 0x02,
  100. DEBUG_FLAG_PREPEND_TIME = 0x04,
  101. #ifdef _INTERNAL
  102. // by default, _INTERNAL builds log to file, but not to console, in the interest
  103. // of speed. want console output? just change this line:
  104. DEBUG_FLAGS_DEFAULT = (DEBUG_FLAG_LOG_TO_FILE)
  105. #else
  106. DEBUG_FLAGS_DEFAULT = (DEBUG_FLAG_LOG_TO_FILE | DEBUG_FLAG_LOG_TO_CONSOLE)
  107. #endif
  108. };
  109. DEBUG_EXTERN_C void DebugInit(int flags);
  110. DEBUG_EXTERN_C void DebugShutdown();
  111. DEBUG_EXTERN_C int DebugGetFlags();
  112. DEBUG_EXTERN_C void DebugSetFlags(int flags);
  113. #define DEBUG_INIT(f) do { DebugInit(f); } while (0)
  114. #define DEBUG_SHUTDOWN() do { DebugShutdown(); } while (0)
  115. #else
  116. #define DEBUG_INIT(f) ((void)0)
  117. #define DEBUG_SHUTDOWN() ((void)0)
  118. #endif
  119. #ifdef DEBUG_LOGGING
  120. DEBUG_EXTERN_C void DebugLog(const char *format, ...);
  121. #define DEBUG_LOG(m) do { { DebugLog m ; } } while (0)
  122. #define DEBUG_ASSERTLOG(c, m) do { { if (!(c)) DebugLog m ; } } while (0)
  123. #else
  124. #define DEBUG_LOG(m) ((void)0)
  125. #define DEBUG_ASSERTLOG(c, m) ((void)0)
  126. #endif
  127. #ifdef DEBUG_CRASHING
  128. DEBUG_EXTERN_C void DebugCrash(const char *format, ...);
  129. /*
  130. Yeah, it's a sleazy global, since we can't reasonably add
  131. any args to DebugCrash due to the varargs nature of it.
  132. We'll just let it slide in this case...
  133. */
  134. DEBUG_EXTERN_C char* TheCurrentIgnoreCrashPtr;
  135. #define DEBUG_CRASH(m) \
  136. do { \
  137. { \
  138. static char ignoreCrash = 0; \
  139. if (!ignoreCrash) { \
  140. TheCurrentIgnoreCrashPtr = &ignoreCrash; \
  141. DebugCrash m ; \
  142. TheCurrentIgnoreCrashPtr = NULL; \
  143. } \
  144. } \
  145. } while (0)
  146. #define DEBUG_ASSERTCRASH(c, m) do { { if (!(c)) DEBUG_CRASH(m); } } while (0)
  147. //Note: RELEASE_CRASH(m) is now always defined.
  148. //#define RELEASE_CRASH(m) DEBUG_CRASH((m))
  149. #else
  150. #define DEBUG_CRASH(m) ((void)0)
  151. #define DEBUG_ASSERTCRASH(c, m) ((void)0)
  152. // DEBUG_EXTERN_C void ReleaseCrash(const char* reason);
  153. // #define RELEASE_CRASH(m) do { ReleaseCrash(m); } while (0)
  154. #endif
  155. DEBUG_EXTERN_C void ReleaseCrash(const char* reason);
  156. DEBUG_EXTERN_C void ReleaseCrashLocalized(const AsciiString& p, const AsciiString& m);
  157. #define RELEASE_CRASH(m) do { ReleaseCrash(m); } while (0)
  158. #define RELEASE_CRASHLOCALIZED(p, m) do { ReleaseCrashLocalized(p, m); } while (0)
  159. #ifdef DEBUG_PROFILE
  160. class SimpleProfiler
  161. {
  162. private:
  163. __int64 m_freq;
  164. __int64 m_startThisSession;
  165. __int64 m_totalThisSession;
  166. __int64 m_totalAllSessions;
  167. int m_numSessions;
  168. public:
  169. SimpleProfiler();
  170. void start();
  171. void stop();
  172. void stopAndLog(const char *msg, int howOftenToLog, int howOftenToResetAvg);
  173. double getTime(); // of most recent session, in milliseconds
  174. int getNumSessions();
  175. double getTotalTime(); // total over all sessions, in milliseconds
  176. double getAverageTime(); // averaged over all sessions, in milliseconds
  177. };
  178. #define BEGIN_PROFILE(uniqueid) \
  179. static SimpleProfiler prof_##uniqueid; \
  180. prof_##uniqueid.start();
  181. #define END_PROFILE(uniqueid, msg, howoftentolog, howoftentoreset) \
  182. prof_##uniqueid.stopAndLog(msg, howoftentolog, howoftentoreset);
  183. #else
  184. #define BEGIN_PROFILE(uniqueid)
  185. #define END_PROFILE(uniqueid, msg, howoftentolog, howoftentoreset)
  186. #endif
  187. // MACROS //////////////////////////////////////////////////////////////////
  188. #endif // __DEBUG_H_