Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

409 lines
11 KiB

  1. //---------------------------------------------------------------------------
  2. // MCSDebug.h
  3. //
  4. // The debug macros and support classes are declared in
  5. // this file, they are:
  6. //
  7. // MCSASSERT & MCSASSERTSZ:
  8. // These macros are only valid for a debug build. The
  9. // usage of these macros is outlined in the MCS Coding
  10. // Standards document. To support automated testing these
  11. // macros look to the environment variable MCS_TEST, if
  12. // this variable is defined a McsDebugException is generated.
  13. //
  14. // MCSEXCEPTION & MCSEXCEPTIONSZ
  15. // These macros are valid for debug and release builds.
  16. // In the debug mode these macros are the same as
  17. // MCSASSERT(SZ). In the release mode they throw an
  18. // exception McsException The usage of these macros is
  19. // outlined in the MCS Coding Standards document.
  20. //
  21. // MCSVERIFY & MCSVERIFYSZ
  22. // These macros are valid for debug and release builds.
  23. // In the debug mode these macros are the same as
  24. // MCSASSERT(SZ). In the release mode they log the
  25. // message using McsVerifyLog class. The usage of these
  26. // macros is outlined in the MCS Coding Standards document.
  27. //
  28. // MCSASSERTVALID
  29. // This macro is based on MCSASSERT(SZ) macros, and is
  30. // available only in debug mode. The usage of this macro
  31. // is outlined in the MCS Coding Standards document.
  32. // IMPORTANT: The macro expects the string allocation done
  33. // in the Validate function uses new operator.
  34. //
  35. // MCSEXCEPTIONVALID
  36. // This macro is based on MCSEXCEPTION(SZ) macros, and is
  37. // available in debug and release modes. The usage of this
  38. // macro is outlined in the MCS Coding Standards document.
  39. // IMPORTANT: The macro expects the string allocation done
  40. // in the Validate function uses new operator.
  41. //
  42. // MCSVERIFYVALID
  43. // This macro is based on MCSVERIFY(SZ) macros, and is
  44. // available debug and release modes. The usage of this
  45. // macro is outlined in the MCS Coding Standards document.
  46. // IMPORTANT: The macro expects the string allocation done
  47. // in the Validate function uses new operator.
  48. //
  49. // McsVerifyLog:
  50. // The McsVerifyLog class is used by MCSVERIFY(SZ) macros
  51. // to log verify messages. This class uses the McsDebugLog
  52. // class to log messages. The user can change the ostream
  53. // object to log messages else where.
  54. // The output log file is created in the directory
  55. // defined by MCS_LOG environment variable, or in the
  56. // TEMP directory, or in the current directory. The name
  57. // of the output log file is <module name>.err.
  58. //
  59. // McsTestLog:
  60. // The McsTestLog class is used by MCSASSERT(SZ) macros
  61. // to log messages in test mode. This class currently
  62. // generates an exception. This may have to be modified
  63. // to support any new functionality required for unit
  64. // testing.
  65. //
  66. // (c) Copyright 1995-1998, Mission Critical Software, Inc., All Rights Reserved
  67. //
  68. // Proprietary and confidential to Mission Critical Software, Inc.
  69. //---------------------------------------------------------------------------
  70. #ifndef MCSINC_McsDebug_h
  71. #define MCSINC_McsDebug_h
  72. #ifdef __cplusplus /* C++ */
  73. #ifdef WIN16_VERSION /* WIN16_VERSION */
  74. #include <assert.h>
  75. // -------------------------------
  76. // MCSASSERT & MCSASSERTSZ macros.
  77. // -------------------------------
  78. #define MCSASSERT(expr) assert(expr)
  79. #define MCSASSERTSZ(expr,msg) assert(expr)
  80. // -----------------------------
  81. // MCSEXCEPTION & MCSEXCEPTIONSZ
  82. // -----------------------------
  83. #define MCSEXCEPTION(expr) MCSASSERT(expr)
  84. #define MCSEXCEPTIONSZ(expr,msg) MCSASSERTSZ(expr,msg)
  85. // -----------------------
  86. // MCSVERIFY & MCSVERIFYSZ
  87. // -----------------------
  88. #define MCSVERIFY(expr) MCSASSERT(expr)
  89. #define MCSVERIFYSZ(expr,msg) MCSASSERTSZ(expr,msg)
  90. // --------------
  91. // MCSASSERTVALID
  92. // --------------
  93. #define MCSASSERTVALID() \
  94. do { \
  95. char *msg = 0; \
  96. int flag = Validate(&msg); \
  97. MCSASSERTSZ(flag, msg); \
  98. delete [] msg; \
  99. } while (0)
  100. // -----------------
  101. // MCSEXCEPTIONVALID
  102. // -----------------
  103. #define MCSEXCEPTIONVALID() \
  104. do { \
  105. char *msg = 0; \
  106. int flag = Validate(&msg); \
  107. MCSEXCEPTIONSZ(flag, msg); \
  108. delete [] msg; \
  109. } while (0)
  110. // --------------
  111. // MCSVERIFYVALID
  112. // --------------
  113. #define MCSVERIFYVALID() \
  114. do { \
  115. char *msg = 0; \
  116. int flag = Validate(&msg); \
  117. MCSVERIFYSZ(flag, msg); \
  118. delete [] msg; \
  119. } while (0)
  120. #else /* WIN16_VERSION */
  121. #include <crtdbg.h>
  122. #include "McsDbgU.h"
  123. // ------------
  124. // McsException
  125. // ------------
  126. class McsDebugException {
  127. public:
  128. McsDebugException ();
  129. McsDebugException (const McsDebugException &sourceIn);
  130. McsDebugException (const char *messageIn,
  131. const char *fileNameIn,
  132. int lineNumIn);
  133. ~McsDebugException();
  134. McsDebugException& operator= (const McsDebugException &rhsIn);
  135. const char *getMessage (void) const;
  136. const char *getFileName (void) const;
  137. int getLineNum (void) const;
  138. private:
  139. char *m_message;
  140. char *m_fileName;
  141. int m_lineNum;
  142. };
  143. // ------------
  144. // McsVerifyLog
  145. // ------------
  146. class McsVerifyLog {
  147. public:
  148. // No public ctors only way to access
  149. // an object of this class by using the
  150. // getLog function. This required for
  151. // the correct static initializations.
  152. static McsVerifyLog* getLog (void);
  153. ~McsVerifyLog (void);
  154. void changeLog (ostream *outStreamIn);
  155. void log (const char *messageIn,
  156. const char *fileNameIn,
  157. int lineNumIn);
  158. private:
  159. McsVerifyLog (void);
  160. const char* getLogFileName (void);
  161. void formatMsg (const char *messageIn,
  162. const char *fileNameIn,
  163. int lineNumIn);
  164. // Do not allow dflt ctor, copy ctor & operator=.
  165. McsVerifyLog (const McsVerifyLog&);
  166. McsVerifyLog& operator= (const McsVerifyLog&);
  167. private:
  168. enum { MSG_BUF_LEN = 2048 };
  169. char m_msgBuf[MSG_BUF_LEN];
  170. McsDebugUtil::McsDebugCritSec m_logSec;
  171. McsDebugUtil::McsDebugLog m_log;
  172. fstream *m_outLog;
  173. };
  174. // ----------
  175. // McsTestLog
  176. // ----------
  177. class McsTestLog {
  178. public:
  179. // No public ctors only way to access
  180. // an object of this class by using the
  181. // getLog function. This required for
  182. // the correct static initializations.
  183. static McsTestLog* getLog (void);
  184. ~McsTestLog (void);
  185. bool isTestMode (void);
  186. void log (const char *messageIn,
  187. const char *fileNameIn,
  188. int lineNumIn);
  189. private:
  190. McsTestLog (void);
  191. // Do not allow copy ctor & operator=.
  192. McsTestLog (const McsTestLog&);
  193. McsTestLog& operator= (const McsTestLog&);
  194. private:
  195. bool m_isTested;
  196. bool m_isTestMode_;
  197. McsDebugUtil::McsDebugCritSec m_testSec;
  198. };
  199. // -------------------------------
  200. // MCSASSERT & MCSASSERTSZ macros.
  201. // -------------------------------
  202. #ifdef _DEBUG
  203. #define MCSASSERT(expr) \
  204. do { \
  205. if (!(expr)) { \
  206. if (McsTestLog::getLog()->isTestMode()) { \
  207. McsTestLog::getLog()->log (#expr, __FILE__, __LINE__); \
  208. } else { \
  209. if (1 == _CrtDbgReport(_CRT_ASSERT, __FILE__, __LINE__, NULL, #expr)) { \
  210. _CrtDbgBreak(); \
  211. } \
  212. } \
  213. } \
  214. } while (0)
  215. #else // _DEBUG
  216. #define MCSASSERT(expr) ((void)0)
  217. #endif // _DEBUG
  218. #ifdef _DEBUG
  219. #define MCSASSERTSZ(expr,msg) \
  220. do { \
  221. if (!(expr)) { \
  222. if (McsTestLog::getLog()->isTestMode()) { \
  223. McsTestLog::getLog()->log (msg, __FILE__, __LINE__); \
  224. } else { \
  225. if (1 == _CrtDbgReport(_CRT_ASSERT, __FILE__, \
  226. __LINE__, NULL, msg)) { \
  227. _CrtDbgBreak(); \
  228. } \
  229. } \
  230. } \
  231. } while (0)
  232. #else // _DEBUG
  233. #define MCSASSERTSZ(expr,msg) ((void)0)
  234. #endif // _DEBUG
  235. // -----------------------------
  236. // MCSEXCEPTION & MCSEXCEPTIONSZ
  237. // -----------------------------
  238. #ifdef _DEBUG
  239. #define MCSEXCEPTION(expr) MCSASSERT(expr)
  240. #else
  241. #define MCSEXCEPTION(expr) \
  242. do { \
  243. if (!(expr)) { \
  244. throw new McsDebugException (#expr, __FILE__, \
  245. __LINE__); \
  246. } \
  247. } while (0)
  248. #endif
  249. #ifdef _DEBUG
  250. #define MCSEXCEPTIONSZ(expr,msg) MCSASSERTSZ(expr,msg)
  251. #else
  252. #define MCSEXCEPTIONSZ(expr,msg) \
  253. do { \
  254. if (!(expr)) { \
  255. throw new McsDebugException ((msg), __FILE__, \
  256. __LINE__); \
  257. } \
  258. } while (0)
  259. #endif
  260. // -----------------------
  261. // MCSVERIFY & MCSVERIFYSZ
  262. // -----------------------
  263. #ifdef _DEBUG
  264. #define MCSVERIFY(expr) MCSASSERT(expr)
  265. #else
  266. #define MCSVERIFY(expr) \
  267. do { \
  268. if (!(expr)) { \
  269. McsVerifyLog::getLog()->log (#expr, __FILE__, __LINE__); \
  270. } \
  271. } while (0)
  272. #endif
  273. #ifdef _DEBUG
  274. #define MCSVERIFYSZ(expr,msg) MCSASSERTSZ(expr,msg)
  275. #else
  276. #define MCSVERIFYSZ(expr,msg) \
  277. do { \
  278. if (!(expr)) { \
  279. McsVerifyLog::getLog()->log ((msg), __FILE__, \
  280. __LINE__); \
  281. } \
  282. } while (0)
  283. #endif
  284. // --------------
  285. // MCSASSERTVALID
  286. // --------------
  287. #define MCSASSERTVALID() \
  288. do { \
  289. char *msg = NULL; \
  290. int flag = Validate(&msg); \
  291. MCSASSERTSZ(flag, msg); \
  292. delete [] msg; \
  293. } while (0)
  294. // -----------------
  295. // MCSEXCEPTIONVALID
  296. // -----------------
  297. #define MCSEXCEPTIONVALID() \
  298. do { \
  299. char *msg = NULL; \
  300. int flag = Validate(&msg); \
  301. MCSEXCEPTIONSZ(flag, msg); \
  302. delete [] msg; \
  303. } while (0)
  304. // --------------
  305. // MCSVERIFYVALID
  306. // --------------
  307. #define MCSVERIFYVALID() \
  308. do { \
  309. char *msg = NULL; \
  310. int flag = Validate(&msg); \
  311. MCSVERIFYSZ(flag, msg); \
  312. delete [] msg; \
  313. } while (0)
  314. // ---------------
  315. // --- INLINES ---
  316. // ---------------
  317. // -----------------
  318. // McsDebugException
  319. // -----------------
  320. inline McsDebugException::McsDebugException ()
  321. : m_message (0), m_fileName (0), m_lineNum (0)
  322. { /* EMPTY */ }
  323. inline McsDebugException::~McsDebugException() {
  324. delete [] m_message;
  325. delete [] m_fileName;
  326. }
  327. inline const char *McsDebugException::getMessage
  328. (void) const {
  329. return m_message;
  330. }
  331. inline const char *McsDebugException::getFileName
  332. (void) const {
  333. return m_fileName;
  334. }
  335. inline int McsDebugException::getLineNum
  336. (void) const {
  337. return m_lineNum;
  338. }
  339. // ------------
  340. // McsVerifyLog
  341. // ------------
  342. inline McsVerifyLog::McsVerifyLog (void)
  343. { /* EMPTY */ }
  344. inline McsVerifyLog::~McsVerifyLog (void) {
  345. delete m_outLog;
  346. }
  347. // ----------
  348. // McsTestLog
  349. // ----------
  350. inline McsTestLog::McsTestLog (void)
  351. : m_isTested (FALSE), m_isTestMode_ (FALSE)
  352. { /* EMPTY */ }
  353. inline McsTestLog::~McsTestLog (void) { /* EMPTY */ }
  354. inline void McsTestLog::log (const char *messageIn,
  355. const char *fileNameIn,
  356. int lineNumIn) {
  357. throw new McsDebugException (messageIn, fileNameIn,
  358. lineNumIn);
  359. }
  360. #endif /* WIN16_VERSION */
  361. #endif /* C++ */
  362. #endif /* MCSINC_McsDebug_h */