debug functions
[radsecproxy.git] / debug.c
1 /*
2  * Copyright (C) 2007 Stig Venaas <venaas@uninett.no>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  */
8
9 #include <stdint.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <errno.h>
13 #include <stdarg.h>
14 #include "debug.h"
15
16 static uint8_t debug_level = 0;
17
18 void debug(uint8_t level, char *format, ...) {
19     extern int errno;
20     
21     if (level >= debug_level) {
22         va_list ap;
23         va_start(ap, format);
24         vfprintf(stderr, format, ap);
25         va_end(ap);
26         if (errno) {
27             fprintf(stderr, ": ");
28             perror(NULL);
29             fprintf(stderr, "errno=%d\n", errno);
30         } else
31             fprintf(stderr, "\n");
32     }
33     if (level >= DBG_ERR)
34         exit(1);
35 }