Browse Source

faux.error: faux_error_sprintf() function to add formatted error message

Serj Kalichev 3 years ago
parent
commit
b8ae944f9b
2 changed files with 40 additions and 7 deletions
  1. 3 2
      faux/error.h
  2. 37 5
      faux/error/error.c

+ 3 - 2
faux/error.h

@@ -21,13 +21,14 @@ void faux_error_reset(faux_error_t *error);
 ssize_t faux_error_len(const faux_error_t *error);
 bool_t faux_error(const faux_error_t *error);
 bool_t faux_error_add(faux_error_t *error, const char *str);
+bool_t faux_error_sprintf(faux_error_t *error, const char *fmt, ...);
 
 faux_error_node_t *faux_error_iter(const faux_error_t *error);
 faux_error_node_t *faux_error_iterr(const faux_error_t *error);
 const char *faux_error_each(faux_error_node_t **iter);
 const char *faux_error_eachr(faux_error_node_t **iter);
-bool_t faux_error_fprint(const faux_error_t *error, FILE *handle);
-bool_t faux_error_print(const faux_error_t *error);
+bool_t faux_error_fshow(const faux_error_t *error, FILE *handle);
+bool_t faux_error_show(const faux_error_t *error);
 
 C_DECL_END
 

+ 37 - 5
faux/error/error.c

@@ -124,6 +124,38 @@ bool_t faux_error_add(faux_error_t *error, const char *str)
 }
 
 
+/** @brief Add formatted error message to message stack.
+ *
+ * @param [in] error Allocated and initialized error object.
+ * @param [in] fmt Format like printf() one.
+ * @param [in] args Variable length args.
+ * @return success - BOOL_TRUE, fail - BOOL_FALSE.
+ */
+bool_t faux_error_sprintf(faux_error_t *error, const char *fmt, ...)
+{
+	char *line = NULL;
+	va_list ap;
+	bool_t retval = BOOL_TRUE;
+
+	if (!error)
+		return BOOL_FALSE;
+	if (!fmt)
+		return BOOL_FALSE;
+
+	va_start(ap, fmt);
+	line = faux_str_vsprintf(fmt, ap);
+	va_end(ap);
+
+	if (!line)
+		return BOOL_FALSE;
+
+	retval = faux_error_add(error, line);
+	faux_str_free(line);
+
+	return retval;
+}
+
+
 /** @brief Initializes iterator to iterate through the entire error object.
  *
  * Before iterating with the faux_error_each() function the iterator must be
@@ -206,7 +238,7 @@ const char *faux_error_eachr(faux_error_node_t **iter)
  * @param [in] hierarchy Print errors using hierarchy view (or peer view).
  * @return BOOL_TRUE - success, BOOL_FALSE - fail.
  */
-static bool_t faux_error_show(const faux_error_t *error, FILE *handle,
+static bool_t faux_error_show_internal(const faux_error_t *error, FILE *handle,
 	bool_t reverse, bool_t hierarchy)
 {
 	faux_error_node_t *iter = NULL;
@@ -234,9 +266,9 @@ static bool_t faux_error_show(const faux_error_t *error, FILE *handle,
  * @param [in] handle File handler to write to.
  * @return BOOL_TRUE - success, BOOL_FALSE - fail.
  */
-bool_t faux_error_fprint(const faux_error_t *error, FILE *handle)
+bool_t faux_error_fshow(const faux_error_t *error, FILE *handle)
 {
-	return faux_error_show(error, handle, BOOL_FALSE, BOOL_FALSE);
+	return faux_error_show_internal(error, handle, BOOL_FALSE, BOOL_FALSE);
 }
 
 
@@ -245,7 +277,7 @@ bool_t faux_error_fprint(const faux_error_t *error, FILE *handle)
  * @param [in] error Allocated and initialized error object.
  * @return BOOL_TRUE - success, BOOL_FALSE - fail.
  */
-bool_t faux_error_print(const faux_error_t *error)
+bool_t faux_error_show(const faux_error_t *error)
 {
-	return faux_error_fprint(error, stderr);
+	return faux_error_fshow(error, stderr);
 }