summaryrefslogtreecommitdiffstats
path: root/lib/log/log.c
blob: 7f14232443fd1538dc8454711b7a69f50b19c950 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

#include <assert.h>
#include <stdarg.h>
#include <stdlib.h>
#include <time.h>

#include "log.h"

static FILE *logf;
static bool debug;

static void __log_timestamp(void)
{
	char hms[20] = {'\0'};
	time_t t;

	if (!logf)
		return;

	t = time(NULL);
	strftime(hms, sizeof(hms), "%T", localtime(&t));
	fprintf(logf, "[%s] ", hms);
}

static void __log(const char *fmt, va_list ap)
{
	if (!logf)
		return;

	vfprintf(logf, fmt, ap);
	fflush(logf);
}

void pb_log(const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	__log_timestamp();
	__log(fmt, ap);
	va_end(ap);
}

void _pb_log_fn(const char *func, const char *fmt, ...)
{
	va_list ap;
	pb_log("%s: ", func);
	va_start(ap, fmt);
	__log(fmt, ap);
	va_end(ap);
}

void pb_debug(const char *fmt, ...)
{
	va_list ap;
	if (!debug)
		return;
	va_start(ap, fmt);
	__log_timestamp();
	__log(fmt, ap);
	va_end(ap);
}

void _pb_debug_fn(const char *func, const char *fmt, ...)
{
	va_list ap;
	if (!debug)
		return;
	pb_log("%s: ", func);
	va_start(ap, fmt);
	__log(fmt, ap);
	va_end(ap);
}

void _pb_debug_fl(const char *func, int line, const char *fmt, ...)
{
	va_list ap;
	if (!debug)
		return;
	pb_log("%s:%d: ", func, line);
	va_start(ap, fmt);
	__log(fmt, ap);
	va_end(ap);
}

void __pb_log_init(FILE *fp, bool _debug)
{
	if (logf)
		fflush(logf);
	logf = fp;
	debug = _debug;
}

void pb_log_set_debug(bool _debug)
{
	debug = _debug;
}

bool pb_log_get_debug(void)
{
	return debug;
}

FILE *pb_log_get_stream(void)
{
	static FILE *null_stream;
	if (!logf) {
		if (!null_stream)
			null_stream = fopen("/dev/null", "a");
		return null_stream;
	}
	return logf;
}
OpenPOWER on IntegriCloud