#include #include #include int dodebug = 0, dbug_level = 0, OnlyLevel = 0; int first = 1; int DoDebug(int level) { char *DebugLevel; if (first) { if ((DebugLevel = getenv("DEBUG_LEVEL"))) { dodebug = 1; dbug_level = atoi(DebugLevel); if (dbug_level < 0) { dbug_level = -dbug_level; OnlyLevel = 1; } } first = 0; } if (!dodebug) return 0; if (OnlyLevel && level != dbug_level) return 0; if (level > dbug_level) return 0; return 1; } void debug(int level, char *fmt, ...) { va_list args; char *DebugLevel, msg[128]; short NoHdr = 0; if (first) { if ((DebugLevel = getenv("DEBUG_LEVEL"))) { dodebug = 1; dbug_level = atoi(DebugLevel); if (dbug_level < 0) { dbug_level = -dbug_level; OnlyLevel = 1; } } first = 0; } if (!dodebug) return; if (OnlyLevel && level != dbug_level) return; if (level < 0) { level = -level; NoHdr = 1; } if (level > dbug_level) return; va_start(args, fmt); if (NoHdr) (void)vfprintf(stderr, fmt, args); else { (void)vsprintf(msg, fmt, args); (void)fprintf(stderr, "dbug %d: %s", level, msg); } fflush(stderr); va_end(args); }