log UPDATE store last message
diff --git a/src/common.h b/src/common.h
index 06a79db..704a4e7 100644
--- a/src/common.h
+++ b/src/common.h
@@ -56,6 +56,9 @@
* Logger
*****************************************************************************/
+/** size of the last message buffer */
+#define LY_LAST_MSG_SIZE 512
+
extern ATOMIC_T ly_ll;
extern ATOMIC_T ly_log_opts;
diff --git a/src/log.c b/src/log.c
index 5ffc42e..a6b1fe4 100644
--- a/src/log.c
+++ b/src/log.c
@@ -41,12 +41,19 @@
THREAD_LOCAL uint32_t *temp_ly_log_opts;
static ly_log_clb log_clb;
static ATOMIC_T path_flag = 1;
+THREAD_LOCAL char last_msg[LY_LAST_MSG_SIZE];
#ifndef NDEBUG
ATOMIC_T ly_ldbg_groups = 0;
#endif
THREAD_LOCAL struct ly_log_location_s log_location = {0};
+LIBYANG_API_DEF const char *
+ly_last_errmsg(void)
+{
+ return last_msg;
+}
+
LIBYANG_API_DEF LY_ERR
ly_errcode(const struct ly_ctx *ctx)
{
@@ -458,14 +465,19 @@
return;
}
- /* store the error/warning (if we need to store errors internally, it does not matter what are the user log options) */
+ /* print into a single message */
+ if (vasprintf(&msg, format, args) == -1) {
+ LOGMEM(ctx);
+ free(path);
+ return;
+ }
+
+ /* store as the last message */
+ strncpy(last_msg, msg, LY_LAST_MSG_SIZE - 1);
+
+ /* store the error/warning in the context (if we need to store errors internally, it does not matter what are
+ * the user log options) */
if ((level < LY_LLVRB) && ctx && lostore) {
- assert(format);
- if (vasprintf(&msg, format, args) == -1) {
- LOGMEM(ctx);
- free(path);
- return;
- }
if (((no & ~LY_EPLUGIN) == LY_EVALID) && (vecode == LYVE_SUCCESS)) {
/* assume we are inheriting the error, so inherit vecode as well */
vecode = ly_vecode(ctx);
@@ -475,11 +487,6 @@
}
free_strs = 0;
} else {
- if (vasprintf(&msg, format, args) == -1) {
- LOGMEM(ctx);
- free(path);
- return;
- }
free_strs = 1;
}
diff --git a/src/log.h b/src/log.h
index 2144668..0d4a5d8 100644
--- a/src/log.h
+++ b/src/log.h
@@ -305,6 +305,16 @@
};
/**
+ * @brief Get the last (thread-specific) error message.
+ *
+ * ::ly_errmsg() should be used instead of this function but this one is useful for getting
+ * errors from functions that do not have any context accessible. Or as a simple unified logging API.
+ *
+ * @return Last generated error message.
+ */
+LIBYANG_API_DECL const char *ly_last_errmsg(void);
+
+/**
* @brief Get the last (thread, context-specific) validation error code.
*
* This value is set only if ly_errno is #LY_EVALID.