aboutsummaryrefslogtreecommitdiffstats
path: root/print_error.c
blob: 9c8bf1c9c6bb0c59af2c46ef873d00386ee63e76 (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
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
113
/* See LICENSE file for copyright and license details. */
#include "internal.h"


static void
print_error(struct liberror_error *error, FILE *fp, char *prefix)
{
	if (*error->description) {
		if (*error->source)
			fprintf(fp, "%sError in function %s: %s\n", prefix, error->source, error->description);
		else
			fprintf(fp, "%sError: %s\n", prefix, error->description);
	} else if (*error->source) {
		fprintf(fp, "%sError in function %s: %s error %lli\n", prefix, error->source, error->code_group, error->code);
	} else {
		fprintf(fp, "%sError: %s error %lli\n", prefix, error->code_group, error->code);
	}

	*strchr(prefix, '\0') = ' ';

	switch (error->details_type) {
	case LIBERROR_DETAILS_USER:
		if (error->details.user.print_data)
			error->details.user.print_data(error->details.user.data, fp, prefix);
		break;

	case LIBERROR_DETAILS_ONE_FILE:
		if (error->details.one_file.fd >= 0 || error->details.one_file.name) {
			fprintf(fp, "%sDetails:\n", prefix);
			if (error->details.one_file.name) {
				fprintf(fp, "%s  %s name: %s\n", prefix,
				        error->details.one_file.role, error->details.one_file.name);
			}
			if (error->details.one_file.fd >= 0) {
				fprintf(fp, "%s  %s descriptor: %i\n", prefix,
				        error->details.one_file.role, error->details.one_file.fd);
			}
		}
		break;

	case LIBERROR_DETAILS_TWO_FILES:
		if (error->details.two_files.fd1 >= 0 || error->details.two_files.name1 ||
		    error->details.two_files.fd2 >= 0 || error->details.two_files.name2) {
			fprintf(fp, "%sDetails:\n", prefix);
			if (error->details.two_files.fd1 >= 0) {
				fprintf(fp, "%s  %s descriptor: %i\n", prefix,
				        error->details.two_files.role1, error->details.two_files.fd1);
			}
			if (error->details.two_files.name1) {
				fprintf(fp, "%s  %s name: %s\n", prefix,
				        error->details.two_files.role1, error->details.two_files.name1);
			}
			if (error->details.two_files.fd2 >= 0) {
				fprintf(fp, "%s  %s descriptor: %i\n", prefix,
				        error->details.two_files.role2, error->details.two_files.fd2);
			}
			if (error->details.two_files.name2) {
				fprintf(fp, "%s  %s name: %s\n", prefix,
				        error->details.two_files.role2, error->details.two_files.name2);
			}
		}
		break;

	case LIBERROR_DETAILS_NONE:
	default:
		break;
	}

	liberror_print_backtrace(error, fp, prefix);
}


void
liberror_print_error(struct liberror_error *error, FILE *fp, int reset, const char *prefix_)
{
	struct liberror_error *err = error;
	char *prefix = (char []){"    "};
	char *p, *q;

	if (!err) {
		err = liberror_get_error();
		if (!err)
			return;
	}

	if (prefix_ && *prefix_) {
		prefix = alloca(strlen(prefix) + sizeof(":     "));
		stpcpy(q = stpcpy(p = stpcpy(stpcpy(prefix, prefix_), ": "), "  "), "  ");
	} else {
		p = &prefix[0];
		q = &prefix[2];
	}

	if (!fp)
		fp = stderr;

	*p = *q = '\0';
	print_error(err, fp, prefix);

	while ((err = err->cause)) {
		*p = *q = '\0';
		fprintf(fp, "%sCaused by:\n", prefix);
		*p = ' ';
		print_error(err, fp, prefix);
	}

	if (reset) {
		if (error)
			liberror_free_error(error);
		else
			liberror_reset_error();
	}
}