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 / mciapp / debug.c < prev    next >
C/C++ Source or Header  |  1997-10-05  |  5KB  |  242 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. //  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. //  History:
  19. //      11/23/92    created. 
  20. //
  21. //==========================================================================;
  22.  
  23. #ifdef DEBUG
  24.  
  25. #include <windows.h>
  26. #include <windowsx.h>
  27. #include <stdarg.h>
  28. #include "debug.h"
  29.  
  30. //
  31. //  since we don't UNICODE our debugging messages, use the ASCII entry
  32. //  points regardless of how we are compiled.
  33. //
  34. #ifdef WIN32
  35. #ifdef UNICODE
  36.     #include <wchar.h>
  37. #endif
  38. #else
  39.     #define lstrcatA            lstrcat
  40.     #define lstrlenA            lstrlen
  41.     #define wvsprintfA          wvsprintf
  42.     #define GetProfileIntA      GetProfileInt
  43.     #define OutputDebugStringA  OutputDebugString
  44. #endif
  45.  
  46. //
  47. //
  48. //
  49. BOOL    __gfDbgEnabled  = TRUE;     // master enable
  50. UINT    __guDbgLevel    = 0;        // current debug level
  51.  
  52. //--------------------------------------------------------------------------;
  53. //
  54. //  void DbgVPrintF(LPSTR szFmt, LPSTR va)
  55. //
  56. //  Description:
  57. //      
  58. //
  59. //  Arguments:
  60. //
  61. //  Return (void):
  62. //
  63. //
  64. //  History:
  65. //      11/28/92    created. 
  66. //
  67. //--------------------------------------------------------------------------;
  68.  
  69. void FAR CDECL DbgVPrintF(LPSTR szFmt, LPSTR va)
  70. {
  71.     char    ach[DEBUG_MAX_LINE_LEN];
  72.     BOOL    fDebugBreak = FALSE;
  73.     BOOL    fPrefix     = TRUE;
  74.     BOOL    fCRLF       = TRUE;
  75.  
  76.     ach[0] = '\0';
  77.  
  78.     for (;;)
  79.     {
  80.         switch(*szFmt)
  81.         {
  82.             case '!':
  83.                 fDebugBreak = TRUE;
  84.                 szFmt++;
  85.                 continue;
  86.  
  87.             case '`':
  88.                 fPrefix = FALSE;
  89.                 szFmt++;
  90.                 continue;
  91.  
  92.             case '~':
  93.                 fCRLF = FALSE;
  94.                 szFmt++;
  95.                 continue;
  96.         }
  97.  
  98.         break;
  99.     }
  100.  
  101.     if (fDebugBreak)
  102.     {
  103.         ach[0] = '\007';
  104.         ach[1] = '\0';
  105.     }
  106.  
  107.     if (fPrefix)
  108.         lstrcatA(ach, DEBUG_MODULE_NAME ": ");
  109.  
  110.     wvsprintfA(ach + lstrlenA(ach), szFmt, va);
  111.  
  112.     if (fCRLF)
  113.         lstrcatA(ach, "\r\n");
  114.  
  115.     OutputDebugStringA(ach);
  116.  
  117.     if (fDebugBreak)
  118.         DebugBreak();
  119. } // DbgVPrintF()
  120.  
  121.  
  122. //--------------------------------------------------------------------------;
  123. //
  124. //  void dprintf(UINT uDbgLevel, LPSTR szFmt, ...)
  125. //
  126. //  Description:
  127. //      dprintf() is called by the DPF macro if DEBUG is defined at compile
  128. //      time.
  129. //      
  130. //      The messages will be send to COM1: like any debug message. To
  131. //      enable debug output, add the following to WIN.INI :
  132. //
  133. //      [debug]
  134. //      ICSAMPLE=1
  135. //
  136. //  Arguments:
  137. //
  138. //  Return (void):
  139. //
  140. //
  141. //  History:
  142. //      11/23/92    created. 
  143. //
  144. //--------------------------------------------------------------------------;
  145.  
  146. void FAR CDECL dprintf(UINT uDbgLevel, LPSTR szFmt, ...)
  147. {
  148.     va_list va;
  149.  
  150.     if (!__gfDbgEnabled || (__guDbgLevel < uDbgLevel))
  151.         return;
  152.  
  153.     va_start(va, szFmt);
  154.     DbgVPrintF(szFmt, va);
  155.     va_end(va);
  156. } // dprintf()
  157.  
  158. //--------------------------------------------------------------------------;
  159. //
  160. //  BOOL DbgEnable(BOOL fEnable)
  161. //
  162. //  Description:
  163. //      
  164. //
  165. //  Arguments:
  166. //
  167. //  Return (BOOL):
  168. //
  169. //
  170. //  History:
  171. //      11/28/92    created. 
  172. //
  173. //--------------------------------------------------------------------------;
  174.  
  175. BOOL WINAPI DbgEnable(BOOL fEnable)
  176. {
  177.     BOOL    fOldState;
  178.  
  179.     fOldState      = __gfDbgEnabled;
  180.     __gfDbgEnabled = fEnable;
  181.  
  182.     return (fOldState);
  183. } // DbgEnable()
  184.  
  185.  
  186. //--------------------------------------------------------------------------;
  187. //
  188. //  UINT DbgSetLevel(UINT uLevel)
  189. //
  190. //  Description:
  191. //      
  192. //
  193. //  Arguments:
  194. //
  195. //  Return (UINT):
  196. //
  197. //
  198. //  History:
  199. //      11/24/92    created. 
  200. //
  201. //--------------------------------------------------------------------------;
  202.  
  203. UINT WINAPI DbgSetLevel(UINT uLevel)
  204. {
  205.     UINT    uOldLevel;
  206.  
  207.     uOldLevel    = __guDbgLevel;
  208.     __guDbgLevel = uLevel;
  209.  
  210.     return (uOldLevel);
  211. } // DbgSetLevel()
  212.  
  213.  
  214. //--------------------------------------------------------------------------;
  215. //
  216. //  UINT DbgInitialize(void)
  217. //
  218. //  Description:
  219. //      
  220. //
  221. //  Arguments:
  222. //
  223. //  Return (UINT):
  224. //
  225. //
  226. //  History:
  227. //      11/24/92    created. 
  228. //
  229. //--------------------------------------------------------------------------;
  230.  
  231. UINT WINAPI DbgInitialize(BOOL fEnable)
  232. {
  233.     DbgSetLevel(GetProfileIntA(DEBUG_SECTION, DEBUG_MODULE_NAME, 0));
  234.     DbgEnable(fEnable);
  235.  
  236.     return (__guDbgLevel);
  237. } // DbgInitialize()
  238.  
  239.  
  240. #endif // #ifdef DEBUG
  241.  
  242.