home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / graphics / audio / midiplyr / debug.c next >
C/C++ Source or Header  |  1997-10-05  |  7KB  |  249 lines

  1. /*****************************************************************************
  2. *
  3. *  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  4. *  ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
  5. *  TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR
  6. *  A PARTICULAR PURPOSE.
  7. *
  8. *  Copyright (C) 1993 - 1997 Microsoft Corporation. All Rights Reserved.
  9. *
  10. ******************************************************************************
  11. *
  12. * Debug.C
  13. *
  14. * Debug output routines
  15. *
  16. *****************************************************************************/
  17.  
  18. #ifdef   DEBUG
  19. #include <windows.h>
  20. #include <mmsystem.h>  
  21. #include <stdarg.h>
  22.  
  23. #include "global.h"
  24. #include "debug.h"
  25.  
  26. PRIVATE VOID FAR CDECL DbgVPrintF(LPSTR szFmt, LPSTR va);
  27.  
  28. /*
  29. **  Since we don't UNICODE our debugging messages, use the ASCII entry
  30. **  points regardless of how we are compiled.
  31. */
  32. #define wvsprintfA          wvsprintf
  33. #define GetProfileIntA      GetProfileInt
  34. #define OutputDebugStringA  OutputDebugStr
  35.  
  36. #define lstrcatA            lstrcat
  37. #define lstrlenA            lstrlen
  38.  
  39. BOOL                        __gfDbgEnabled  = TRUE;
  40. UINT                        __guDbgLevel    = 0;   
  41.  
  42. WORD                        wDebugLevel     = 0;
  43.  
  44. /*****************************************************************************
  45. *
  46. * WinAssert
  47. *
  48. * Cause a debug out on an assert; breaking to the debugger if installed.
  49. *
  50. * lpstrExp                  - Assert expression string
  51. * lpstrFile                 - File of assert
  52. * dwLine                    - Line number of assert
  53. *
  54. * Handle WM_CREATE message to main application window.
  55. *
  56. * HWND hWnd                 - Window handle
  57. * CREATESTRUCT FAR* lpCreateStruct
  58. *                           - Pointer to creation parameters for the window.
  59. *
  60. *****************************************************************************/
  61. VOID WINAPI WinAssert(
  62.     LPSTR                   lpstrExp,
  63.     LPSTR                   lpstrFile,
  64.     DWORD                   dwLine)
  65. {
  66.     static char szWork[256];
  67.     static char BCODE szFormat[] =
  68.         "!Assert: %s#%lu [%s]";
  69.  
  70.     dprintf(0, (LPSTR)szFormat, (LPSTR)lpstrFile, dwLine, (LPSTR)lpstrExp);
  71. }
  72.  
  73. /*****************************************************************************
  74. *
  75. * DbgVPrintF
  76. *
  77. * Format and print a debug output string
  78. *
  79. * szFmt                     - Format string
  80. * va                        - printf style argument list
  81. *
  82. * The following characters have special meanings when they are
  83. * the first in the string:
  84. *
  85. *   !   Causes a break into the debugger after the output has been printed
  86. *   `   Causes no module prefix to be printed
  87. *   ~   Causes no CR/LF pair to follow the string
  88. *
  89. *****************************************************************************/
  90. VOID FAR CDECL DbgVPrintF(
  91.     LPSTR                   szFmt, 
  92.     LPSTR                   va)
  93. {
  94.     char                    ach[DEBUG_MAX_LINE_LEN];
  95.     BOOL                    fDebugBreak = FALSE;
  96.     BOOL                    fPrefix     = TRUE;
  97.     BOOL                    fCRLF       = TRUE;
  98.  
  99.     ach[0] = '\0';
  100.  
  101.     for (;;)
  102.     {
  103.         switch(*szFmt)
  104.         {
  105.             case '!':
  106.                 fDebugBreak = TRUE;
  107.                 szFmt++;
  108.                 continue;
  109.  
  110.             case '`':
  111.                 fPrefix = FALSE;
  112.                 szFmt++;
  113.                 continue;
  114.  
  115.             case '~':
  116.                 fCRLF = FALSE;
  117.                 szFmt++;
  118.                 continue;
  119.         }
  120.  
  121.         break;
  122.     }
  123.  
  124.     if (fDebugBreak)
  125.     {
  126.         ach[0] = '\007';
  127.         ach[1] = '\0';
  128.     }
  129.  
  130.     if (fPrefix)
  131.         lstrcatA(ach, DEBUG_MODULE_NAME ": ");
  132.  
  133.     wvsprintfA(ach + lstrlenA(ach), szFmt, (LPSTR)va);
  134.  
  135.     if (fCRLF)
  136.         lstrcatA(ach, "\r\n");
  137.  
  138.     OutputDebugStringA(ach);
  139.  
  140.     if (fDebugBreak)
  141.         DebugBreak();
  142. }
  143.  
  144. /*****************************************************************************
  145. *
  146. * dprintf
  147. *
  148. * User-level function to print a debug string
  149. *
  150. * uDbgLevel                 - Debug level of this message
  151. * szFmt                     - Format string for this message
  152. * ...                       - printf style arguments
  153. *
  154. * The message will be printed only if debugging is enabled and the messages's
  155. * debug level is less than or equal to the current debug level.
  156. *
  157. * This function is called by the DPF() macro in Debug.H.
  158. *
  159. *****************************************************************************/
  160. void FAR CDECL dprintf(
  161.     UINT                    uDbgLevel, 
  162.     LPSTR                   szFmt, 
  163.     ...)
  164. {
  165.     va_list                 va;
  166.  
  167.     if (!__gfDbgEnabled || (__guDbgLevel < uDbgLevel))
  168.         return;
  169.  
  170.     va_start(va, szFmt);
  171.     DbgVPrintF(szFmt, (LPSTR)va);
  172.     va_end(va);
  173. }
  174.  
  175.  
  176. /*****************************************************************************
  177. *
  178. * DbgEnable
  179. *
  180. * Enable or disable debug output
  181. *
  182. * fEnable                   - Enable or disable output
  183. *
  184. * Returns the old state.
  185. *
  186. *****************************************************************************/
  187. BOOL WINAPI DbgEnable(
  188.     BOOL                    fEnable)
  189. {
  190.     BOOL                    fOldState;
  191.  
  192.     fOldState      = __gfDbgEnabled;
  193.     __gfDbgEnabled = fEnable;
  194.  
  195.     return (fOldState);
  196. }
  197.  
  198. /*****************************************************************************
  199. *
  200. * DbgSetLevel
  201. *
  202. * Set the current level for debug output
  203. *
  204. * uLevel                    - New level for debug output
  205. *
  206. * Returns the old level.
  207. *
  208. *****************************************************************************/
  209. UINT WINAPI DbgSetLevel(
  210.     UINT                    uLevel)
  211. {
  212.     UINT                    uOldLevel;
  213.  
  214.     uOldLevel    = __guDbgLevel;
  215.     __guDbgLevel = wDebugLevel = uLevel;
  216.  
  217.     return (uOldLevel);
  218. }
  219.  
  220.  
  221. /*****************************************************************************
  222. *
  223. * DbgInitialize
  224. *
  225. * Get debug output settings from WIN.INI for this module
  226. *
  227. * fEnable                   - Enable or disable debug output
  228. *
  229. * Returns the current debug level
  230. *
  231. * The debug level will be read from the [debug] section of WIN.INI,
  232. * from an entry with this module's name as #define'd in Debug.H.
  233. *
  234. * WIN.INI:
  235. *  [Debug]
  236. *  Module=3
  237. *
  238. *****************************************************************************/
  239. UINT WINAPI DbgInitialize(BOOL fEnable)
  240. {
  241.     DbgSetLevel(GetProfileIntA(DEBUG_SECTION, DEBUG_MODULE_NAME, 0));
  242.     DbgEnable(fEnable);
  243.  
  244.     return (__guDbgLevel);
  245.  
  246.  
  247. #endif
  248.