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.

95 lines
2.5 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997-2002.
  5. //
  6. // File: Debug.cpp
  7. //
  8. // Contents:
  9. //
  10. //----------------------------------------------------------------------------
  11. // Debug.cpp
  12. #include "stdafx.h"
  13. #include "debug.h"
  14. #include "util.h"
  15. #include "StrSafe.h"
  16. #ifdef DEBUG
  17. #define DoBreakpoint() DebugBreak()
  18. void DoDebugAssert(PCWSTR pszFile, int nLine, PCWSTR pszExpr)
  19. {
  20. if ( !pszFile || nLine < 0 || !pszExpr )
  21. return;
  22. // MSDN says itow returns a string of up to 33 wchars in length
  23. const size_t MAX_INT_WIDTH = 33;
  24. PCWSTR pwszFormat = L"Assertion: (%s)\nFile %s, line %d.";
  25. size_t cchFormat = 0;
  26. HRESULT hr = StringCchLength (pwszFormat, 1000, &cchFormat);
  27. if ( FAILED (hr) )
  28. return;
  29. size_t cchExpr = 0;
  30. hr = StringCchLength (pszExpr, 1000, &cchExpr);
  31. if ( FAILED (hr) )
  32. return;
  33. size_t cchFile = 0;
  34. hr = StringCchLength (pszFile, 1000, &cchFile);
  35. if ( FAILED (hr) )
  36. return;
  37. size_t cchBuf = cchFormat + cchExpr + cchFile + MAX_INT_WIDTH + 1;
  38. PWSTR pwszBuf = new WCHAR[cchBuf];
  39. if ( pwszBuf )
  40. {
  41. hr = StringCchPrintf (pwszBuf, cchBuf, pwszFormat, pszExpr, pszFile, nLine);
  42. if ( SUCCEEDED (hr) )
  43. {
  44. int nRet = MessageBox(::GetActiveWindow(), pwszBuf, L"Send Console Message - Assertion Failed",
  45. MB_ABORTRETRYIGNORE | MB_ICONERROR);
  46. switch (nRet)
  47. {
  48. case IDABORT:
  49. DoBreakpoint();
  50. exit(-1);
  51. case IDRETRY:
  52. DoBreakpoint();
  53. }
  54. }
  55. delete [] pwszBuf;
  56. }
  57. } // DoDebugAssert()
  58. /////////////////////////////////////////////////////////////////////////////
  59. void DebugTracePrintf(
  60. const WCHAR * szFormat,
  61. ...)
  62. {
  63. va_list arglist;
  64. const size_t BUF_LEN = 1024;
  65. WCHAR sz[BUF_LEN];
  66. Assert(szFormat != NULL);
  67. if ( !szFormat )
  68. return;
  69. va_start(arglist, szFormat);
  70. // ISSUE convert to strsafe
  71. HRESULT hr = StringCchPrintf(OUT sz, BUF_LEN, szFormat, arglist);
  72. if ( SUCCEEDED (hr) )
  73. {
  74. Assert(wcslen(sz) < LENGTH(sz));
  75. sz[LENGTH(sz) - 1] = 0; // Just in case we overflowed into sz
  76. ::OutputDebugString(sz);
  77. }
  78. } // DebugTracePrintf()
  79. #endif // DEBUG