aboutsummaryrefslogtreecommitdiffstats
path: root/print_error.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2019-04-12 21:12:12 +0200
committerMattias Andrée <maandree@kth.se>2019-04-12 21:13:19 +0200
commit1484b1264552ad05d75a4ca4f40972ac94e729e9 (patch)
treeb9f6d6212866699db66e8bd64310d445498db2f3 /print_error.c
downloadliberror-1484b1264552ad05d75a4ca4f40972ac94e729e9.tar.gz
liberror-1484b1264552ad05d75a4ca4f40972ac94e729e9.tar.bz2
liberror-1484b1264552ad05d75a4ca4f40972ac94e729e9.tar.xz
First commit
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'print_error.c')
-rw-r--r--print_error.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/print_error.c b/print_error.c
new file mode 100644
index 0000000..4675bab
--- /dev/null
+++ b/print_error.c
@@ -0,0 +1,68 @@
+/* See LICENSE file for copyright and license details. */
+#include "internal.h"
+
+
+static void
+print_error_description(struct liberror_error *error, FILE *fp, const 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);
+ }
+}
+
+
+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_description(err, fp, prefix);
+
+ *p = ' ';
+ liberror_print_backtrace(err, fp, prefix);
+
+ while ((err = err->cause)) {
+ *p = *q = '\0';
+ fprintf(fp, "%sCaused by:\n", prefix);
+
+ *p = ' ';
+ print_error_description(err, fp, prefix);
+
+ *q = ' ';
+ liberror_print_backtrace(err, fp, prefix);
+ }
+
+ if (reset) {
+ if (error)
+ liberror_free_error(error);
+ else
+ liberror_reset_error();
+ }
+}