#ifndef _DEBUG_H_
#define _DEBUG_H_

#ifdef DEBUG
extern unsigned int debug;
#define DPRINTF(level, fmt, args...) \
       do { if (debug & (level)) fprintf(stderr, fmt , ##args ); } while(0)
#define DEBUG_PERROR(errmsg) \
       do { if (debug & DEBUG_ERRS) perror(errmsg); } while(0)
#else
#define DPRINTF(args...)
#define DEBUG_PERROR(args...)
#endif

/*
 * Debug levels can be binary OR'd together to specify exactly the
 * amount of debugging output you'd like.
 */

#define DEBUG_NONE      0x00
#define DEBUG_ERRS      0x01
#define DEBUG_INIT      0x02
#define DEBUG_SOCKETS   0x04
#define DEBUG_PROCESSES 0x08
#define DEBUG_TCP       0x10
#define DEBUG_PACKETS   0x20
#define DEBUG_ROUTING   0x40
/* ... and so on ... */

/* To use:  in your main, if DEBUG is defined, do
 * getenv("DEBUG"), atoi it, and set the debug global equal to it.
 *
 * In your code, use as
 * DPRINTF(DEBUG_SOCKETS, "main() creating listen socket\n");
 * DPRINTF(DEBUG_SOCKETS, "main() listen socket created: %d\n",
listen_sock);
 * DEBUG_PERROR(DEBUG_ERRS|DEBUG_SOCKETS, "accept_client failed")
 * ...
 */

#endif /* _DEBUG_H_ */
