Skip to content

Commit

Permalink
libyang REFACTOR major logging updates
Browse files Browse the repository at this point in the history
Structured error information instead of a
single error message.
  • Loading branch information
michalvasko committed Feb 12, 2024
1 parent cf57dce commit 93f311f
Show file tree
Hide file tree
Showing 75 changed files with 1,777 additions and 1,997 deletions.
54 changes: 18 additions & 36 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,21 @@ extern ATOMIC_T ly_ll;
extern ATOMIC_T ly_log_opts;

struct ly_log_location_s {
uint64_t line; /**< One-time line value being reset after use - replaces whatever is in inputs */
struct ly_set inputs; /**< Set of const struct ly_in *in pointers providing the input handler with the line information (LIFO) */
struct ly_set scnodes; /**< Set of const struct lysc_node *scnode pointers providing the compiled schema node to generate path (LIFO) */
struct ly_set dnodes; /**< Set of const struct lyd_node *dnode pointers providing the data node to generate path (LIFO) */
struct ly_set paths; /**< Set of path strings (LIFO) */
struct ly_set spaths; /**< Set of schema path strings (LIFO) */
};

/**
* @brief Print a log message and store it into the context (if provided).
*
* @param[in] ctx libyang context to store the error record. If not provided, the error is just printed.
* @param[in] level Log message level (error, warning, etc.)
* @param[in] no Error type code.
* @param[in] err Error code.
* @param[in] format Format string to print.
*/
void ly_log(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, const char *format, ...) _FORMAT_PRINTF(4, 5);
void ly_log(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR err, const char *format, ...) _FORMAT_PRINTF(4, 5);

/**
* @brief Generate data path based on the data and schema nodes stored in the log location.
Expand Down Expand Up @@ -117,26 +116,27 @@ void ly_vlog(const struct ly_ctx *ctx, const char *apptag, LY_VECODE code, const
void ly_err_move(struct ly_ctx *src_ctx, struct ly_ctx *trg_ctx);

/**
* @brief Logger's location data setter.
* @brief Logger location data setter.
*
* If all the parameter are NULL, a root @p dnode is added (NULL).
*
* @param[in] scnode Compiled schema node.
* @param[in] dnode Data node.
* @param[in] path Direct path string to print.
* @param[in] in Input handler (providing line number)
* @param[in] line One-time line value to be reset when used.
* @param[in] spath Direct schema path string to print.
* @param[in] in Input handler (providing line number).
*/
void ly_log_location(const struct lysc_node *scnode, const struct lyd_node *dnode,
const char *path, const struct ly_in *in, uint64_t line);
const char *spath, const struct ly_in *in);

/**
* @brief Revert the specific logger's location data by number of changes made by ::ly_log_location().
* @brief Revert the specific logger location data by number of changes made by ::ly_log_location().
*
* @param[in] scnode_steps Number of items in ::ly_log_location_s.scnodes to forget.
* @param[in] dnode_steps Number of items in ::ly_log_location_s.dnodes to forget.
* @param[in] path_steps Number of path strings in ::ly_log_location_s.paths to forget.
* @param[in] spath_steps Number of path strings in ::ly_log_location_s.spaths to forget.
* @param[in] in_steps Number of input handlers ::ly_log_location_s.inputs to forget.
*/
void ly_log_location_revert(uint32_t scnode_steps, uint32_t dnode_steps, uint32_t path_steps, uint32_t in_steps);
void ly_log_location_revert(uint32_t scnode_steps, uint32_t dnode_steps, uint32_t spath_steps, uint32_t in_steps);

/**
* @brief Get the stored data node for logging at the index.
Expand All @@ -154,26 +154,22 @@ const struct lyd_node *ly_log_location_dnode(uint32_t idx);
uint32_t ly_log_location_dnode_count(void);

/**
* @brief Update location data for logger, not provided arguments (NULLs) are kept (does not override).
* @brief Update location schema/data nodes for logger, not provided arguments (NULLs) are kept (does not override).
*
* @param[in] SCNODE Compiled schema node.
* @param[in] DNODE Data node.
* @param[in] PATH Direct path string to print.
* @param[in] IN Input handler (providing line number)
*/
#define LOG_LOCSET(SCNODE, DNODE, PATH, IN) \
ly_log_location(SCNODE, DNODE, PATH, IN, 0)
#define LOG_LOCSET(SCNODE, DNODE) \
ly_log_location(SCNODE, DNODE, NULL, NULL)

