Browse Source

error: Add faux_error_cstr(). Function creates single c-string from faux_error_t object

Serj Kalichev 2 years ago
parent
commit
7aa2af4f64
3 changed files with 29 additions and 0 deletions
  1. 1 0
      faux/error.h
  2. 27 0
      faux/error/error.c
  3. 1 0
      faux/faux.map

+ 1 - 0
faux/error.h

@@ -29,6 +29,7 @@ const char *faux_error_each(faux_error_node_t **iter);
 const char *faux_error_eachr(faux_error_node_t **iter);
 bool_t faux_error_fshow(const faux_error_t *error, FILE *handle);
 bool_t faux_error_show(const faux_error_t *error);
+char *faux_error_cstr(const faux_error_t *error);
 
 C_DECL_END
 

+ 27 - 0
faux/error/error.c

@@ -281,3 +281,30 @@ bool_t faux_error_show(const faux_error_t *error)
 {
 	return faux_error_fshow(error, stderr);
 }
+
+
+/** @brief Print error stack to C-string.
+ *
+ * Result must be freed by faux_str_free() later.
+ *
+ * @param [in] error Allocated and initialized error object.
+ * @return Allocated C-string or NULL on error.
+ */
+char *faux_error_cstr(const faux_error_t *error)
+{
+	faux_error_node_t *iter = NULL;
+	const char *s = NULL;
+	char *cstr = NULL;
+
+	if (!error)
+		return NULL;
+
+	iter = faux_error_iter(error);
+	while ((s = faux_error_each(&iter))) {
+		faux_str_cat(&cstr, s);
+		if (iter)
+			faux_str_cat(&cstr, "\n");
+	}
+
+	return cstr;
+}

+ 1 - 0
faux/faux.map

@@ -80,6 +80,7 @@ FAUX_2.0 {
 		faux_error_eachr;
 		faux_error_fshow;
 		faux_error_show;
+		faux_error_cstr;
 
 		faux_free;
 		faux_malloc;