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.

105 lines
2.2 KiB

  1. //
  2. // Debug.cpp
  3. //
  4. // Debug functionality shared between different projects (for use in
  5. // non-MFC projects).
  6. //
  7. // History:
  8. //
  9. // 3/??/96 KenSh Copied from InetSDK sample, added AfxTrace from MFC
  10. // 4/10/96 KenSh Renamed AfxTrace to MyTrace (to avoid linking conflicts
  11. // when linking with MFC).
  12. // 11/15/96 KenSh Automatically break on assert within assert
  13. //
  14. #include "stdafx.h"
  15. #ifdef _DEBUG
  16. #include "Debug.h"
  17. #include <stdlib.h>
  18. #define new DEBUG_NEW
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. // determine number of elements in an array (not bytes)
  22. #ifndef _countof
  23. #define _countof(array) (sizeof(array)/sizeof((array)[0]))
  24. #endif
  25. //*** Globals
  26. //
  27. static BOOL g_bInAssert = FALSE;
  28. // DisplayAssert
  29. //
  30. // Given a file and line number, displays an Assertion dialog box with
  31. // Abort/Retry/Ignore choices.
  32. //
  33. // Returns TRUE if the program should break into the debugger, else FALSE.
  34. //
  35. extern "C" BOOL DisplayAssert(LPCSTR pszMessage, LPCSTR pszFile, UINT nLine)
  36. {
  37. char szMsg[250];
  38. if (!pszFile)
  39. pszFile = _T("Unknown file");
  40. if (!pszMessage)
  41. pszMessage = _T("");
  42. // Break on assert within assert
  43. if (g_bInAssert)
  44. {
  45. AfxDebugBreak();
  46. return FALSE;
  47. }
  48. wnsprintf(szMsg, ARRAYSIZE(szMsg), _T("Assertion Failed! Abort, Break, or Ignore?\n\nFile: %s\nLine: %d\n\n%s"),
  49. pszFile, nLine, pszMessage);
  50. HWND hwndActive = GetActiveWindow();
  51. // Put up a dialog box
  52. //
  53. g_bInAssert = TRUE;
  54. int nResult = MessageBox(hwndActive, szMsg, _T("Assertion failed!"),
  55. MB_ICONHAND | MB_ABORTRETRYIGNORE | MB_SYSTEMMODAL);
  56. g_bInAssert = FALSE;
  57. switch(nResult)
  58. {
  59. case IDABORT:
  60. FatalAppExit(0, _T("Bye"));
  61. return FALSE;
  62. case IDRETRY:
  63. return TRUE; // Need to break into debugger
  64. default:
  65. return FALSE; // continue.
  66. }
  67. }
  68. void __cdecl MyTrace(const char* lpszFormat, ...)
  69. {
  70. va_list args;
  71. va_start(args, lpszFormat);
  72. int nBuf;
  73. TCHAR szBuffer[512];
  74. nBuf = wvnsprintf(szBuffer, ARRAYSIZE(szBuffer), lpszFormat, args);
  75. ASSERT(nBuf < _countof(szBuffer));
  76. OutputDebugString(szBuffer);
  77. va_end(args);
  78. }
  79. #endif // _DEBUG