File: debug.c

package info (click to toggle)
cdebconf 0.227
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,552 kB
  • sloc: ansic: 16,568; sh: 549; makefile: 456; sql: 52; perl: 13
file content (54 lines) | stat: -rw-r--r-- 1,096 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#ifndef NODEBUG
#include "common.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#ifdef SYSLOG_LOGGING
#include <syslog.h>
#endif

void debug_printf(int level, const char *fmt, ...)
{
	static int loglevel = -1;
	static FILE *fp = NULL;
	va_list ap;

	if (loglevel < 0)
	{
		const char *loglevel_env = getenv("DEBCONF_DEBUG");
		if (loglevel_env) {
			/* Allow DEBCONF_DEBUG=. to match everything, to
			 * make it easier to debug cdebconf and debconf
			 * simultaneously.
			 */
			if (strcmp(loglevel_env, ".") == 0)
				loglevel = INFO_VERBOSE;
			else if (strcmp(loglevel_env, "developer") == 0)
				loglevel = INFO_DEBUG;
			else
				loglevel = atoi(loglevel_env);
		} else
			loglevel = 0;

		if (getenv("DEBCONF_DEBUGFILE") == NULL ||
		    (fp = fopen(getenv("DEBCONF_DEBUGFILE"), "a")) == NULL)
			fp = stderr;
	}

	if (level <= loglevel)
	{
#ifndef SYSLOG_LOGGING
		va_start(ap, fmt);
		vfprintf(fp, fmt, ap);
		fputc('\n', fp);
		va_end(ap);

		fflush(fp);
#else
		va_start(ap, fmt);
		vsyslog(LOG_USER | LOG_DEBUG, fmt, ap);
		va_end(ap);
#endif
	}
}
#endif /* DEBUG */