home *** CD-ROM | disk | FTP | other *** search
/ Game Audio Programming / GameAudioProgramming.iso / Extras / Sensaura / SDK1.0 / data1.cab / Example_Files / ZoomFX / Debug.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-13  |  4.7 KB  |  187 lines

  1. /*
  2.     Company:            Sensaura Ltd
  3.     Copyright:          (C) 2000
  4.  
  5.     File Name:            Debug.c
  6.     File Description:    Source file for debug support functions
  7.     Author:                Adam Philp
  8.     Last Update:        04-JAN-00
  9.  
  10.     Target Compiler:    Microsoft Visual C++ Version 5.0
  11. */
  12.  
  13. #ifdef _DEBUG                            /* File only compiles to code if this is a debug build    */
  14.  
  15. /*
  16.     Included files
  17. */
  18.  
  19. #include <windows.h>
  20.  
  21. #include <stdio.h>
  22. #include <string.h>
  23.  
  24. #include "debug.h"
  25.  
  26. /*
  27.     Local definitions
  28. */
  29.  
  30. #define DEBUG_BUFFER_SIZE    256
  31.  
  32. #define DEBUG_OK               0
  33. #define DEBUG_ERR_WRITE        -1
  34.  
  35. /*
  36.     Local functions
  37. */
  38.  
  39. void DebugCreateMessage(char* lpszMessage, const char* lpcszFormat, va_list argPtr);
  40. int    DebugPrintf(const char* lpcszFormat, ...);
  41.  
  42. /*
  43.     Local variables
  44. */
  45.  
  46. #ifdef DEBUGLEVEL                        /* We have a defined debug threshold                    */
  47. int    g_DebugLevel = DEBUGLEVEL;            /* Only TRACE messages with a level greater or equal to    */                
  48. int    g_DebugOldLevel = DEBUGLEVEL;        /* g_DebugLevel will be output                            */
  49. #else                                    /* No defined threshold                                    */                                    
  50. int    g_DebugLevel = 0;                    /* Default is to output all TRACE messages                */
  51. int    g_DebugOldLevel = 0;
  52. #endif
  53.  
  54. const char*    g_pFileName = NULL;            /* Name of the source file which generated the TRACE    */
  55. int g_nFileLine = 0;                    /* Number of the line in the source file                */
  56. int g_nDebugMsgs = 0;
  57.  
  58. /*
  59.     Module code
  60. */
  61.  
  62. /*
  63.     Function:        DebugTraceDisable()
  64.     Description:    Disable TRACE message output. TRACE messages with a positive debug level will 
  65.                     not appear as either debug strings or message boxes.  PTRACE and TRACEERROR
  66.                     messages are not affected.
  67. */
  68.  
  69. void DebugTraceDisable()                
  70. {        
  71.     g_DebugLevel = -1;                    
  72. }
  73.  
  74. /*
  75.     Function:        DebugTraceEnable()
  76.     Description:    Enable TRACE message output. Restore the debug threshold operating before
  77.                     DebugTraceDisable() was called.
  78. */
  79.  
  80. extern void DebugTraceEnable()
  81. {
  82.     g_DebugLevel = g_DebugOldLevel;
  83. }
  84.  
  85. /*
  86.     Function:        DebugTracePrepare()
  87.     Description:    Called by all TRACE macros before generating a TRACE message.  Format and store
  88.                     the file name and line number which generated the TRACE message.
  89. */
  90.  
  91. void DebugTracePrepare(const char* lpcszFileName, int NumLine)
  92. {
  93.                                         /* Remove directory information from the file name        */
  94.     g_pFileName = strrchr(lpcszFileName, '\\');
  95.     if(g_pFileName == NULL)
  96.         g_pFileName = lpcszFileName;
  97.     else
  98.         ++g_pFileName;
  99.  
  100.     g_nFileLine = NumLine;                /* Save the line number                                    */
  101. }
  102.  
  103. /*
  104.     Function:        DebugTrace()
  105.     Description:    Generate a TRACE message if its debug level is high enough
  106. */
  107.  
  108. void DebugTrace(int DebugLevel, const char* lpcszFormat, ...)
  109. {
  110.     va_list argPtr;
  111.     char    achMessage[DEBUG_BUFFER_SIZE];
  112.  
  113.     if((DebugLevel < g_DebugLevel) && DebugLevel >= 0)
  114.         return;                            /* Don't output message if it's below the threshold        */
  115.                                         /* NOTE: Debug levels < 0 are ALWAYS allowed through    */
  116.        va_start(argPtr, lpcszFormat);        
  117.     DebugCreateMessage(achMessage, lpcszFormat, argPtr);
  118.     DebugPrintf("%d\t\t%-12s #%d:\t%s\n", ++g_nDebugMsgs, g_pFileName, g_nFileLine, achMessage);
  119. }
  120.  
  121. /*
  122.     Function:        DebugOutput()
  123.     Description:    Generate a TRACE message 
  124. */
  125.  
  126. void DebugOutput(const char* lpcszFormat, ...)
  127. {
  128.      va_list argPtr;
  129.     char    achMessage[DEBUG_BUFFER_SIZE];
  130.  
  131.        va_start(argPtr, lpcszFormat);
  132.     DebugCreateMessage(achMessage, lpcszFormat, argPtr);
  133.     DebugPrintf("%d\t\t%-12s #%d:\t%s\n", ++g_nDebugMsgs, g_pFileName, g_nFileLine, achMessage);
  134. }
  135.  
  136. /*
  137.     Function:        DebugError()
  138.     Description:    Generate a TRACE error message 
  139. */
  140.  
  141. void DebugError(const char* lpcszFormat, ...)
  142. {
  143.     va_list argPtr;
  144.     char    achMessage[DEBUG_BUFFER_SIZE];
  145.  
  146.        va_start(argPtr, lpcszFormat);
  147.     DebugCreateMessage(achMessage, lpcszFormat, argPtr);
  148.     DebugPrintf("%d\t\t%-12s #%d:\tERROR:- %s\n", ++g_nDebugMsgs, g_pFileName, g_nFileLine, achMessage);
  149. }
  150.  
  151. /*
  152.     Function:        DebugCreateMessage()
  153.     Description:    Convert the TRACE message parameters into a single message string
  154. */
  155.  
  156. void DebugCreateMessage(char* lpszMessage, const char* lpcszFormat, va_list argPtr)
  157. {
  158.     int    len;
  159.  
  160.     len = vsprintf(lpszMessage, lpcszFormat, argPtr);
  161.       while(lpszMessage[--len] == '\n')    /* Remove trailing carriage returns                        */
  162.         lpszMessage[len] = '\0';
  163. }
  164.  
  165. /*
  166.     Function:        DebugPrintf()
  167.     Description:    Output a string to the application's debugger
  168. */
  169.  
  170. int DebugPrintf(const char* lpcszFormat, ...)
  171. {
  172.     int        RetVal;
  173.     va_list argPtr;
  174.     char    achDebugString[DEBUG_BUFFER_SIZE];
  175.  
  176.     va_start(argPtr, lpcszFormat);        // Format the string
  177.     RetVal = vsprintf(achDebugString, lpcszFormat, argPtr);
  178.     if(RetVal == EOF)                    // Unable to format the string
  179.         return DEBUG_ERR_WRITE;
  180.  
  181.     OutputDebugString(achDebugString);
  182.     return RetVal;
  183. }
  184.  
  185. #endif             // _DEBUG
  186.  
  187.