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 / acmapp / debug.c < prev    next >
C/C++ Source or Header  |  1997-10-05  |  6KB  |  293 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.  
  23. #include <windows.h>
  24. #include <windowsx.h>
  25. #include <stdarg.h>
  26. #include "debug.h"
  27.  
  28.  
  29. //
  30. //  since we don't UNICODE our debugging messages, use the ASCII entry
  31. //  points regardless of how we are compiled.
  32. //
  33. #ifndef WIN32
  34.     #define lstrcatA            lstrcat
  35.     #define lstrlenA            lstrlen
  36.     #define GetProfileIntA      GetProfileInt
  37.     #define OutputDebugStringA  OutputDebugString
  38. #endif
  39.  
  40. //
  41. //
  42. //
  43. BOOL    __gfDbgEnabled          = TRUE;         // master enable
  44. UINT    __guDbgLevel            = 0;            // current debug level
  45.  
  46.  
  47. //--------------------------------------------------------------------------;
  48. //  
  49. //  void DbgVPrintF
  50. //  
  51. //  Description:
  52. //  
  53. //  
  54. //  Arguments:
  55. //      LPSTR szFormat:
  56. //  
  57. //      va_list va:
  58. //  
  59. //  Return (void):
  60. //      No value is returned.
  61. //  
  62. //--------------------------------------------------------------------------;
  63.  
  64. void FAR CDECL DbgVPrintF
  65. (
  66.     LPSTR                   szFormat,
  67.     va_list                 va
  68. )
  69. {
  70.     char                ach[DEBUG_MAX_LINE_LEN];
  71.     BOOL                fDebugBreak = FALSE;
  72.     BOOL                fPrefix     = TRUE;
  73.     BOOL                fCRLF       = TRUE;
  74.  
  75.     ach[0] = '\0';
  76.  
  77.     for (;;)
  78.     {
  79.         switch (*szFormat)
  80.         {
  81.             case '!':
  82.                 fDebugBreak = TRUE;
  83.                 szFormat++;
  84.                 continue;
  85.  
  86.             case '`':
  87.                 fPrefix = FALSE;
  88.                 szFormat++;
  89.                 continue;
  90.  
  91.             case '~':
  92.                 fCRLF = FALSE;
  93.                 szFormat++;
  94.                 continue;
  95.         }
  96.  
  97.         break;
  98.     }
  99.  
  100.     if (fDebugBreak)
  101.     {
  102.         ach[0] = '\007';
  103.         ach[1] = '\0';
  104.     }
  105.  
  106.     if (fPrefix)
  107.     {
  108.         lstrcatA(ach, DEBUG_MODULE_NAME ": ");
  109.     }
  110.  
  111. #ifdef WIN32
  112.     wvsprintfA(ach + lstrlenA(ach), szFormat, va);
  113. #else
  114.     wvsprintf(ach + lstrlenA(ach), szFormat, (LPSTR)va);
  115. #endif
  116.  
  117.     if (fCRLF)
  118.     {
  119.         lstrcatA(ach, "\r\n");
  120.     }
  121.  
  122.     OutputDebugStringA(ach);
  123.  
  124.     if (fDebugBreak)
  125.     {
  126.         DebugBreak();
  127.     }
  128. } // DbgVPrintF()
  129.  
  130.  
  131. //--------------------------------------------------------------------------;
  132. //  
  133. //  void dprintf
  134. //  
  135. //  Description:
  136. //      dprintf() is called by the DPF() macro if DEBUG is defined at compile
  137. //      time. It is recommended that you only use the DPF() macro to call
  138. //      this function--so you don't have to put #ifdef DEBUG around all
  139. //      of your code.
  140. //      
  141. //  Arguments:
  142. //      UINT uDbgLevel:
  143. //  
  144. //      LPSTR szFormat:
  145. //  
  146. //  Return (void):
  147. //      No value is returned.
  148. //
  149. //--------------------------------------------------------------------------;
  150.  
  151. void FAR CDECL dprintf
  152. (
  153.     UINT                    uDbgLevel,
  154.     LPSTR                   szFormat,
  155.     ...
  156. )
  157. {
  158.     va_list va;
  159.  
  160.     if (!__gfDbgEnabled || (__guDbgLevel < uDbgLevel))
  161.         return;
  162.  
  163.     va_start(va, szFormat);
  164.     DbgVPrintF(szFormat, va);
  165.     va_end(va);
  166. } // dprintf()
  167.  
  168.  
  169. //--------------------------------------------------------------------------;
  170. //  
  171. //  BOOL DbgEnable
  172. //  
  173. //  Description:
  174. //  
  175. //  
  176. //  Arguments:
  177. //      BOOL fEnable:
  178. //  
  179. //  Return (BOOL):
  180. //      Returns the previous debugging state.
  181. //  
  182. //--------------------------------------------------------------------------;
  183.  
  184. BOOL WINAPI DbgEnable
  185. (
  186.     BOOL                    fEnable
  187. )
  188. {
  189.     BOOL                fOldState;
  190.  
  191.     fOldState      = __gfDbgEnabled;
  192.     __gfDbgEnabled = fEnable;
  193.  
  194.     return (fOldState);
  195. } // DbgEnable()
  196.  
  197.  
  198. //--------------------------------------------------------------------------;
  199. //  
  200. //  UINT DbgSetLevel
  201. //  
  202. //  Description:
  203. //  
  204. //  
  205. //  Arguments:
  206. //      UINT uLevel:
  207. //  
  208. //  Return (UINT):
  209. //      Returns the previous debugging level.
  210. //  
  211. //--------------------------------------------------------------------------;
  212.  
  213. UINT WINAPI DbgSetLevel
  214. (
  215.     UINT                    uLevel
  216. )
  217. {
  218.     UINT                uOldLevel;
  219.  
  220.     uOldLevel    = __guDbgLevel;
  221.     __guDbgLevel = uLevel;
  222.  
  223.     return (uOldLevel);
  224. } // DbgSetLevel()
  225.  
  226.  
  227. //--------------------------------------------------------------------------;
  228. //  
  229. //  UINT DbgGetLevel
  230. //  
  231. //  Description:
  232. //  
  233. //  
  234. //  Arguments:
  235. //      None.
  236. //  
  237. //  Return (UINT):
  238. //      Returns the current debugging level.
  239. //  
  240. //--------------------------------------------------------------------------;
  241.  
  242. UINT WINAPI DbgGetLevel
  243. (
  244.     void
  245. )
  246. {
  247.     return (__guDbgLevel);
  248. } // DbgGetLevel()
  249.  
  250.  
  251. //--------------------------------------------------------------------------;
  252. //  
  253. //  UINT DbgInitialize
  254. //  
  255. //  Description:
  256. //  
  257. //  
  258. //  Arguments:
  259. //      BOOL fEnable:
  260. //  
  261. //  Return (UINT):
  262. //      Returns the debugging level that was set.
  263. //  
  264. //--------------------------------------------------------------------------;
  265.  
  266. UINT WINAPI DbgInitialize
  267. (
  268.     BOOL                    fEnable
  269. )
  270. {
  271.     UINT                uLevel;
  272.  
  273.     uLevel = GetProfileIntA(DEBUG_SECTION, DEBUG_MODULE_NAME, (UINT)-1);
  274.     if ((UINT)-1 == uLevel)
  275.     {
  276.         //
  277.         //  if the debug key is not present, then force debug output to
  278.         //  be disabled. this way running a debug version of a component
  279.         //  on a non-debugging machine will not generate output unless
  280.         //  the debug key exists.
  281.         //
  282.         uLevel  = 0;
  283.         fEnable = FALSE;
  284.     }
  285.  
  286.     DbgSetLevel(uLevel);
  287.     DbgEnable(fEnable);
  288.  
  289.     return (__guDbgLevel);
  290. } // DbgInitialize()
  291.  
  292. #endif // #ifdef DEBUG
  293.