wdebug.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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-UnSafe! (UNIX version is safe though)
  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 "odevice.h"
  48. #include "streamer.h"
  49. #include <time.h>
  50. // Print an information message
  51. #define INFMSG(X)\
  52. {\
  53. char timebuf[40]; \
  54. time_t clock=time(NULL); \
  55. cftime(timebuf,"%D %T",&clock); \
  56. if (MsgManager::infoStream()) \
  57. (*(MsgManager::infoStream())) << "INF " << timebuf << " [" << \
  58. __FILE__ << " " << __LINE__ << "] " << X << endl; \
  59. }
  60. // Print a warning message
  61. #define WRNMSG(X)\
  62. {\
  63. char timebuf[40]; \
  64. time_t clock=time(NULL); \
  65. cftime(timebuf,"%D %T",&clock); \
  66. if (MsgManager::warnStream()) \
  67. (*(MsgManager::warnStream())) << "WRN " << timebuf << " [" << \
  68. __FILE__ << " " << __LINE__ << "] " << X << endl; \
  69. }
  70. // Print an error message
  71. #define ERRMSG(X)\
  72. {\
  73. char timebuf[40]; \
  74. time_t clock=time(NULL); \
  75. strcpy(timebuf,ctime(&clock)); \
  76. if (MsgManager::errorStream()) \
  77. (*(MsgManager::errorStream())) << "ERR " << timebuf << " [" << \
  78. __FILE__ << " " << __LINE__ << "] " << X << endl; \
  79. }
  80. // Just get a stream to the information device, no extra junk
  81. #define INFSTREAM(X)\
  82. {\
  83. if (MsgManager::infoStream()) \
  84. (*(MsgManager::infoStream())) << X;\
  85. }
  86. // Just get a stream to the warning device, no extra junk
  87. #define WRNSTREAM(X)\
  88. {\
  89. if (MsgManager::warnStream()) \
  90. (*(MsgManager::warnStream())) << X;\
  91. }
  92. // Just get a stream to the error device, no extra junk
  93. #define ERRSTREAM(X)\
  94. {\
  95. if (MsgManager::errorStream()) \
  96. (*(MsgManager::errorStream())) << X;\
  97. }
  98. #ifndef DEBUG
  99. // No debugging, no debug messages.
  100. // Note that anything enclosed in "DBG()" will NOT get executed
  101. // unless DEBUG is defined.
  102. // They are defined to {} for consistency when DEBUG is defined
  103. #define DBG(X)
  104. #define DBGSTREAM(X) {}
  105. #define PVAR(v) {}
  106. #define DBGMSG(X) {}
  107. #define VERBOSE(X) {}
  108. #else // DEBUG _is_ defined
  109. // Execute only if in debugging mode
  110. #define DBG(X) X
  111. // Print a variable
  112. #define PVAR(v) \
  113. { \
  114. if (MsgManager::debugStream()) \
  115. (*(MsgManager::debugStream())) << __FILE__ << "[" << __LINE__ << \
  116. "]: " << ##V << " = " << V << endl; \
  117. }
  118. #define DBGMSG(X)\
  119. {\
  120. if (MsgManager::debugStream()) \
  121. (*(MsgManager::debugStream())) << "DBG [" << __FILE__ << \
  122. " " << __LINE__ << "] " << X << endl;\
  123. }
  124. // Just get a stream to the debugging device, no extra junk
  125. #define DBGSTREAM(X)\
  126. {\
  127. if (MsgManager::debugStream()) \
  128. (*(MsgManager::debugStream())) << X;\
  129. }
  130. // Verbosely execute a statement
  131. #define VERBOSE(X)\
  132. { \
  133. if (MsgManager::debugStream()) \
  134. (*(DebugManager::debugStream())) << __FILE__ << "[" << __LINE__ << \
  135. "]: " << ##X << endl; X \
  136. }
  137. #endif // DEBUG
  138. class MsgManager
  139. {
  140. protected:
  141. MsgManager();
  142. public:
  143. static int setAllStreams(OutputDevice *device);
  144. static int setDebugStream(OutputDevice *device);
  145. static int setInfoStream(OutputDevice *device);
  146. static int setWarnStream(OutputDevice *device);
  147. static int setErrorStream(OutputDevice *device);
  148. static void enableDebug(int flag);
  149. static void enableInfo(int flag);
  150. static void enableWarn(int flag);
  151. static void enableError(int flag);
  152. static ostream *debugStream(void);
  153. static ostream *infoStream(void);
  154. static ostream *warnStream(void);
  155. static ostream *errorStream(void);
  156. };
  157. #endif