/**
* @brief Update location data for logger, not provided arguments (NULLs) are kept (does not override).
* @brief Update location schema/data nodes for logger, not provided arguments (NULLs) are kept (does not override).
*
* @param[in] SCNODE_STEPS Number of the compiled schema nodes to remove from the stack.
* @param[in] DNODE_STEPS Number of the data nodes to remove from the stack.
* @param[in] PATH_STEPS Number of the direct path strings to remove from the stack.
* @param[in] IN_STEPS Number of the input handlers (providing line number) to remove from the stack.
*/
#define LOG_LOCBACK(SCNODE_STEPS, DNODE_STEPS, PATH_STEPS, IN_STEPS) \
ly_log_location_revert(SCNODE_STEPS, DNODE_STEPS, PATH_STEPS, IN_STEPS)
#define LOG_LOCBACK(SCNODE_STEPS, DNODE_STEPS) \
ly_log_location_revert(SCNODE_STEPS, DNODE_STEPS, 0, 0)

#define LOGERR(ctx, errno, ...) ly_log(ctx, LY_LLERR, errno, __VA_ARGS__)
#define LOGWRN(ctx, ...) ly_log(ctx, LY_LLWRN, 0, __VA_ARGS__)
Expand Down Expand Up @@ -201,20 +197,6 @@ void ly_log_dbg(uint32_t group, const char *format, ...);
#define LOGARG(CTX, ARG) LOGERR(CTX, LY_EINVAL, "Invalid argument %s (%s()).", #ARG, __func__)
#define LOGVAL(CTX, ...) ly_vlog(CTX, NULL, __VA_ARGS__)
#define LOGVAL_APPTAG(CTX, APPTAG, ...) ly_vlog(CTX, APPTAG, __VA_ARGS__)
#define LOGVAL_LINE(CTX, LINE, ...) \
ly_log_location(NULL, NULL, NULL, NULL, LINE); \
ly_vlog(CTX, NULL, __VA_ARGS__)

/**
* @brief Print Validation error from struct ly_err_item.
*
* String ::ly_err_item.msg cannot be used directly because it may contain the % character,
* which is incorrectly interpreted in this situation as a conversion specification.
*
* @param[in] CTX libyang context to store the error record. If not provided, the error is just printed.
* @param[in] ERRITEM pointer to ly_err_item that contains an error message.
*/
#define LOGVAL_ERRITEM(CTX, ERRITEM) ly_vlog(CTX, ERRITEM->apptag, ERRITEM->vecode, "%s", ERRITEM->msg)

#define LOGMEM_RET(CTX) LOGMEM(CTX); return LY_EMEM
#define LOGINT_RET(CTX) LOGINT(CTX); return LY_EINT
Expand Down
12 changes: 8 additions & 4 deletions src/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ lyjson_string(struct lyjson_ctx *jsonctx)
size_t len; /* length of the output string (write offset in output buffer) */
size_t size = 0; /* size of the output buffer */
size_t u;
uint64_t start_line;
uint64_t start_line, orig_line;
uint32_t value;
uint8_t i;

Expand Down Expand Up @@ -283,7 +283,10 @@ lyjson_string(struct lyjson_ctx *jsonctx)

/* EOF reached before endchar */
LOGVAL(jsonctx->ctx, LY_VCODE_EOF);
LOGVAL_LINE(jsonctx->ctx, start_line, LYVE_SYNTAX, "Missing quotation-mark at the end of a JSON string.");
orig_line = jsonctx->in->line;
jsonctx->in->line = start_line;
LOGVAL(jsonctx->ctx, LYVE_SYNTAX, "Missing quotation-mark at the end of a JSON string.");
jsonctx->in->line = orig_line;

error:
free(buf);
Expand Down Expand Up @@ -716,7 +719,8 @@ lyjson_ctx_new(const struct ly_ctx *ctx, struct ly_in *in, struct lyjson_ctx **j
jsonctx->ctx = ctx;
jsonctx->in = in;

LOG_LOCSET(NULL, NULL, NULL, in);
/* input line logging */
ly_log_location(NULL, NULL, NULL, in);

/* WS are always expected to be skipped */
lyjson_skip_ws(jsonctx);
Expand Down Expand Up @@ -1077,7 +1081,7 @@ lyjson_ctx_free(struct lyjson_ctx *jsonctx)
return;
}

LOG_LOCBACK(0, 0, 0, 1);
ly_log_location_revert(0, 0, 0, 1);

if (jsonctx->dynamic) {
free((char *)jsonctx->value);
Expand Down
Loading

0 comments on commit 93f311f

Please sign in to comment.