Code explaination

showbix
showbix used Ask the Experts™
on
Can anyone explain to me line by line what is the meaning of this function. Really appreciate your assistance.
---------------------------------------------------------
#include "struct.h"
#include <errno.h>
#include <syslog.h>
#include "h.h"
 
#ifndef ssize_t
#define ssize_t unsigned int
#endif

#ifdef DEBUGMODE
static  char  debugbuf[1024];

#ifndef USE_VARARGS
/*VARARGS2*/
void  debug(level, form, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10)
int level;
char  *form, *p1, *p2, *p3, *p4, *p5, *p6, *p7, *p8, *p9, *p10;
{
#else
void  debug(level, form, va_alist)
int level;
char  *form;
va_dcl
{
  va_list vl;

  va_start(vl);
#endif
  int err = errno;

  if (level == 9)
    syslog(LOG_ERR, form, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10);
  if ((debuglevel >= 0) && (level <= debuglevel))
      {
#ifndef USE_VARARGS
    (void)sprintf(debugbuf, form,
        p1, p2, p3, p4, p5, p6, p7, p8, p9, p10);
#else
    (void)vsprintf(debugbuf, form, vl);
#endif
    if (local[2])
        {
      /*local[2]->sendM++; */
      local[2]->sendB += strlen(debugbuf);
        }
    (void)fprintf(stderr, "%s", debugbuf);
    (void)fputc('\n', stderr);
      }
  errno = err;
}
#endif /*debugmode */
---------------------------------------------------------
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Firstly the function is declared in one of two ways depending on whether USE_VARARGS is defined or not.  If it is the function is declared to take an integer (level), a char * (form) and a variable argument list.  The function then sets up a va_list ready for use.

If USE_VARARGS is not defined the function is declared to take an integer (level), a char * (form) and 10 other char * arguments.

The value of errno is stored in a local variable err to preserve its value should any subsequent function calls set it.

If the level argument is 9 the message is logged using unix syslog. BTW I don't think this code is right as it would only work if USE_VARARGS is not defined otherwise p1 -> p10 wouldn't be defined.  I think this should look something like :-

if ( level == 9 )
#ifdef USE_VARARGS
    vsyslog(level, form, vl);
#else
    syslog(LOG_ERR, form, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10);
#endif

Next the function checks if a variable called debuglevel is >= 0 and that the passed in level is <= debuglevel.  The definition of debuglevel is not shown but I assume that it's a global variable which can be set in any program using this function.

If these conditions are met the message is written into a buffer in one of two ways depending on whether variable arguments are being used or not.

The length of the message is added to a field sendB in a structure pointed to by local[2].  Again the definition of local[2] is not shown.  I'm not sure what the significance of this is.

THe message is then written to stderr along with a new line.

Finally errno is restored from the local variable err.

Hope this helps


Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial