log FEATURE optionally store ly_err errors in ctx
It also uses the standard API now so that the
behavior is always the same.
diff --git a/src/log.c b/src/log.c
index 250ca9d..1633b27 100644
--- a/src/log.c
+++ b/src/log.c
@@ -491,17 +491,35 @@
free(plugin_msg);
}
-API void
-ly_err_print(struct ly_err_item *eitem)
+/**
+ * @brief Exact same functionality as ::ly_err_print() but has variable arguments so va_start() can
+ * be used and an empty va_list created.
+ */
+static void
+_ly_err_print(const struct ly_ctx *ctx, struct ly_err_item *eitem, ...)
{
- if (ly_log_opts & LY_LOLOG) {
- if (log_clb) {
- log_clb(eitem->level, eitem->msg, eitem->path);
- } else {
- fprintf(stderr, "libyang[%d]: %s%s", eitem->level, eitem->msg, eitem->path ? " " : "\n");
- if (eitem->path) {
- fprintf(stderr, "(path: %s)\n", eitem->path);
- }
- }
+ va_list ap;
+ char *path_dup = NULL;
+
+ LY_CHECK_ARG_RET(ctx, eitem, );
+
+ if (eitem->path) {
+ /* duplicate path because it will be freed */
+ path_dup = strdup(eitem->path);
+ LY_CHECK_ERR_RET(!path_dup, LOGMEM(ctx), );
}
+
+ va_start(ap, eitem);
+ log_vprintf(ctx, eitem->level, eitem->no, eitem->vecode, eitem->path, eitem->msg, ap);
+ va_end(ap);
+
+ if (path_dup) {
+ eitem->path = path_dup;
+ }
+}
+
+API void
+ly_err_print(const struct ly_ctx *ctx, struct ly_err_item *eitem)
+{
+ _ly_err_print(ctx, eitem);
}
diff --git a/src/log.h b/src/log.h
index bc41e0e..090d1aa 100644
--- a/src/log.h
+++ b/src/log.h
@@ -369,9 +369,10 @@
/**
* @brief Print the error structure as if just generated.
*
+ * @param[in] ctx Optional context to store the message in.
* @param[in] eitem Error item structure to print.
*/
-void ly_err_print(struct ly_err_item *eitem);
+void ly_err_print(const struct ly_ctx *ctx, struct ly_err_item *eitem);
/**
* @brief Free error structures from a context.
diff --git a/src/xpath.c b/src/xpath.c
index 9e672b5..2defc5a 100644
--- a/src/xpath.c
+++ b/src/xpath.c
@@ -3821,7 +3821,7 @@
}
if (err) {
- ly_err_print(err);
+ ly_err_print(set->ctx, err);
ly_err_free(err);
}
LY_CHECK_RET(rc);
@@ -4600,7 +4600,7 @@
free(*pattern);
LY_ARRAY_FREE(patterns);
if (rc && (rc != LY_EVALID)) {
- ly_err_print(err);
+ ly_err_print(set->ctx, err);
ly_err_free(err);
return rc;
}