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 / mixapp / debug.c < prev    next >
C/C++ Source or Header  |  1997-10-05  |  6KB  |  275 lines

  1. //==========================================================================;
  2. //
  3. //  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  4. //  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  5. //  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  6. //  PURPOSE.
  7. //
  8. //  Copyright (C) 1992 - 1997 Microsoft Corporation.  All Rights Reserved.
  9. //
  10. //--------------------------------------------------------------------------;
  11. //
  12. //  debug.c
  13. //
  14. //  Description:
  15. //      This file contains code yanked from several places to provide debug
  16. //      support that works in win 16 and win 32.
  17. //
  18. //
  19. //==========================================================================;
  20.  
  21. #ifdef DEBUG
  22. #include <windows.h>
  23. #include <windowsx.h>
  24. #include <mmsystem.h>
  25. #include <stdarg.h>
  26. #include "debug.h"
  27.  
  28. BOOL    __gfDbgEnabled          = TRUE;         // master enable
  29. UINT    __guDbgLevel            = 0;            // current debug level
  30.  
  31.  
  32. //--------------------------------------------------------------------------;
  33. //
  34. //  void DbgVPrintF
  35. //
  36. //  Description:
  37. //
  38. //
  39. //  Arguments:
  40. //      LPSTR szFormat:
  41. //
  42. //      va_list va:
  43. //
  44. //  Return (void):
  45. //      No value is returned.
  46. //
  47. //--------------------------------------------------------------------------;
  48.  
  49. void FAR CDECL DbgVPrintF
  50. (
  51.     LPSTR                   szFormat,
  52.     va_list                 va
  53. )
  54. {
  55.     char                ach[DEBUG_MAX_LINE_LEN];
  56.     BOOL                fDebugBreak = FALSE;
  57.     BOOL                fPrefix     = TRUE;
  58.     BOOL                fCRLF       = TRUE;
  59.  
  60.     ach[0] = '\0';
  61.  
  62.     for (;;)
  63.     {
  64.         switch (*szFormat)
  65.         {
  66.             case '!':
  67.                 fDebugBreak = TRUE;
  68.                 szFormat++;
  69.                 continue;
  70.  
  71.             case '`':
  72.                 fPrefix = FALSE;
  73.                 szFormat++;
  74.                 continue;
  75.  
  76.             case '~':
  77.                 fCRLF = FALSE;
  78.                 szFormat++;
  79.                 continue;
  80.         }
  81.  
  82.         break;
  83.     }
  84.  
  85.     if (fDebugBreak)
  86.     {
  87.         ach[0] = '\007';
  88.         ach[1] = '\0';
  89.     }
  90.  
  91.     if (fPrefix)
  92.     {
  93.         lstrcatA(ach, DEBUG_MODULE_NAME ": ");
  94.     }
  95.  
  96.     wvsprintfA(ach + lstrlenA(ach), szFormat, va);
  97.  
  98.     if (fCRLF)
  99.     {
  100.         lstrcatA(ach, "\r\n");
  101.     }
  102.  
  103.     OutputDebugStringA(ach);
  104.  
  105.     if (fDebugBreak)
  106.     {
  107.         DebugBreak();
  108.     }
  109. } // DbgVPrintF()
  110.  
  111.  
  112. //--------------------------------------------------------------------------;
  113. //
  114. //  void dprintf
  115. //
  116. //  Description:
  117. //      dprintf() is called by the DPF() macro if DEBUG is defined at compile
  118. //      time. It is recommended that you only use the DPF() macro to call
  119. //      this function--so you don't have to put #ifdef DEBUG around all
  120. //      of your code.
  121. //
  122. //  Arguments:
  123. //      UINT uDbgLevel:
  124. //
  125. //      LPSTR szFormat:
  126. //
  127. //  Return (void):
  128. //      No value is returned.
  129. //
  130. //--------------------------------------------------------------------------;
  131.  
  132. void FAR CDECL dprintf
  133. (
  134.     UINT                    uDbgLevel,
  135.     LPSTR                   szFormat,
  136.     ...
  137. )
  138. {
  139.     va_list va;
  140.  
  141.     if (!__gfDbgEnabled || (__guDbgLevel < uDbgLevel))
  142.         return;
  143.  
  144.     va_start(va, szFormat);
  145.     DbgVPrintF(szFormat, va);
  146.     va_end(va);
  147. } // dprintf()
  148.  
  149.  
  150. //--------------------------------------------------------------------------;
  151. //
  152. //  BOOL DbgEnable
  153. //
  154. //  Description:
  155. //
  156. //
  157. //  Arguments:
  158. //      BOOL fEnable:
  159. //
  160. //  Return (BOOL):
  161. //      Returns the previous debugging state.
  162. //
  163. //--------------------------------------------------------------------------;
  164.  
  165. BOOL WINAPI DbgEnable
  166. (
  167.     BOOL                    fEnable
  168. )
  169. {
  170.     BOOL                fOldState;
  171.  
  172.     fOldState      = __gfDbgEnabled;
  173.     __gfDbgEnabled = fEnable;
  174.  
  175.     return (fOldState);
  176. } // DbgEnable()
  177.  
  178.  
  179. //--------------------------------------------------------------------------;
  180. //
  181. //  UINT DbgSetLevel
  182. //
  183. //  Description:
  184. //
  185. //
  186. //  Arguments:
  187. //      UINT uLevel:
  188. //
  189. //  Return (UINT):
  190. //      Returns the previous debugging level.
  191. //
  192. //--------------------------------------------------------------------------;
  193.  
  194. UINT WINAPI DbgSetLevel
  195. (
  196.     UINT                    uLevel
  197. )
  198. {
  199.     UINT                uOldLevel;
  200.  
  201.     uOldLevel    = __guDbgLevel;
  202.     __guDbgLevel = uLevel;
  203.  
  204.     return (uOldLevel);
  205. } // DbgSetLevel()
  206.  
  207.  
  208. //--------------------------------------------------------------------------;
  209. //
  210. //  UINT DbgGetLevel
  211. //
  212. //  Description:
  213. //
  214. //
  215. //  Arguments:
  216. //      None.
  217. //
  218. //  Return (UINT):
  219. //      Returns the current debugging level.
  220. //
  221. //--------------------------------------------------------------------------;
  222.  
  223. UINT WINAPI DbgGetLevel
  224. (
  225.     void
  226. )
  227. {
  228.     return (__guDbgLevel);
  229. } // DbgGetLevel()
  230.  
  231.  
  232. //--------------------------------------------------------------------------;
  233. //
  234. //  UINT DbgInitialize
  235. //
  236. //  Description:
  237. //
  238. //
  239. //  Arguments:
  240. //      BOOL fEnable:
  241. //
  242. //  Return (UINT):
  243. //      Returns the debugging level that was set.
  244. //
  245. //--------------------------------------------------------------------------;
  246.  
  247. UINT WINAPI DbgInitialize
  248. (
  249.     BOOL                    fEnable
  250. )
  251. {
  252.     UINT                uLevel;
  253.  
  254.     uLevel = GetProfileIntA(DEBUG_SECTION, DEBUG_MODULE_NAME, (UINT)-1);
  255.     if ((UINT)-1 == uLevel)
  256.     {
  257.         //
  258.         //  if the debug key is not present, then force debug output to
  259.         //  be disabled. this way running a debug version of a component
  260.         //  on a non-debugging machine will not generate output unless
  261.         //  the debug key exists.
  262.         //
  263.         uLevel  = 0;
  264.         fEnable = FALSE;
  265.     }
  266.  
  267.     DbgSetLevel(uLevel);
  268.     DbgEnable(fEnable);
  269.  
  270.     return (__guDbgLevel);
  271. } // DbgInitialize()
  272.  
  273. #endif // #ifdef DEBUG
  274.  
  275.