blob: 1a75dece64092674f6148829cc2bd8b222a3af9a [file] [log] [blame]
Radek Krejci5aeea3a2018-09-05 13:29:36 +02001/**
2 * @file log.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko193dacd2022-10-13 08:43:05 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
Radek Krejci5aeea3a2018-09-05 13:29:36 +02005 * @brief Logger routines implementations
6 *
Michal Vasko193dacd2022-10-13 08:43:05 +02007 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
Radek Krejci5aeea3a2018-09-05 13:29:36 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
Christian Hopps32874e12021-05-01 09:43:54 -040016#define _GNU_SOURCE /* asprintf, strdup */
Radek Krejci535ea9f2020-05-29 16:01:05 +020017
18#include "log.h"
Radek Krejcib7db73a2018-10-24 14:18:40 +020019
Radek Krejci5aeea3a2018-09-05 13:29:36 +020020#include <assert.h>
Radek Krejcic04f0a22018-09-21 15:49:45 +020021#include <inttypes.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <pthread.h>
Radek Krejci5aeea3a2018-09-05 13:29:36 +020023#include <stdarg.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020024#include <stdint.h>
Radek Krejci5aeea3a2018-09-05 13:29:36 +020025#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include <stdlib.h>
27#include <string.h>
Radek Krejci5aeea3a2018-09-05 13:29:36 +020028
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "common.h"
Radek Krejciaa45bda2020-07-20 07:43:38 +020030#include "compat.h"
Radek Krejciaddfc9a2020-12-17 20:46:35 +010031#include "in_internal.h"
Radek Krejci0935f412019-08-20 16:15:18 +020032#include "plugins_exts.h"
Radek Krejci77114102021-03-10 15:21:57 +010033#include "set.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020034#include "tree_data.h"
Michal Vaskodbf3e652022-10-21 08:46:25 +020035#include "tree_data_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "tree_schema.h"
Michal Vasko193dacd2022-10-13 08:43:05 +020037#include "tree_schema_internal.h"
Radek Krejci5aeea3a2018-09-05 13:29:36 +020038
Václav Kubernátd367ad92021-11-29 09:28:56 +010039ATOMIC_T ly_ll = (uint_fast32_t)LY_LLWRN;
40ATOMIC_T ly_log_opts = (uint_fast32_t)(LY_LOLOG | LY_LOSTORE_LAST);
Michal Vaskod4a6d042022-12-08 08:34:29 +010041THREAD_LOCAL uint32_t *temp_ly_log_opts;
Michal Vaskod8085612020-08-21 12:55:23 +020042static ly_log_clb log_clb;
Václav Kubernátd367ad92021-11-29 09:28:56 +010043static ATOMIC_T path_flag = 1;
Michal Vasko236cbac2023-02-10 15:45:37 +010044THREAD_LOCAL char last_msg[LY_LAST_MSG_SIZE];
Radek Krejci5aeea3a2018-09-05 13:29:36 +020045#ifndef NDEBUG
Václav Kubernátd367ad92021-11-29 09:28:56 +010046ATOMIC_T ly_ldbg_groups = 0;
Radek Krejci5aeea3a2018-09-05 13:29:36 +020047#endif
48
Radek Krejciddace2c2021-01-08 11:30:56 +010049THREAD_LOCAL struct ly_log_location_s log_location = {0};
50
Michal Vasko236cbac2023-02-10 15:45:37 +010051LIBYANG_API_DEF const char *
52ly_last_errmsg(void)
53{
54 return last_msg;
55}
56
Jan Kundrátc53a7ec2021-12-09 16:01:19 +010057LIBYANG_API_DEF LY_ERR
Radek Krejcid33273d2018-10-25 14:55:52 +020058ly_errcode(const struct ly_ctx *ctx)
59{
60 struct ly_err_item *i;
61
Radek Krejci572ee602020-09-16 14:35:08 +020062 i = ly_err_last(ctx);
Radek Krejcid33273d2018-10-25 14:55:52 +020063 if (i) {
Radek Krejci572ee602020-09-16 14:35:08 +020064 return i->no;
Radek Krejcid33273d2018-10-25 14:55:52 +020065 }
66
67 return LY_SUCCESS;
68}
69
Jan Kundrátc53a7ec2021-12-09 16:01:19 +010070LIBYANG_API_DEF LY_VECODE
Radek Krejci5aeea3a2018-09-05 13:29:36 +020071ly_vecode(const struct ly_ctx *ctx)
72{
73 struct ly_err_item *i;
74
Radek Krejci572ee602020-09-16 14:35:08 +020075 i = ly_err_last(ctx);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020076 if (i) {
Radek Krejci572ee602020-09-16 14:35:08 +020077 return i->vecode;
Radek Krejci5aeea3a2018-09-05 13:29:36 +020078 }
79
80 return LYVE_SUCCESS;
81}
82
Jan Kundrátc53a7ec2021-12-09 16:01:19 +010083LIBYANG_API_DEF const char *
Radek Krejci5aeea3a2018-09-05 13:29:36 +020084ly_errmsg(const struct ly_ctx *ctx)
85{
86 struct ly_err_item *i;
87
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020088 LY_CHECK_ARG_RET(NULL, ctx, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020089
Radek Krejci572ee602020-09-16 14:35:08 +020090 i = ly_err_last(ctx);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020091 if (i) {
Radek Krejci572ee602020-09-16 14:35:08 +020092 return i->msg;
Radek Krejci5aeea3a2018-09-05 13:29:36 +020093 }
94
95 return NULL;
96}
97
Jan Kundrátc53a7ec2021-12-09 16:01:19 +010098LIBYANG_API_DEF const char *
Radek Krejci5aeea3a2018-09-05 13:29:36 +020099ly_errpath(const struct ly_ctx *ctx)
100{
101 struct ly_err_item *i;
102
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200103 LY_CHECK_ARG_RET(NULL, ctx, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200104
Radek Krejci572ee602020-09-16 14:35:08 +0200105 i = ly_err_last(ctx);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200106 if (i) {
Radek Krejci572ee602020-09-16 14:35:08 +0200107 return i->path;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200108 }
109
110 return NULL;
111}
112
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100113LIBYANG_API_DEF const char *
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200114ly_errapptag(const struct ly_ctx *ctx)
115{
116 struct ly_err_item *i;
117
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200118 LY_CHECK_ARG_RET(NULL, ctx, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200119
Radek Krejci572ee602020-09-16 14:35:08 +0200120 i = ly_err_last(ctx);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200121 if (i) {
Radek Krejci572ee602020-09-16 14:35:08 +0200122 return i->apptag;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200123 }
124
125 return NULL;
126}
127
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100128LIBYANG_API_DEF LY_ERR
aPiecek6d618552021-06-18 10:02:59 +0200129ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *path, char *apptag, const char *err_format, ...)
Radek Krejcie7b95092019-05-15 11:03:07 +0200130{
Radek Krejcidb0ee022021-03-15 16:53:44 +0100131 char *msg = NULL;
132 struct ly_err_item *e;
Radek Krejcie7b95092019-05-15 11:03:07 +0200133
Radek Krejcid43298b2021-03-25 16:17:15 +0100134 if (!err || (ecode == LY_SUCCESS)) {
Radek Krejcidb0ee022021-03-15 16:53:44 +0100135 /* nothing to do */
136 return ecode;
137 }
138
139 e = malloc(sizeof *e);
140 LY_CHECK_ERR_RET(!e, LOGMEM(NULL), LY_EMEM);
141 e->prev = (*err) ? (*err)->prev : e;
142 e->next = NULL;
143 if (*err) {
144 (*err)->prev->next = e;
145 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200146
147 /* fill in the information */
Radek Krejcidb0ee022021-03-15 16:53:44 +0100148 e->level = LY_LLERR;
149 e->no = ecode;
150 e->vecode = vecode;
151 e->path = path;
152 e->apptag = apptag;
Radek Krejcie7b95092019-05-15 11:03:07 +0200153
aPiecek6d618552021-06-18 10:02:59 +0200154 if (err_format) {
Radek Krejcidb0ee022021-03-15 16:53:44 +0100155 va_list print_args;
156
aPiecek6d618552021-06-18 10:02:59 +0200157 va_start(print_args, err_format);
Radek Krejcidb0ee022021-03-15 16:53:44 +0100158
aPiecek6d618552021-06-18 10:02:59 +0200159 if (vasprintf(&msg, err_format, print_args) == -1) {
160 /* we don't have anything more to do, just set msg to NULL to avoid undefined content,
Radek Krejcidb0ee022021-03-15 16:53:44 +0100161 * still keep the information about the original error instead of LY_EMEM or other printf's error */
162 msg = NULL;
163 }
164
165 va_end(print_args);
166 }
167 e->msg = msg;
168
169 if (!(*err)) {
170 *err = e;
171 }
172
173 return e->no;
Radek Krejcie7b95092019-05-15 11:03:07 +0200174}
175
Michal Vasko88ccd582023-03-30 11:50:57 +0200176/**
177 * @brief Get error record from error hash table of a context for the current thread.
178 *
179 * @param[in] ctx Context to use.
180 * @return Thread error record, if any.
181 */
182static struct ly_ctx_err_rec *
183ly_err_get_rec(const struct ly_ctx *ctx)
184{
Michal Vasko4cca0092023-03-30 13:20:53 +0200185 struct ly_ctx_err_rec rec, *match;
Michal Vasko88ccd582023-03-30 11:50:57 +0200186
187 /* prepare record */
188 rec.tid = pthread_self();
189
190 /* get the pointer to the matching record */
Michal Vaskoae130f52023-04-20 14:25:16 +0200191 if (lyht_find(ctx->err_ht, &rec, lyht_hash((void *)&rec.tid, sizeof rec.tid), (void **)&match)) {
Michal Vasko4cca0092023-03-30 13:20:53 +0200192 return NULL;
193 }
Michal Vasko88ccd582023-03-30 11:50:57 +0200194
195 return match;
196}
197
Michal Vaskoe0be7452023-03-30 13:35:35 +0200198/**
199 * @brief Insert new error record to error hash table of a context for the current thread.
200 *
201 * @param[in] ctx Context to use.
202 * @return Thread error record.
203 */
204static struct ly_ctx_err_rec *
205ly_err_new_rec(const struct ly_ctx *ctx)
206{
207 struct ly_ctx_err_rec new, *rec;
208 LY_ERR r;
209
210 /* insert a new record */
211 new.err = NULL;
212 new.tid = pthread_self();
213
214 /* reuse lock */
215 /* LOCK */
216 pthread_mutex_lock((pthread_mutex_t *)&ctx->lyb_hash_lock);
217
Michal Vaskoae130f52023-04-20 14:25:16 +0200218 r = lyht_insert(ctx->err_ht, &new, lyht_hash((void *)&new.tid, sizeof new.tid), (void **)&rec);
Michal Vaskoe0be7452023-03-30 13:35:35 +0200219
220 /* UNLOCK */
221 pthread_mutex_unlock((pthread_mutex_t *)&ctx->lyb_hash_lock);
222
223 return r ? NULL : rec;
224}
225
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100226LIBYANG_API_DEF struct ly_err_item *
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200227ly_err_first(const struct ly_ctx *ctx)
228{
Michal Vasko88ccd582023-03-30 11:50:57 +0200229 struct ly_ctx_err_rec *rec;
230
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200231 LY_CHECK_ARG_RET(NULL, ctx, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200232
Michal Vasko88ccd582023-03-30 11:50:57 +0200233 /* get the pointer to the matching record */
234 rec = ly_err_get_rec(ctx);
235
236 return rec ? rec->err : NULL;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200237}
238
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100239LIBYANG_API_DEF struct ly_err_item *
Radek Krejci572ee602020-09-16 14:35:08 +0200240ly_err_last(const struct ly_ctx *ctx)
241{
Michal Vasko88ccd582023-03-30 11:50:57 +0200242 struct ly_ctx_err_rec *rec;
Radek Krejci572ee602020-09-16 14:35:08 +0200243
244 LY_CHECK_ARG_RET(NULL, ctx, NULL);
245
Michal Vasko88ccd582023-03-30 11:50:57 +0200246 /* get the pointer to the matching record */
247 if (!(rec = ly_err_get_rec(ctx))) {
248 return NULL;
249 }
250
251 return rec->err ? rec->err->prev : NULL;
Radek Krejci572ee602020-09-16 14:35:08 +0200252}
253
Michal Vasko9dbb91d2023-01-30 13:59:22 +0100254void
255ly_err_move(struct ly_ctx *src_ctx, struct ly_ctx *trg_ctx)
256{
Michal Vasko88ccd582023-03-30 11:50:57 +0200257 struct ly_ctx_err_rec *rec;
258 struct ly_err_item *err = NULL;
Michal Vasko9dbb91d2023-01-30 13:59:22 +0100259
Michal Vasko88ccd582023-03-30 11:50:57 +0200260 /* get and remove the errors from src */
261 rec = ly_err_get_rec(src_ctx);
262 if (rec) {
263 err = rec->err;
264 rec->err = NULL;
265 }
Michal Vasko9dbb91d2023-01-30 13:59:22 +0100266
267 /* set them for trg */
Michal Vaskoe0be7452023-03-30 13:35:35 +0200268 if (!(rec = ly_err_get_rec(trg_ctx))) {
269 if (!(rec = ly_err_new_rec(trg_ctx))) {
270 LOGINT(NULL);
271 ly_err_free(err);
272 return;
273 }
274 }
Michal Vasko88ccd582023-03-30 11:50:57 +0200275 ly_err_free(rec->err);
276 rec->err = err;
Michal Vasko9dbb91d2023-01-30 13:59:22 +0100277}
278
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100279LIBYANG_API_DEF void
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200280ly_err_free(void *ptr)
281{
Michal Vasko88ccd582023-03-30 11:50:57 +0200282 struct ly_err_item *e, *next;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200283
284 /* clean the error list */
Michal Vasko88ccd582023-03-30 11:50:57 +0200285 LY_LIST_FOR_SAFE(ptr, next, e) {
286 free(e->msg);
287 free(e->path);
288 free(e->apptag);
289 free(e);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200290 }
291}
292
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100293LIBYANG_API_DEF void
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200294ly_err_clean(struct ly_ctx *ctx, struct ly_err_item *eitem)
295{
Michal Vasko88ccd582023-03-30 11:50:57 +0200296 struct ly_ctx_err_rec *rec;
297 struct ly_err_item *e;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200298
Michal Vasko88ccd582023-03-30 11:50:57 +0200299 if (!(rec = ly_err_get_rec(ctx))) {
300 return;
301 }
302 if (rec->err == eitem) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200303 eitem = NULL;
304 }
Michal Vasko88ccd582023-03-30 11:50:57 +0200305
306 if (!eitem) {
307 /* free all err */
308 ly_err_free(rec->err);
309 rec->err = NULL;
310 } else {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200311 /* disconnect the error */
Michal Vasko88ccd582023-03-30 11:50:57 +0200312 for (e = rec->err; e && (e->next != eitem); e = e->next) {}
313 assert(e);
314 e->next = NULL;
315 rec->err->prev = e;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200316 /* free this err and newer */
317 ly_err_free(eitem);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200318 }
319}
320
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100321LIBYANG_API_DEF LY_LOG_LEVEL
Radek Krejci52b6d512020-10-12 12:33:17 +0200322ly_log_level(LY_LOG_LEVEL level)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200323{
Václav Kubernátd367ad92021-11-29 09:28:56 +0100324 LY_LOG_LEVEL prev = ATOMIC_LOAD_RELAXED(ly_ll);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200325
Václav Kubernátd367ad92021-11-29 09:28:56 +0100326 ATOMIC_STORE_RELAXED(ly_ll, level);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200327 return prev;
328}
329
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100330LIBYANG_API_DEF uint32_t
Radek Krejci1deb5be2020-08-26 16:43:36 +0200331ly_log_options(uint32_t opts)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200332{
Václav Kubernátd367ad92021-11-29 09:28:56 +0100333 uint32_t prev = ATOMIC_LOAD_RELAXED(ly_log_opts);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200334
Václav Kubernátd367ad92021-11-29 09:28:56 +0100335 ATOMIC_STORE_RELAXED(ly_log_opts, opts);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200336 return prev;
337}
338
Michal Vaskod4a6d042022-12-08 08:34:29 +0100339LIBYANG_API_DEF void
340ly_temp_log_options(uint32_t *opts)
341{
342 temp_ly_log_opts = opts;
343}
344
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100345LIBYANG_API_DEF uint32_t
Radek Krejci68433c92020-10-12 17:03:55 +0200346ly_log_dbg_groups(uint32_t dbg_groups)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200347{
348#ifndef NDEBUG
Václav Kubernátd367ad92021-11-29 09:28:56 +0100349 uint32_t prev = ATOMIC_LOAD_RELAXED(ly_ldbg_groups);
Radek Krejciebdaed02020-11-09 13:05:06 +0100350
Václav Kubernátd367ad92021-11-29 09:28:56 +0100351 ATOMIC_STORE_RELAXED(ly_ldbg_groups, dbg_groups);
Radek Krejciebdaed02020-11-09 13:05:06 +0100352 return prev;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200353#else
354 (void)dbg_groups;
Radek Krejciebdaed02020-11-09 13:05:06 +0100355 return 0;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200356#endif
357}
358
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100359LIBYANG_API_DEF void
Radek Krejci857189e2020-09-01 13:26:36 +0200360ly_set_log_clb(ly_log_clb clb, ly_bool path)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200361{
Michal Vaskod8085612020-08-21 12:55:23 +0200362 log_clb = clb;
Václav Kubernátd367ad92021-11-29 09:28:56 +0100363 ATOMIC_STORE_RELAXED(path_flag, path);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200364}
365
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100366LIBYANG_API_DEF ly_log_clb
Michal Vaskod8085612020-08-21 12:55:23 +0200367ly_get_log_clb(void)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200368{
Michal Vaskod8085612020-08-21 12:55:23 +0200369 return log_clb;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200370}
371
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100372void
Michal Vasko59e69e72022-02-18 09:18:21 +0100373ly_log_location(const struct lysc_node *scnode, const struct lyd_node *dnode, const char *path, const struct ly_in *in,
Michal Vaskof8ebf132022-11-21 14:06:48 +0100374 uint64_t line)
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100375{
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100376 if (scnode) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100377 ly_set_add(&log_location.scnodes, (void *)scnode, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100378 }
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100379 if (dnode) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100380 ly_set_add(&log_location.dnodes, (void *)dnode, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100381 }
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100382 if (path) {
383 char *s = strdup(path);
Michal Vasko26bbb272022-08-02 14:54:33 +0200384
Radek Krejciddace2c2021-01-08 11:30:56 +0100385 LY_CHECK_ERR_RET(!s, LOGMEM(NULL), );
386 ly_set_add(&log_location.paths, s, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100387 }
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100388 if (in) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100389 ly_set_add(&log_location.inputs, (void *)in, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100390 }
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100391 if (line) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100392 log_location.line = line;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100393 }
394}
395
396void
Michal Vasko59e69e72022-02-18 09:18:21 +0100397ly_log_location_revert(uint32_t scnode_steps, uint32_t dnode_steps, uint32_t path_steps, uint32_t in_steps)
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100398{
Radek Krejciddace2c2021-01-08 11:30:56 +0100399 for (uint32_t i = scnode_steps; i && log_location.scnodes.count; i--) {
400 log_location.scnodes.count--;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100401 }
402
Radek Krejciddace2c2021-01-08 11:30:56 +0100403 for (uint32_t i = dnode_steps; i && log_location.dnodes.count; i--) {
404 log_location.dnodes.count--;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100405 }
406
Radek Krejciddace2c2021-01-08 11:30:56 +0100407 for (uint32_t i = path_steps; i && log_location.paths.count; i--) {
408 ly_set_rm_index(&log_location.paths, log_location.paths.count - 1, free);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100409 }
410
Radek Krejciddace2c2021-01-08 11:30:56 +0100411 for (uint32_t i = in_steps; i && log_location.inputs.count; i--) {
412 log_location.inputs.count--;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100413 }
414
Radek Krejciddace2c2021-01-08 11:30:56 +0100415 /* deallocate the empty sets */
416 if (scnode_steps && !log_location.scnodes.count) {
417 ly_set_erase(&log_location.scnodes, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100418 }
Radek Krejciddace2c2021-01-08 11:30:56 +0100419 if (dnode_steps && !log_location.dnodes.count) {
420 ly_set_erase(&log_location.dnodes, NULL);
421 }
422 if (path_steps && !log_location.paths.count) {
423 ly_set_erase(&log_location.paths, free);
424 }
425 if (in_steps && !log_location.inputs.count) {
426 ly_set_erase(&log_location.inputs, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100427 }
428}
429
Michal Vasko88ccd582023-03-30 11:50:57 +0200430/**
431 * @brief Store generated error in a context.
432 *
433 * @param[in] ctx Context to use.
434 * @param[in] level Message log level.
435 * @param[in] no Error number.
436 * @param[in] vecode Error validation error code.
437 * @param[in] msg Error message, always spent.
438 * @param[in] path Error path, always spent.
439 * @param[in] apptag Error app tag, always spent.
440 * @return LY_ERR value.
441 */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200442static LY_ERR
443log_store(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, LY_VECODE vecode, char *msg, char *path, char *apptag)
444{
Michal Vaskoe0be7452023-03-30 13:35:35 +0200445 struct ly_ctx_err_rec *rec;
Michal Vasko88ccd582023-03-30 11:50:57 +0200446 struct ly_err_item *e, *last;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200447
448 assert(ctx && (level < LY_LLVRB));
449
Michal Vasko88ccd582023-03-30 11:50:57 +0200450 if (!(rec = ly_err_get_rec(ctx))) {
Michal Vaskoe0be7452023-03-30 13:35:35 +0200451 if (!(rec = ly_err_new_rec(ctx))) {
452 goto mem_fail;
Michal Vasko88ccd582023-03-30 11:50:57 +0200453 }
454 }
455
456 e = rec->err;
457 if (!e) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200458 /* if we are only to fill in path, there must have been an error stored */
459 assert(msg);
Michal Vasko88ccd582023-03-30 11:50:57 +0200460 e = malloc(sizeof *e);
461 LY_CHECK_GOTO(!e, mem_fail);
462 e->prev = e;
463 e->next = NULL;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200464
Michal Vasko88ccd582023-03-30 11:50:57 +0200465 rec->err = e;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200466 } else if (!msg) {
467 /* only filling the path */
468 assert(path);
469
470 /* find last error */
Michal Vasko88ccd582023-03-30 11:50:57 +0200471 e = e->prev;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200472 do {
Michal Vasko88ccd582023-03-30 11:50:57 +0200473 if (e->level == LY_LLERR) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200474 /* fill the path */
Michal Vasko88ccd582023-03-30 11:50:57 +0200475 free(e->path);
476 e->path = path;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200477 return LY_SUCCESS;
478 }
Michal Vasko88ccd582023-03-30 11:50:57 +0200479 e = e->prev;
480 } while (e->prev->next);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200481 /* last error was not found */
482 assert(0);
Michal Vaskod4a6d042022-12-08 08:34:29 +0100483 } else if ((temp_ly_log_opts && ((*temp_ly_log_opts & LY_LOSTORE_LAST) == LY_LOSTORE_LAST)) ||
484 (!temp_ly_log_opts && ((ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOSTORE_LAST) == LY_LOSTORE_LAST))) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200485 /* overwrite last message */
Michal Vasko88ccd582023-03-30 11:50:57 +0200486 free(e->msg);
487 free(e->path);
488 free(e->apptag);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200489 } else {
490 /* store new message */
Michal Vasko88ccd582023-03-30 11:50:57 +0200491 last = e->prev;
492 e->prev = malloc(sizeof *e);
493 LY_CHECK_GOTO(!e->prev, mem_fail);
494 e = e->prev;
495 e->prev = last;
496 e->next = NULL;
497 last->next = e;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200498 }
499
500 /* fill in the information */
Michal Vasko88ccd582023-03-30 11:50:57 +0200501 e->level = level;
502 e->no = no;
503 e->vecode = vecode;
504 e->msg = msg;
505 e->path = path;
506 e->apptag = apptag;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200507 return LY_SUCCESS;
508
509mem_fail:
510 LOGMEM(NULL);
511 free(msg);
512 free(path);
513 free(apptag);
514 return LY_EMEM;
515}
516
517static void
Michal Vaskoe9391c72021-10-05 10:04:56 +0200518log_vprintf(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, LY_VECODE vecode, char *path, const char *apptag,
Radek Krejci0f969882020-08-21 16:56:47 +0200519 const char *format, va_list args)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200520{
521 char *msg = NULL;
Michal Vaskod4a6d042022-12-08 08:34:29 +0100522 ly_bool free_strs, lolog, lostore;
523
524 /* learn effective logger options */
525 if (temp_ly_log_opts) {
526 lolog = *temp_ly_log_opts & LY_LOLOG;
527 lostore = *temp_ly_log_opts & LY_LOSTORE;
528 } else {
529 lolog = ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOLOG;
530 lostore = ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOSTORE;
531 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200532
Václav Kubernátd367ad92021-11-29 09:28:56 +0100533 if (level > ATOMIC_LOAD_RELAXED(ly_ll)) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200534 /* do not print or store the message */
535 free(path);
536 return;
537 }
538
Michal Vasko5a016922021-05-07 08:19:15 +0200539 if (no == LY_EMEM) {
540 /* just print it, anything else would most likely fail anyway */
Michal Vaskod4a6d042022-12-08 08:34:29 +0100541 if (lolog) {
Michal Vasko5a016922021-05-07 08:19:15 +0200542 if (log_clb) {
543 log_clb(level, LY_EMEM_MSG, path);
544 } else {
545 fprintf(stderr, "libyang[%d]: ", level);
546 vfprintf(stderr, format, args);
547 if (path) {
548 fprintf(stderr, " (path: %s)\n", path);
549 } else {
550 fprintf(stderr, "\n");
551 }
552 }
553 }
554 free(path);
555 return;
556 }
557
Michal Vasko236cbac2023-02-10 15:45:37 +0100558 /* print into a single message */
559 if (vasprintf(&msg, format, args) == -1) {
560 LOGMEM(ctx);
561 free(path);
562 return;
563 }
564
565 /* store as the last message */
566 strncpy(last_msg, msg, LY_LAST_MSG_SIZE - 1);
567
568 /* store the error/warning in the context (if we need to store errors internally, it does not matter what are
569 * the user log options) */
Michal Vaskod4a6d042022-12-08 08:34:29 +0100570 if ((level < LY_LLVRB) && ctx && lostore) {
Radek Krejcic9e64a62020-09-18 20:08:12 +0200571 if (((no & ~LY_EPLUGIN) == LY_EVALID) && (vecode == LYVE_SUCCESS)) {
572 /* assume we are inheriting the error, so inherit vecode as well */
573 vecode = ly_vecode(ctx);
574 }
Michal Vaskoe9391c72021-10-05 10:04:56 +0200575 if (log_store(ctx, level, no, vecode, msg, path, apptag ? strdup(apptag) : NULL)) {
Michal Vasko004d3152020-06-11 19:59:22 +0200576 return;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200577 }
578 free_strs = 0;
579 } else {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200580 free_strs = 1;
581 }
582
583 /* if we are only storing errors internally, never print the message (yet) */
Michal Vaskod4a6d042022-12-08 08:34:29 +0100584 if (lolog) {
Michal Vaskod8085612020-08-21 12:55:23 +0200585 if (log_clb) {
586 log_clb(level, msg, path);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200587 } else {
588 fprintf(stderr, "libyang[%d]: %s%s", level, msg, path ? " " : "\n");
589 if (path) {
590 fprintf(stderr, "(path: %s)\n", path);
591 }
592 }
593 }
594
595 if (free_strs) {
596 free(path);
597 free(msg);
598 }
599}
600
Radek Krejci4ab61562018-09-05 15:00:37 +0200601#ifndef NDEBUG
602
603void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200604ly_log_dbg(uint32_t group, const char *format, ...)
Radek Krejci4ab61562018-09-05 15:00:37 +0200605{
606 char *dbg_format;
607 const char *str_group;
608 va_list ap;
609
Václav Kubernátd367ad92021-11-29 09:28:56 +0100610 if (!(ATOMIC_LOAD_RELAXED(ly_ldbg_groups) & group)) {
Radek Krejci4ab61562018-09-05 15:00:37 +0200611 return;
612 }
613
614 switch (group) {
615 case LY_LDGDICT:
616 str_group = "DICT";
617 break;
Radek Krejci4ab61562018-09-05 15:00:37 +0200618 case LY_LDGXPATH:
619 str_group = "XPATH";
620 break;
Michal Vaskoe558f792021-07-28 08:20:15 +0200621 case LY_LDGDEPSETS:
622 str_group = "DEPSETS";
623 break;
Radek Krejci4ab61562018-09-05 15:00:37 +0200624 default:
625 LOGINT(NULL);
626 return;
627 }
628
629 if (asprintf(&dbg_format, "%s: %s", str_group, format) == -1) {
630 LOGMEM(NULL);
631 return;
632 }
633
634 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200635 log_vprintf(NULL, LY_LLDBG, 0, 0, NULL, NULL, dbg_format, ap);
Radek Krejci4ab61562018-09-05 15:00:37 +0200636 va_end(ap);
637}
638
639#endif
640
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200641void
642ly_log(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, const char *format, ...)
643{
644 va_list ap;
645
646 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200647 log_vprintf(ctx, level, no, 0, NULL, NULL, format, ap);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200648 va_end(ap);
649}
650
Michal Vaskodbf3e652022-10-21 08:46:25 +0200651/**
652 * @brief Append a schema node name to a generated data path, only if it fits.
653 *
654 * @param[in,out] str Generated path to update.
655 * @param[in] snode Schema node to append.
656 * @param[in] parent Last printed data node.
657 * @return LY_ERR value.
658 */
659static LY_ERR
660ly_vlog_build_path_append(char **str, const struct lysc_node *snode, const struct lyd_node *parent)
661{
662 const struct lys_module *mod, *prev_mod;
663 uint32_t len, new_len;
664 void *mem;
665
666 if (snode->nodetype & (LYS_CHOICE | LYS_CASE)) {
667 /* schema-only node */
668 return LY_SUCCESS;
669 } else if (lysc_data_parent(snode) != parent->schema) {
670 /* not a direct descendant node */
671 return LY_SUCCESS;
672 }
673
674 /* get module to print, if any */
675 mod = snode->module;
676 prev_mod = (parent->schema) ? parent->schema->module : lyd_owner_module(parent);
677 if (prev_mod == mod) {
678 mod = NULL;
679 }
680
681 /* realloc string */
682 len = strlen(*str);
683 new_len = len + 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(snode->name);
684 mem = realloc(*str, new_len + 1);
685 LY_CHECK_ERR_RET(!mem, LOGMEM(LYD_CTX(parent)), LY_EMEM);
686 *str = mem;
687
688 /* print the last schema node */
689 sprintf(*str + len, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", snode->name);
690 return LY_SUCCESS;
691}
692
693/**
694 * @brief Build log path from the stored log location information.
695 *
696 * @param[in] ctx Context to use.
697 * @param[out] path Generated log path.
698 * @return LY_ERR value.
699 */
Radek Krejci94aa9942018-09-07 17:12:17 +0200700static LY_ERR
Radek Krejciddace2c2021-01-08 11:30:56 +0100701ly_vlog_build_path(const struct ly_ctx *ctx, char **path)
Radek Krejci94aa9942018-09-07 17:12:17 +0200702{
Michal Vaskodbf3e652022-10-21 08:46:25 +0200703 int r;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100704 char *str = NULL, *prev = NULL;
Michal Vaskodbf3e652022-10-21 08:46:25 +0200705 const struct lyd_node *dnode;
Radek Krejcicb3e6472021-01-06 08:19:01 +0100706
Radek Krejci2efc45b2020-12-22 16:25:44 +0100707 *path = NULL;
Radek Krejci94aa9942018-09-07 17:12:17 +0200708
Radek Krejciddace2c2021-01-08 11:30:56 +0100709 if (log_location.paths.count && ((const char *)(log_location.paths.objs[log_location.paths.count - 1]))[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100710 /* simply get what is in the provided path string */
Michal Vasko6727c682023-02-17 10:40:26 +0100711 r = asprintf(path, "Path \"%s\".", (const char *)log_location.paths.objs[log_location.paths.count - 1]);
712 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100713 } else {
Michal Vaskodbf3e652022-10-21 08:46:25 +0200714 /* data/schema node */
715 if (log_location.dnodes.count) {
716 dnode = log_location.dnodes.objs[log_location.dnodes.count - 1];
Michal Vaskoa4dfb3c2022-10-25 14:59:31 +0200717 if (dnode->parent || !lysc_data_parent(dnode->schema)) {
718 /* data node with all of its parents */
719 str = lyd_path(log_location.dnodes.objs[log_location.dnodes.count - 1], LYD_PATH_STD, NULL, 0);
720 LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM);
721 } else {
Michal Vaskodbf3e652022-10-21 08:46:25 +0200722 /* data parsers put all the parent nodes in the set, but they are not connected */
723 str = lyd_path_set(&log_location.dnodes, LYD_PATH_STD);
Michal Vasko3e65ee32022-10-21 10:09:51 +0200724 LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM);
Michal Vaskoa4dfb3c2022-10-25 14:59:31 +0200725 }
Michal Vaskodbf3e652022-10-21 08:46:25 +0200726
Michal Vaskoa4dfb3c2022-10-25 14:59:31 +0200727 /* sometimes the last node is not created yet and we only have the schema node */
728 if (log_location.scnodes.count) {
729 ly_vlog_build_path_append(&str, log_location.scnodes.objs[log_location.scnodes.count - 1], dnode);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200730 }
Michal Vaskodbf3e652022-10-21 08:46:25 +0200731
732 r = asprintf(path, "Data location \"%s\"", str);
733 free(str);
734 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
735 } else if (log_location.scnodes.count) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100736 str = lysc_path(log_location.scnodes.objs[log_location.scnodes.count - 1], LYSC_PATH_LOG, NULL, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100737 LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM);
738
Michal Vaskodbf3e652022-10-21 08:46:25 +0200739 r = asprintf(path, "Schema location \"%s\"", str);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100740 free(str);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200741 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100742 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100743
Michal Vaskodbf3e652022-10-21 08:46:25 +0200744 /* line */
745 prev = *path;
Radek Krejciddace2c2021-01-08 11:30:56 +0100746 if (log_location.line) {
Michal Vaskodbf3e652022-10-21 08:46:25 +0200747 r = asprintf(path, "%s%sine number %" PRIu64, prev ? prev : "", prev ? ", l" : "L", log_location.line);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100748 free(prev);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200749 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100750
Radek Krejciddace2c2021-01-08 11:30:56 +0100751 log_location.line = 0;
752 } else if (log_location.inputs.count) {
Michal Vaskodbf3e652022-10-21 08:46:25 +0200753 r = asprintf(path, "%s%sine number %" PRIu64, prev ? prev : "", prev ? ", l" : "L",
Radek Krejciddace2c2021-01-08 11:30:56 +0100754 ((struct ly_in *)log_location.inputs.objs[log_location.inputs.count - 1])->line);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100755 free(prev);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200756 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100757 }
758
759 if (*path) {
760 prev = *path;
Michal Vaskodbf3e652022-10-21 08:46:25 +0200761 r = asprintf(path, "%s.", prev);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100762 free(prev);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200763 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100764 }
Radek Krejci94aa9942018-09-07 17:12:17 +0200765 }
766
Radek Krejci94aa9942018-09-07 17:12:17 +0200767 return LY_SUCCESS;
768}
769
770void
Michal Vaskoe9391c72021-10-05 10:04:56 +0200771ly_vlog(const struct ly_ctx *ctx, const char *apptag, LY_VECODE code, const char *format, ...)
Radek Krejci94aa9942018-09-07 17:12:17 +0200772{
773 va_list ap;
Michal Vasko22df3f02020-08-24 13:29:22 +0200774 char *path = NULL;
Radek Krejci94aa9942018-09-07 17:12:17 +0200775
Václav Kubernátd367ad92021-11-29 09:28:56 +0100776 if (ATOMIC_LOAD_RELAXED(path_flag) && ctx) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100777 ly_vlog_build_path(ctx, &path);
Radek Krejci94aa9942018-09-07 17:12:17 +0200778 }
779
780 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200781 log_vprintf(ctx, LY_LLERR, LY_EVALID, code, path, apptag, format, ap);
Radek Krejci94aa9942018-09-07 17:12:17 +0200782 /* path is spent and should not be freed! */
783 va_end(ap);
784}
785
Michal Vasko193dacd2022-10-13 08:43:05 +0200786/**
787 * @brief Print a log message from an extension plugin callback.
788 *
789 * @param[in] ctx libyang context to store the error record. If not provided, the error is just printed.
790 * @param[in] plugin_name Name of the plugin generating the message.
791 * @param[in] level Log message level (error, warning, etc.)
792 * @param[in] err_no Error type code.
Michal Vasko6727c682023-02-17 10:40:26 +0100793 * @param[in] path Optional path of the error, used if set.
Michal Vasko193dacd2022-10-13 08:43:05 +0200794 * @param[in] format Format string to print.
795 * @param[in] ap Var arg list for @p format.
796 */
797static void
Michal Vasko6727c682023-02-17 10:40:26 +0100798ly_ext_log(const struct ly_ctx *ctx, const char *plugin_name, LY_LOG_LEVEL level, LY_ERR err_no, char *path,
Michal Vasko193dacd2022-10-13 08:43:05 +0200799 const char *format, va_list ap)
Radek Krejci0935f412019-08-20 16:15:18 +0200800{
Radek Krejci0935f412019-08-20 16:15:18 +0200801 char *plugin_msg;
Radek Krejci0935f412019-08-20 16:15:18 +0200802
Václav Kubernátd367ad92021-11-29 09:28:56 +0100803 if (ATOMIC_LOAD_RELAXED(ly_ll) < level) {
Radek Krejci0935f412019-08-20 16:15:18 +0200804 return;
805 }
Michal Vasko193dacd2022-10-13 08:43:05 +0200806 if (asprintf(&plugin_msg, "Ext plugin \"%s\": %s", plugin_name, format) == -1) {
807 LOGMEM(ctx);
Radek Krejci0935f412019-08-20 16:15:18 +0200808 return;
809 }
810
Michal Vasko6727c682023-02-17 10:40:26 +0100811 log_vprintf(ctx, level, (level == LY_LLERR ? LY_EPLUGIN : 0) | err_no, LYVE_OTHER, path, NULL, plugin_msg, ap);
Michal Vasko193dacd2022-10-13 08:43:05 +0200812 free(plugin_msg);
813}
814
815LIBYANG_API_DEF void
816lyplg_ext_parse_log(const struct lysp_ctx *pctx, const struct lysp_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no,
817 const char *format, ...)
818{
819 va_list ap;
820 char *path = NULL;
821
822 if (ATOMIC_LOAD_RELAXED(path_flag)) {
823 ly_vlog_build_path(PARSER_CTX(pctx), &path);
824 }
825
Radek Krejci0935f412019-08-20 16:15:18 +0200826 va_start(ap, format);
Michal Vasko193dacd2022-10-13 08:43:05 +0200827 ly_ext_log(PARSER_CTX(pctx), ext->record->plugin.id, level, err_no, path, format, ap);
Radek Krejci0935f412019-08-20 16:15:18 +0200828 va_end(ap);
Michal Vasko193dacd2022-10-13 08:43:05 +0200829}
830
831LIBYANG_API_DEF void
832lyplg_ext_compile_log(const struct lysc_ctx *cctx, const struct lysc_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no,
833 const char *format, ...)
834{
835 va_list ap;
Michal Vasko6727c682023-02-17 10:40:26 +0100836 char *path = NULL;
837
838 if (cctx && (asprintf(&path, "Path \"%s\".", cctx->path) == -1)) {
839 LOGMEM(cctx->ctx);
840 return;
841 }
Michal Vasko193dacd2022-10-13 08:43:05 +0200842
843 va_start(ap, format);
Michal Vasko6727c682023-02-17 10:40:26 +0100844 ly_ext_log(ext->module->ctx, ext->def->plugin->id, level, err_no, path, format, ap);
Michal Vasko193dacd2022-10-13 08:43:05 +0200845 va_end(ap);
846}
847
848LIBYANG_API_DEF void
849lyplg_ext_compile_log_path(const char *path, const struct lysc_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no,
850 const char *format, ...)
851{
852 va_list ap;
Michal Vasko6727c682023-02-17 10:40:26 +0100853 char *log_path = NULL;
854
855 if (path && (asprintf(&log_path, "Path \"%s\".", path) == -1)) {
856 LOGMEM(ext->module->ctx);
857 return;
858 }
Michal Vasko193dacd2022-10-13 08:43:05 +0200859
860 va_start(ap, format);
Michal Vasko6727c682023-02-17 10:40:26 +0100861 ly_ext_log(ext->module->ctx, ext->def->plugin->id, level, err_no, log_path, format, ap);
Michal Vasko193dacd2022-10-13 08:43:05 +0200862 va_end(ap);
Radek Krejci0935f412019-08-20 16:15:18 +0200863}
864
Michal Vasko177d0ed2020-11-23 16:43:03 +0100865/**
Michal Vasko6727c682023-02-17 10:40:26 +0100866 * @brief Serves only for creating ap.
867 */
868static void
869_lyplg_ext_compile_log_err(const struct ly_err_item *err, const struct lysc_ext_instance *ext, ...)
870{
871 va_list ap;
872
873 va_start(ap, ext);
874 ly_ext_log(ext->module->ctx, ext->def->plugin->id, err->level, err->no, err->path ? strdup(err->path) : NULL, "%s", ap);
875 va_end(ap);
876}
877
878LIBYANG_API_DEF void
879lyplg_ext_compile_log_err(const struct ly_err_item *err, const struct lysc_ext_instance *ext)
880{
881 _lyplg_ext_compile_log_err(err, ext, err->msg);
882}
883
884/**
Michal Vaskoc78a6092021-05-07 15:27:35 +0200885 * @brief Exact same functionality as ::ly_err_print() but has variable arguments so log_vprintf() can be called.
Michal Vasko177d0ed2020-11-23 16:43:03 +0100886 */
887static void
Michal Vaskoc78a6092021-05-07 15:27:35 +0200888_ly_err_print(const struct ly_ctx *ctx, struct ly_err_item *eitem, const char *format, ...)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200889{
Michal Vasko177d0ed2020-11-23 16:43:03 +0100890 va_list ap;
891 char *path_dup = NULL;
892
893 LY_CHECK_ARG_RET(ctx, eitem, );
894
895 if (eitem->path) {
896 /* duplicate path because it will be freed */
897 path_dup = strdup(eitem->path);
898 LY_CHECK_ERR_RET(!path_dup, LOGMEM(ctx), );
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200899 }
Michal Vasko177d0ed2020-11-23 16:43:03 +0100900
Michal Vaskoc78a6092021-05-07 15:27:35 +0200901 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200902 log_vprintf(ctx, eitem->level, eitem->no, eitem->vecode, path_dup, eitem->apptag, format, ap);
Michal Vasko177d0ed2020-11-23 16:43:03 +0100903 va_end(ap);
Michal Vasko177d0ed2020-11-23 16:43:03 +0100904}
905
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100906LIBYANG_API_DEF void
Michal Vasko177d0ed2020-11-23 16:43:03 +0100907ly_err_print(const struct ly_ctx *ctx, struct ly_err_item *eitem)
908{
Michal Vaskoc78a6092021-05-07 15:27:35 +0200909 /* String ::ly_err_item.msg cannot be used directly because it may contain the % character */
910 _ly_err_print(ctx, eitem, "%s", eitem->msg);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200911}