home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / appwiz / customwz / template / debug.cpp < prev    next >
C/C++ Source or Header  |  1998-03-05  |  2KB  |  81 lines

  1. #include "stdafx.h"
  2.  
  3. #ifdef _PSEUDO_DEBUG   // entire file
  4.  
  5. #ifdef _PSEUDO_DEBUG
  6. #undef THIS_FILE
  7. static char THIS_FILE[] = __FILE__;
  8. #endif
  9.  
  10. LONG AssertBusy = -1;
  11. LONG AssertReallyBusy = -1;
  12.  
  13. BOOL AssertFailedLine(LPCSTR lpszFileName, int nLine)
  14. {
  15.     TCHAR szMessage[_MAX_PATH*2];
  16.  
  17.     InterlockedDecrement(&AssertReallyBusy);
  18.  
  19.     // format message into buffer
  20.     wsprintf(szMessage, _T("File %hs, Line %d"),
  21.         lpszFileName, nLine);
  22.  
  23.     TCHAR szT[_MAX_PATH*2 + 20];
  24.     wsprintf(szT, _T("Assertion Failed: %s\n"), szMessage);
  25.     OutputDebugString(szT);
  26.  
  27.     if (InterlockedIncrement(&AssertBusy) > 0)
  28.     {
  29.         InterlockedDecrement(&AssertBusy);
  30.  
  31.         // assert within assert (examine call stack to determine first one)
  32.         DebugBreak();
  33.         return FALSE;
  34.     }
  35.  
  36.     // active popup window for the current thread
  37.     HWND hWndParent = GetActiveWindow();
  38.     if (hWndParent != NULL)
  39.         hWndParent = GetLastActivePopup(hWndParent);
  40.  
  41.     // display the assert
  42.     int nCode = ::MessageBox(hWndParent, szMessage, _T("Assertion Failed!"),
  43.         MB_TASKMODAL|MB_ICONHAND|MB_ABORTRETRYIGNORE|MB_SETFOREGROUND);
  44.  
  45.     // cleanup
  46.     InterlockedDecrement(&AssertBusy);
  47.  
  48.     if (nCode == IDIGNORE)
  49.         return FALSE;   // ignore
  50.  
  51.     if (nCode == IDRETRY)
  52.         return TRUE;    // will cause DebugBreak
  53.  
  54.     AfxAbort();     // should not return (but otherwise DebugBreak)
  55.     return TRUE;
  56. }
  57.  
  58. void Trace(LPCTSTR lpszFormat, ...)
  59. {
  60.     va_list args;
  61.     va_start(args, lpszFormat);
  62.  
  63.     int nBuf;
  64.     TCHAR szBuffer[512];
  65.  
  66.     nBuf = _vstprintf(szBuffer, lpszFormat, args);
  67.     ASSERT(nBuf < (sizeof(szBuffer)/sizeof(szBuffer[0])));
  68.  
  69.     CString strMessage;
  70.  
  71.     if (AfxGetApp() != NULL)
  72.         strMessage = ((CString) (AfxGetApp()->m_pszExeName)) + _T(": ");
  73.     strMessage += szBuffer;
  74.     OutputDebugString(strMessage);
  75.  
  76.     va_end(args);
  77. }
  78.  
  79.  
  80. #endif // _PSEUDO_DEBUG
  81.