summaryrefslogtreecommitdiff
path: root/src/logerr.c
blob: 2d5c34083462743cf9e1e1a01ee7c905baf53029 (plain) (blame)
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
#include <s.h>

#include <stdarg.h>
#include <stdio.h>

#include "logerr.h"



static FILE *
STREAM = NULL;



void
vflogerrf(
	const char *const file,
	const char *const function,
	const int lineno,
	FILE *restrict stream,
	const char *restrict format,
	va_list args
) {
	if (fprintf(stream, "%s:%s:%d: ", file, function, lineno) < 0) {
		perror(__FILE__ ":vlogerr(): fprintf() < 0");
	}

	if (vfprintf(stream, format, args) < 0) {
		perror(__FILE__ ":vlogerr(): vfprintf() < 0");
	}

	if (fprintf(stream, "\n") < 0) {
		perror(__FILE__ ":vlogerr(): fprintf() < 0");
	}
}

void
flogerrf(
	const char *const file,
	const char *const function,
	const int lineno,
	FILE *restrict stream,
	const char *restrict format,
	...
) {
	va_list args;
	va_start(args, format);
	vflogerrf(file, function, lineno, stream, format, args);
	va_end(args);
}

void
logerrf(
	const char *const file,
	const char *const function,
	const int lineno,
	const char *restrict format,
	...
) {
	if (STREAM == NULL) {
		STREAM = stderr;
	}
	va_list args;
	va_start(args, format);
	vflogerrf(file, function, lineno, STREAM, format, args);
	va_end(args);
}

void
logerr_set_stream(FILE *stream) {
	STREAM = stream;
}