Thread: A small Debug routine

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    1

    A small Debug routine

    Hi,
    following is a small debugging routine embedded in some working sample source code provided by a hardware supplier. I am just going through their sample source code to get a better understanding of how their API works. However, I just can't make sense of how the parameters of DEBUG(x, outp) are being passed to dbg_p (char i, char *fmt, ...). What does "..." in dbg_p (char i, char *fmt, ...) mean?

    Would really appreciate if someone can enlighten me.

    Thanks in advance.

    Code:
    /* defines for debugging */
    #define DEBUG_OFF           99
    #define DEBUG_BASIC         03 
    #define DEBUG_ADV           02
    #define DEBUG_EXP           01
    
    static int can_debug = DEBUG_EXP;
    
    // debug version
    #ifdef _DEBUG
    
        #define DEBUG(x, outp)        ( dbg_p(x, outp ) )
    
        _inline void dbg_p (char i, char *fmt, ...)
        {
          char tmp[100];
          if (i>=can_debug){
    
            sprintf (tmp, "xlCANcontrol: %s", fmt);
    
            OutputDebugString (tmp);
          }
        } 
        
    // release version.
    #else
        #define DEBUG(x, outp)     
    #endif

    Following is a portion of the main routine that calls the debugging routine:

    Code:
    XLstatus CCANFunctions::canGetChannelMask()
    {
      XLstatus        xlStatus = XL_ERROR;
      char            tmp[100];
    
      // default values
      unsigned int  hwType     = 0;
      unsigned int  hwIndex    = 0;
      unsigned int  hwChannel  = 0;
      unsigned int  appChannel = 0;
      unsigned int  busType    = XL_BUS_TYPE_CAN;   
      unsigned int  i; 
     
      XLdriverConfig  xlDrvConfig;
    
       //check for hardware:
      xlStatus = xlGetDriverConfig(&xlDrvConfig);
      if (xlStatus) return xlStatus;
      
      // we check only if there is an application registered or not.
      xlStatus = xlGetApplConfig("xlCANcontrol", CHAN01, &hwType, &hwIndex, &hwChannel, busType); 
     
      // Set the params into registry (default values...!)
      if (xlStatus) {
        DEBUG(DEBUG_ADV,"set in VHWConf");
    
        for (i=0; i < xlDrvConfig.channelCount; i++) {
    
          sprintf (tmp, "hwType: %d, bustype: %d, hwChannel: %d, cap: 0x%x", 
            xlDrvConfig.channel[i].hwType, 
            xlDrvConfig.channel[i].connectedBusType,
            xlDrvConfig.channel[i].hwChannel,
            xlDrvConfig.channel[i].channelBusCapabilities);
          DEBUG(DEBUG_ADV,tmp);
    
          // we search not the first CAN cabs
          if ( (xlDrvConfig.channel[i].channelBusCapabilities & XL_BUS_ACTIVE_CAP_CAN) && (appChannel < 2) ) {
    
            hwType    = xlDrvConfig.channel[i].hwType;
            hwIndex   = xlDrvConfig.channel[i].hwIndex;
            hwChannel = xlDrvConfig.channel[i].hwChannel;
    
    .
    .
    .

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > What does "..." in dbg_p (char i, char *fmt, ...) mean?
    It means that a variable number of arguments are expected, in exactly the same way that printf() works.

    But then the author of the debug code has completely messed up that aspect with
    sprintf (tmp, "xlCANcontrol: %s", fmt);

    What they should have done is used vsprintf(), then all the arguments would be printed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary not built with debug info - why?
    By ulillillia in forum C Programming
    Replies: 15
    Last Post: 12-11-2008, 01:37 AM
  2. makefiles - debug & release?
    By cpjust in forum C Programming
    Replies: 6
    Last Post: 10-26-2007, 04:00 PM
  3. Debug --> Exceptions in Visual Studio 2005
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2007, 02:12 AM
  4. Results in Debug and Release mode are different
    By jaro in forum C Programming
    Replies: 11
    Last Post: 05-27-2006, 11:08 AM
  5. Ask about Debug Assert Failed
    By ooosawaddee3 in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2002, 11:07 PM