home *** CD-ROM | disk | FTP | other *** search
/ NEXT Generation 27 / NEXT27.iso / pc / demos / emperor / dx3.exe / SDK / SAMPLES / DSSHOW3D / DEBUG.C < prev    next >
C/C++ Source or Header  |  1996-08-28  |  9KB  |  414 lines

  1. /*==========================================================================
  2.  *
  3.  *  Copyright (C) 1995-1996 Microsoft Corporation. All Rights Reserved.
  4.  *
  5.  *  File:       debug.c
  6.  *  Content:    debug code
  7.  *
  8.  ***************************************************************************/
  9.  
  10. #include <windows.h>
  11. #include <windowsx.h>
  12. #include <stdarg.h>
  13. #include <dsound.h>
  14. #include "debug.h"
  15.  
  16. #if defined(DEBUG) || defined(_DEBUG)
  17.  
  18.  
  19. //
  20. //  since we don't UNICODE our debugging messages, use the ASCII entry
  21. //  points regardless of how we are compiled.
  22. //
  23. #ifdef _WIN32
  24.     #include <wchar.h>
  25. #else
  26.     #define lstrcatA            lstrcat
  27.     #define lstrlenA            lstrlen
  28.     #define GetProfileIntA      GetProfileInt
  29.     #define OutputDebugStringA  OutputDebugString
  30.     #define wsprintfA           wsprintf
  31.     #define MessageBoxA         MessageBox
  32. #endif
  33.  
  34. //
  35. //
  36. //
  37. BOOL    __gfDbgEnabled          = TRUE;         // master enable
  38. UINT    __guDbgLevel            = 0;            // current debug level
  39.  
  40.  
  41. //--------------------------------------------------------------------------;
  42. //  
  43. //  void DbgVPrintF
  44. //  
  45. //  Description:
  46. //  
  47. //  
  48. //  Arguments:
  49. //      LPSTR szFormat:
  50. //  
  51. //      va_list va:
  52. //  
  53. //  Return (void):
  54. //      No value is returned.
  55. //  
  56. //--------------------------------------------------------------------------;
  57.  
  58. void FAR CDECL DbgVPrintF
  59. (
  60.     LPSTR                   szFormat,
  61.     va_list                 va
  62. )
  63. {
  64.     char                ach[DEBUG_MAX_LINE_LEN];
  65.     BOOL                fDebugBreak = FALSE;
  66.     BOOL                fPrefix     = TRUE;
  67.     BOOL                fCRLF       = TRUE;
  68.  
  69.     ach[0] = '\0';
  70.  
  71.     for (;;)
  72.     {
  73.         switch (*szFormat)
  74.         {
  75.             case '!':
  76.                 fDebugBreak = TRUE;
  77.                 szFormat++;
  78.                 continue;
  79.  
  80.             case '`':
  81.                 fPrefix = FALSE;
  82.                 szFormat++;
  83.                 continue;
  84.  
  85.             case '~':
  86.                 fCRLF = FALSE;
  87.                 szFormat++;
  88.                 continue;
  89.         }
  90.  
  91.         break;
  92.     }
  93.  
  94.     if (fDebugBreak)
  95.     {
  96.         ach[0] = '\007';
  97.         ach[1] = '\0';
  98.     }
  99.  
  100.     if (fPrefix)
  101.     {
  102.         lstrcatA(ach, DEBUG_MODULE_NAME ": ");
  103.     }
  104.  
  105. #ifdef _WIN32
  106.     wvsprintfA(ach + lstrlenA(ach), szFormat, va);
  107. #else
  108.     wvsprintf(ach + lstrlenA(ach), szFormat, (LPSTR)va);
  109. #endif
  110.  
  111.     if (fCRLF)
  112.     {
  113.         lstrcatA(ach, "\r\n");
  114.     }
  115.  
  116.     OutputDebugStringA(ach);
  117.  
  118.     if (fDebugBreak)
  119.     {
  120.         DebugBreak();
  121.     }
  122. } // DbgVPrintF()
  123.  
  124.  
  125. //--------------------------------------------------------------------------;
  126. //  
  127. //  void dprintf
  128. //  
  129. //  Description:
  130. //      dprintf() is called by the DPF() macro if DEBUG is defined at compile
  131. //      time. It is recommended that you only use the DPF() macro to call
  132. //      this function--so you don't have to put #ifdef DEBUG around all
  133. //      of your code.
  134. //      
  135. //  Arguments:
  136. //      UINT uDbgLevel:
  137. //  
  138. //      LPSTR szFormat:
  139. //  
  140. //  Return (void):
  141. //      No value is returned.
  142. //
  143. //--------------------------------------------------------------------------;
  144.  
  145. void FAR CDECL dprintf
  146. (
  147.     UINT                    uDbgLevel,
  148.     LPSTR                   szFormat,
  149.     ...
  150. )
  151. {
  152.     va_list va;
  153.  
  154.     if (!__gfDbgEnabled || (__guDbgLevel < uDbgLevel))
  155.         return;
  156.  
  157.     va_start(va, szFormat);
  158.     DbgVPrintF(szFormat, va);
  159.     va_end(va);
  160. } // dprintf()
  161.  
  162.  
  163. //--------------------------------------------------------------------------;
  164. //  
  165. //  BOOL DbgEnable
  166. //  
  167. //  Description:
  168. //  
  169. //  
  170. //  Arguments:
  171. //      BOOL fEnable:
  172. //  
  173. //  Return (BOOL):
  174. //      Returns the previous debugging state.
  175. //  
  176. //--------------------------------------------------------------------------;
  177.  
  178. BOOL WINAPI DbgEnable
  179. (
  180.     BOOL                    fEnable
  181. )
  182. {
  183.     BOOL                fOldState;
  184.  
  185.     fOldState      = __gfDbgEnabled;
  186.     __gfDbgEnabled = fEnable;
  187.  
  188.     return (fOldState);
  189. } // DbgEnable()
  190.  
  191.  
  192. //--------------------------------------------------------------------------;
  193. //  
  194. //  UINT DbgSetLevel
  195. //  
  196. //  Description:
  197. //  
  198. //  
  199. //  Arguments:
  200. //      UINT uLevel:
  201. //  
  202. //  Return (UINT):
  203. //      Returns the previous debugging level.
  204. //  
  205. //--------------------------------------------------------------------------;
  206.  
  207. UINT WINAPI DbgSetLevel
  208. (
  209.     UINT                    uLevel
  210. )
  211. {
  212.     UINT                uOldLevel;
  213.  
  214.     uOldLevel    = __guDbgLevel;
  215.     __guDbgLevel = uLevel;
  216.  
  217.     return (uOldLevel);
  218. } // DbgSetLevel()
  219.  
  220.  
  221. //--------------------------------------------------------------------------;
  222. //  
  223. //  UINT DbgGetLevel
  224. //  
  225. //  Description:
  226. //  
  227. //  
  228. //  Arguments:
  229. //      None.
  230. //  
  231. //  Return (UINT):
  232. //      Returns the current debugging level.
  233. //  
  234. //--------------------------------------------------------------------------;
  235.  
  236. UINT WINAPI DbgGetLevel
  237. (
  238.     void
  239. )
  240. {
  241.     return (__guDbgLevel);
  242. } // DbgGetLevel()
  243.  
  244.  
  245. //--------------------------------------------------------------------------;
  246. //  
  247. //  UINT DbgInitialize
  248. //  
  249. //  Description:
  250. //  
  251. //  
  252. //  Arguments:
  253. //      BOOL fEnable:
  254. //  
  255. //  Return (UINT):
  256. //      Returns the debugging level that was set.
  257. //  
  258. //--------------------------------------------------------------------------;
  259.  
  260. UINT WINAPI DbgInitialize
  261. (
  262.     BOOL                    fEnable
  263. )
  264. {
  265.     UINT                uLevel;
  266.  
  267.     uLevel = GetProfileIntA(DEBUG_SECTION, DEBUG_MODULE_NAME, (UINT)-1);
  268.     if ((UINT)-1 == uLevel)
  269.     {
  270.         //
  271.         //  if the debug key is not present, then force debug output to
  272.         //  be disabled. this way running a debug version of a component
  273.         //  on a non-debugging machine will not generate output unless
  274.         //  the debug key exists.
  275.         //
  276.         uLevel  = 0;
  277.         fEnable = FALSE;
  278.     }
  279.  
  280.     DbgSetLevel(uLevel);
  281.     DbgEnable(fEnable);
  282.  
  283.     return (__guDbgLevel);
  284. } // DbgInitialize()
  285.  
  286.  
  287. //--------------------------------------------------------------------------;
  288. //  
  289. //  void _Assert
  290. //  
  291. //  Description:
  292. //      This routine is called if the ASSERT macro (defined in debug.h)
  293. //      tests and expression that evaluates to FALSE.  This routine 
  294. //      displays an "assertion failed" message box allowing the user to
  295. //      abort the program, enter the debugger (the "retry" button), or
  296. //      ignore the assertion and continue executing.  The message box
  297. //      displays the file name and line number of the _Assert() call.
  298. //  
  299. //  Arguments:
  300. //      char *  szFile: Filename where assertion occurred.
  301. //      int     iLine:  Line number of assertion.
  302. //  
  303. //--------------------------------------------------------------------------;
  304.  
  305. #ifndef _WIN32
  306. #pragma warning(disable:4704)
  307. #endif
  308.  
  309. void WINAPI _Assert
  310. (
  311.     char *  szFile,
  312.     int     iLine
  313. )
  314. {
  315.     static char     ach[300];       // debug output (avoid stack overflow)
  316.     int             id;
  317. #ifndef _WIN32
  318.     int             iExitCode;
  319. #endif
  320.  
  321.     wsprintfA(ach, "Assertion failed in file %s, line %d.  [Press RETRY to debug.]", (LPSTR)szFile, iLine);
  322.  
  323.     id = MessageBoxA(NULL, ach, "Assertion Failed",
  324.             MB_SYSTEMMODAL | MB_ICONHAND | MB_ABORTRETRYIGNORE );
  325.  
  326.     switch (id)
  327.     {
  328.  
  329.     case IDABORT:               // Kill the application.
  330. #ifndef _WIN32
  331.         iExitCode = 0;
  332.         _asm
  333.         {
  334.             mov ah, 4Ch
  335.             mov al, BYTE PTR iExitCode
  336.             int     21h
  337.         }
  338. #else
  339.         FatalAppExit(0, TEXT("Good Bye"));
  340. #endif // WIN16
  341.         break;
  342.  
  343.     case IDRETRY:               // Break into the debugger.
  344.         DebugBreak();
  345.         break;
  346.  
  347.     case IDIGNORE:              // Ignore assertion, continue executing.
  348.         break;
  349.     }
  350. } // _Assert
  351.  
  352. #ifndef _WIN32
  353. #pragma warning(default:4704)
  354. #endif
  355.  
  356. #endif // #if defined(DEBUG) || defined(_DEBUG)
  357.  
  358. // Silly little function gives meaningful error messages from HRESULT's
  359. LPSTR TranslateDSError( HRESULT hr )
  360.     {
  361.     switch( hr )
  362.     {
  363.     case DSERR_ALLOCATED:
  364.         return "DSERR_ALLOCATED";
  365.  
  366.     case DSERR_CONTROLUNAVAIL:
  367.         return "DSERR_CONTROLUNAVAIL";
  368.  
  369.     case DSERR_INVALIDPARAM:
  370.         return "DSERR_INVALIDPARAM";
  371.  
  372.     case DSERR_INVALIDCALL:
  373.         return "DSERR_INVALIDCALL";
  374.  
  375.     case DSERR_GENERIC:
  376.         return "DSERR_GENERIC";
  377.  
  378.     case DSERR_PRIOLEVELNEEDED:
  379.         return "DSERR_PRIOLEVELNEEDED";
  380.  
  381.     case DSERR_OUTOFMEMORY:
  382.         return "DSERR_OUTOFMEMORY";
  383.  
  384.     case DSERR_BADFORMAT:
  385.         return "DSERR_BADFORMAT";
  386.  
  387.     case DSERR_UNSUPPORTED:
  388.         return "DSERR_UNSUPPORTED";
  389.  
  390.     case DSERR_NODRIVER:
  391.         return "DSERR_NODRIVER";
  392.  
  393.     case DSERR_ALREADYINITIALIZED:
  394.         return "DSERR_ALREADYINITIALIZED";
  395.  
  396.     case DSERR_NOAGGREGATION:
  397.         return "DSERR_NOAGGREGATION";
  398.  
  399.     case DSERR_BUFFERLOST:
  400.         return "DSERR_BUFFERLOST";
  401.  
  402.     case DSERR_OTHERAPPHASPRIO:
  403.         return "DSERR_OTHERAPPHASPRIO";
  404.  
  405.     case DSERR_UNINITIALIZED:
  406.         return "DSERR_UNINITIALIZED";
  407.  
  408.     default:
  409.         return "Unknown HRESULT";
  410.     }
  411.     }
  412.  
  413.  
  414.