wdebug.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. wdebug Neal Kettler
  20. MT-LEVEL
  21. MT-Safe
  22. The debugging module is pretty good for debugging and it has some message
  23. printing stuff as well. The basic idea is that you write a class that
  24. inherits from OutputDevice (severall are provided) and assign that output
  25. device to a stream. There are seperate streams for debugging, information,
  26. warning, and error messages. Each one can have a seperate output device,
  27. or they can all have the same one. Debugging messages only get compiled
  28. in if your module defines 'DEBUG'. If you don't define debug, then not even
  29. the text of the debugging message gets into the binary. All the other
  30. output streams get printed regardless of whether DEBUG is defined.
  31. Sample usage:
  32. FileD debug_device("gameres.debug"); // create a file device
  33. MsgManager::setDebugStream(&debug_device);
  34. DBGMSG("This debug message #" << 1 << " you use C++ streams");
  35. Note that since these are defines you really don't need to put a semicolon
  36. at the end, and it can be bad in situations like this:
  37. if (x)
  38. DBGMSG("Stuff is broken");
  39. else
  40. DBGMSG("Stuff is NOT broken");
  41. This won't compile, read the code until you figure it out. Only then
  42. will you be ready to leave grasshopper.
  43. \*****************************************************************************/
  44. #ifndef WDEBUG_HEADER
  45. #define WDEBUG_HEADER
  46. #include <iostream.h>
  47. #include "sem4.h"
  48. #include "odevice.h"
  49. #include "streamer.h"
  50. // This is needed because the streams return a pointer. Every time you
  51. // change the output device the old stream is deleted, and a new one
  52. // is created.
  53. extern Sem4 DebugLibSemaphore;
  54. // Print an information message
  55. #define INFMSG(X)\
  56. {\
  57. char timebuf[40]; \
  58. time_t clock=time(NULL); \
  59. cftime(timebuf,"%D %T",&clock); \
  60. DebugLibSemaphore.Wait(); \
  61. if (MsgManager::infoStream()) \
  62. (*(MsgManager::infoStream())) << "INF " << timebuf << " [" << \
  63. __FILE__ << " " << __LINE__ << "] " << X << endl; \
  64. DebugLibSemaphore.Post(); \
  65. }
  66. // Print a warning message
  67. #define WRNMSG(X)\
  68. {\
  69. char timebuf[40]; \
  70. time_t clock=time(NULL); \
  71. cftime(timebuf,"%D %T",&clock); \
  72. DebugLibSemaphore.Wait(); \
  73. if (MsgManager::warnStream()) \
  74. (*(MsgManager::warnStream())) << "WRN " << timebuf << " [" << \
  75. __FILE__ << " " << __LINE__ << "] " << X << endl; \
  76. DebugLibSemaphore.Post(); \
  77. }
  78. // Print an error message
  79. #define ERRMSG(X)\
  80. {\
  81. char timebuf[40]; \
  82. time_t clock=time(NULL); \
  83. cftime(timebuf,"%D %T",&clock); \
  84. DebugLibSemaphore.Wait(); \
  85. if (MsgManager::errorStream()) \
  86. (*(MsgManager::errorStream())) << "ERR " << timebuf << " [" << \
  87. __FILE__ << " " << __LINE__ << "] " << X << endl; \
  88. DebugLibSemaphore.Post(); \
  89. }
  90. // Just get a stream to the information device, no extra junk
  91. #define INFSTREAM(X)\
  92. {\
  93. DebugLibSemaphore.Wait(); \
  94. if (MsgManager::infoStream()) \
  95. (*(MsgManager::infoStream())) << X;\
  96. DebugLibSemaphore.Post(); \
  97. }
  98. // Just get a stream to the warning device, no extra junk
  99. #define WRNSTREAM(X)\
  100. {\
  101. DebugLibSemaphore.Wait(); \
  102. if (MsgManager::warnStream()) \
  103. (*(MsgManager::warnStream())) << X;\
  104. DebugLibSemaphore.Post(); \
  105. }
  106. // Just get a stream to the error device, no extra junk
  107. #define ERRSTREAM(X)\
  108. {\
  109. DebugLibSemaphore.Wait(); \
  110. if (MsgManager::errorStream()) \
  111. (*(MsgManager::errorStream())) << X;\
  112. DebugLibSemaphore.Post(); \
  113. }
  114. #ifndef DEBUG
  115. // No debugging, no debug messages.
  116. // Note that anything enclosed in "DBG()" will NOT get executed
  117. // unless DEBUG is defined.
  118. // They are defined to {} for consistency when DEBUG is defined
  119. #define DBG(X)
  120. #define DBGSTREAM(X) {}
  121. #define PVAR(v) {}
  122. #define DBGMSG(X) {}
  123. #define VERBOSE(X) {}
  124. #else // DEBUG _is_ defined
  125. // Execute only if in debugging mode
  126. #define DBG(X) X
  127. // Print a variable
  128. #define PVAR(v) \
  129. { \
  130. DebugLibSemaphore.Wait(); \
  131. if (MsgManager::debugStream()) \
  132. (*(MsgManager::debugStream())) << __FILE__ << "[" << __LINE__ << \
  133. "]: " << ##V << " = " << V << endl; \
  134. DebugLibSemaphore.Post(); \
  135. }
  136. #define DBGMSG(X)\
  137. {\
  138. DebugLibSemaphore.Wait(); \
  139. if (MsgManager::debugStream()) \
  140. (*(MsgManager::debugStream())) << "DBG [" << __FILE__ << \
  141. " " << __LINE__ << "] " << X << endl;\
  142. DebugLibSemaphore.Post(); \
  143. }
  144. // Just get a stream to the debugging device, no extra junk
  145. #define DBGSTREAM(X)\
  146. {\
  147. DebugLibSemaphore.Wait(); \
  148. if (MsgManager::debugStream()) \
  149. (*(MsgManager::debugStream())) << X;\
  150. DebugLibSemaphore.Post(); \
  151. }
  152. // Verbosely execute a statement
  153. #define VERBOSE(X)\
  154. { \
  155. DebugLibSemaphore.Wait(); \
  156. if (MsgManager::debugStream()) \
  157. (*(DebugManager::debugStream())) << __FILE__ << "[" << __LINE__ << \
  158. "]: " << ##X << endl; X \
  159. DebugLibSemaphore.Post(); \
  160. }
  161. #endif // DEBUG
  162. class MsgManager
  163. {
  164. protected:
  165. MsgManager();
  166. public:
  167. static int setAllStreams(OutputDevice *device);
  168. static int setDebugStream(OutputDevice *device);
  169. static int setInfoStream(OutputDevice *device);
  170. static int setWarnStream(OutputDevice *device);
  171. static int setErrorStream(OutputDevice *device);
  172. static void enableDebug(int flag);
  173. static void enableInfo(int flag);
  174. static void enableWarn(int flag);
  175. static void enableError(int flag);
  176. static ostream *debugStream(void);
  177. static ostream *infoStream(void);
  178. static ostream *warnStream(void);
  179. static ostream *errorStream(void);
  180. };
  181. #endif