summaryrefslogtreecommitdiff
path: root/fitz/base_error.c
blob: 3045d9123829079a4965fd3d061bccefcf722860 (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
#include "fitz.h"

char fz_errorbuf[150*20] = {0};
static int fz_errorlen = 0;
static int fz_errorclear = 1;

static void
fz_printerror(int type, const char *file, int line, const char *func, char *msg)
{
	char buf[150];
	int len;
	char *s;

	s = strrchr(file, '\\');
	if (s)
		file = s + 1;

	fprintf(stderr, "%c %s:%d: %s(): %s\n", type, file, line, func, msg);

	snprintf(buf, sizeof buf, "%s:%d: %s(): %s", file, line, func, msg);
	buf[sizeof(buf)-1] = 0;
	len = strlen(buf);

	if (fz_errorclear)
	{
		fz_errorclear = 0;
		fz_errorlen = 0;
		memset(fz_errorbuf, 0, sizeof fz_errorbuf);
	}

	if (fz_errorlen + len + 2 < sizeof fz_errorbuf)
	{
		memcpy(fz_errorbuf + fz_errorlen, buf, len);
		fz_errorlen += len;
		fz_errorbuf[fz_errorlen++] = '\n';
		fz_errorbuf[fz_errorlen] = 0;
	}
}

void fz_warn(char *fmt, ...)
{
	va_list ap;
	fprintf(stderr, "warning: ");
	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);
	fprintf(stderr, "\n");
}

fz_error fz_throwimp(const char *file, int line, const char *func, char *fmt, ...)
{
	char buf[150];
	va_list ap;
	va_start(ap, fmt);
	vsnprintf(buf, sizeof buf, fmt, ap);
	va_end(ap);
	buf[sizeof(buf)-1] = 0;
	fz_printerror('+', file, line, func, buf);
	return -1;
}

fz_error fz_rethrowimp(fz_error cause, const char *file, int line, const char *func, char *fmt, ...)
{
	char buf[150];
	va_list ap;
	va_start(ap, fmt);
	vsnprintf(buf, sizeof buf, fmt, ap);
	va_end(ap);
	buf[sizeof(buf)-1] = 0;
	fz_printerror('|', file, line, func, buf);
	return cause;
}

fz_error fz_catchimp(fz_error cause, const char *file, int line, const char *func, char *fmt, ...)
{
	char buf[150];
	va_list ap;
	va_start(ap, fmt);
	vsnprintf(buf, sizeof buf, fmt, ap);
	va_end(ap);
	buf[sizeof(buf)-1] = 0;
	fz_printerror('\\', file, line, func, buf);
	fz_errorclear = 1;
	return cause;
}