summaryrefslogtreecommitdiff
path: root/ccan/pr_log/pr_log.c
blob: 1de8cb9e5c25cb833d01486bf048e85b659c68c3 (plain)
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
/* Licensed under LGPLv2.1+ - see LICENSE file for details */
#include "pr_log.h"

#include <ctype.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>

#include <ccan/str/str.h>

#define DEBUG_NEED_INIT INT_MIN
static int debug = DEBUG_NEED_INIT;

bool debug_is(int lvl)
{
	return lvl <= debug_level();
}

int debug_level(void)
{
	if (debug != DEBUG_NEED_INIT)
		return debug;
	char *c = getenv("DEBUG");
	if (!c) {
		debug = CCAN_PR_LOG_DEFAULT_LEVEL;
		return debug;
	}

	debug = atoi(c);
	return debug;
}

void pr_log_(char const *fmt, ...)
{
	int level = INT_MIN;
	if (fmt[0] == '<' && cisdigit(fmt[1]) && fmt[2] == '>')
		level = fmt[1] - '0';

	if (!debug_is(level))
		return;

	va_list va;
	va_start(va, fmt);
	vfprintf(stderr, fmt, va);
	va_end(va);
}