home *** CD-ROM | disk | FTP | other *** search
- /*
- Company: Sensaura Ltd
- Copyright: (C) 2000
-
- File Name: Debug.c
- File Description: Source file for debug support functions
- Author: Adam Philp
- Last Update: 04-JAN-00
-
- Target Compiler: Microsoft Visual C++ Version 5.0
- */
-
- #ifdef _DEBUG /* File only compiles to code if this is a debug build */
-
- /*
- Included files
- */
-
- #include <windows.h>
-
- #include <stdio.h>
- #include <string.h>
-
- #include "debug.h"
-
- /*
- Local definitions
- */
-
- #define DEBUG_BUFFER_SIZE 256
-
- #define DEBUG_OK 0
- #define DEBUG_ERR_WRITE -1
-
- /*
- Local functions
- */
-
- void DebugCreateMessage(char* lpszMessage, const char* lpcszFormat, va_list argPtr);
- int DebugPrintf(const char* lpcszFormat, ...);
-
- /*
- Local variables
- */
-
- #ifdef DEBUGLEVEL /* We have a defined debug threshold */
- int g_DebugLevel = DEBUGLEVEL; /* Only TRACE messages with a level greater or equal to */
- int g_DebugOldLevel = DEBUGLEVEL; /* g_DebugLevel will be output */
- #else /* No defined threshold */
- int g_DebugLevel = 0; /* Default is to output all TRACE messages */
- int g_DebugOldLevel = 0;
- #endif
-
- const char* g_pFileName = NULL; /* Name of the source file which generated the TRACE */
- int g_nFileLine = 0; /* Number of the line in the source file */
- int g_nDebugMsgs = 0;
-
- /*
- Module code
- */
-
- /*
- Function: DebugTraceDisable()
- Description: Disable TRACE message output. TRACE messages with a positive debug level will
- not appear as either debug strings or message boxes. PTRACE and TRACEERROR
- messages are not affected.
- */
-
- void DebugTraceDisable()
- {
- g_DebugLevel = -1;
- }
-
- /*
- Function: DebugTraceEnable()
- Description: Enable TRACE message output. Restore the debug threshold operating before
- DebugTraceDisable() was called.
- */
-
- extern void DebugTraceEnable()
- {
- g_DebugLevel = g_DebugOldLevel;
- }
-
- /*
- Function: DebugTracePrepare()
- Description: Called by all TRACE macros before generating a TRACE message. Format and store
- the file name and line number which generated the TRACE message.
- */
-
- void DebugTracePrepare(const char* lpcszFileName, int NumLine)
- {
- /* Remove directory information from the file name */
- g_pFileName = strrchr(lpcszFileName, '\\');
- if(g_pFileName == NULL)
- g_pFileName = lpcszFileName;
- else
- ++g_pFileName;
-
- g_nFileLine = NumLine; /* Save the line number */
- }
-
- /*
- Function: DebugTrace()
- Description: Generate a TRACE message if its debug level is high enough
- */
-
- void DebugTrace(int DebugLevel, const char* lpcszFormat, ...)
- {
- va_list argPtr;
- char achMessage[DEBUG_BUFFER_SIZE];
-
- if((DebugLevel < g_DebugLevel) && DebugLevel >= 0)
- return; /* Don't output message if it's below the threshold */
- /* NOTE: Debug levels < 0 are ALWAYS allowed through */
- va_start(argPtr, lpcszFormat);
- DebugCreateMessage(achMessage, lpcszFormat, argPtr);
- DebugPrintf("%d\t\t%-12s #%d:\t%s\n", ++g_nDebugMsgs, g_pFileName, g_nFileLine, achMessage);
- }
-
- /*
- Function: DebugOutput()
- Description: Generate a TRACE message
- */
-
- void DebugOutput(const char* lpcszFormat, ...)
- {
- va_list argPtr;
- char achMessage[DEBUG_BUFFER_SIZE];
-
- va_start(argPtr, lpcszFormat);
- DebugCreateMessage(achMessage, lpcszFormat, argPtr);
- DebugPrintf("%d\t\t%-12s #%d:\t%s\n", ++g_nDebugMsgs, g_pFileName, g_nFileLine, achMessage);
- }
-
- /*
- Function: DebugError()
- Description: Generate a TRACE error message
- */
-
- void DebugError(const char* lpcszFormat, ...)
- {
- va_list argPtr;
- char achMessage[DEBUG_BUFFER_SIZE];
-
- va_start(argPtr, lpcszFormat);
- DebugCreateMessage(achMessage, lpcszFormat, argPtr);
- DebugPrintf("%d\t\t%-12s #%d:\tERROR:- %s\n", ++g_nDebugMsgs, g_pFileName, g_nFileLine, achMessage);
- }
-
- /*
- Function: DebugCreateMessage()
- Description: Convert the TRACE message parameters into a single message string
- */
-
- void DebugCreateMessage(char* lpszMessage, const char* lpcszFormat, va_list argPtr)
- {
- int len;
-
- len = vsprintf(lpszMessage, lpcszFormat, argPtr);
- while(lpszMessage[--len] == '\n') /* Remove trailing carriage returns */
- lpszMessage[len] = '\0';
- }
-
- /*
- Function: DebugPrintf()
- Description: Output a string to the application's debugger
- */
-
- int DebugPrintf(const char* lpcszFormat, ...)
- {
- int RetVal;
- va_list argPtr;
- char achDebugString[DEBUG_BUFFER_SIZE];
-
- va_start(argPtr, lpcszFormat); // Format the string
- RetVal = vsprintf(achDebugString, lpcszFormat, argPtr);
- if(RetVal == EOF) // Unable to format the string
- return DEBUG_ERR_WRITE;
-
- OutputDebugString(achDebugString);
- return RetVal;
- }
-
- #endif // _DEBUG
-
-