blob: f936abfee215617ad1ffbd7a1e8c0d355ad16558 [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 Vaskob5b883c2023-07-10 10:36:18 +02007 * Copyright (c) 2015 - 2023 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 Krejciaa45bda2020-07-20 07:43:38 +020029#include "compat.h"
Radek Krejciaddfc9a2020-12-17 20:46:35 +010030#include "in_internal.h"
Michal Vasko8f702ee2024-02-20 15:44:24 +010031#include "ly_common.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;
Michal Vasko236cbac2023-02-10 15:45:37 +010043THREAD_LOCAL char last_msg[LY_LAST_MSG_SIZE];
Radek Krejci5aeea3a2018-09-05 13:29:36 +020044#ifndef NDEBUG
Václav Kubernátd367ad92021-11-29 09:28:56 +010045ATOMIC_T ly_ldbg_groups = 0;
Radek Krejci5aeea3a2018-09-05 13:29:36 +020046#endif
47
Radek Krejciddace2c2021-01-08 11:30:56 +010048THREAD_LOCAL struct ly_log_location_s log_location = {0};
49
Michal Vaskob5b883c2023-07-10 10:36:18 +020050LIBYANG_API_DEF const char *
Michal Vasko7a266772024-01-23 11:02:38 +010051ly_strerr(LY_ERR err)
Michal Vaskob5b883c2023-07-10 10:36:18 +020052{
53 /* ignore plugin flag */
54 err &= ~LY_EPLUGIN;
55
56 switch (err) {
57 case LY_SUCCESS:
Michal Vaskoe4207652023-07-10 14:57:32 +020058 return "Success";
Michal Vaskob5b883c2023-07-10 10:36:18 +020059 case LY_EMEM:
60 return "Out of memory";
61 case LY_ESYS:
62 return "System call failed";
63 case LY_EINVAL:
64 return "Invalid value";
65 case LY_EEXIST:
66 return "Already exists";
67 case LY_ENOTFOUND:
68 return "Not found";
69 case LY_EINT:
70 return "Internal error";
71 case LY_EVALID:
72 return "Validation failed";
73 case LY_EDENIED:
74 return "Operation denied";
75 case LY_EINCOMPLETE:
76 return "Operation incomplete";
77 case LY_ERECOMPILE:
78 return "Recompilation required";
79 case LY_ENOT:
80 return "Negative result";
81 case LY_EOTHER:
82 return "Another failure reason";
83 case LY_EPLUGIN:
84 break;
85 }
86
87 /* unreachable */
Michal Vaskob87e9a12023-07-10 15:10:58 +020088 return "Unknown";
Michal Vaskob5b883c2023-07-10 10:36:18 +020089}
90
Jan Kundrátc53a7ec2021-12-09 16:01:19 +010091LIBYANG_API_DEF const char *
Michal Vaskob5b883c2023-07-10 10:36:18 +020092ly_strvecode(LY_VECODE vecode)
93{
94 switch (vecode) {
95 case LYVE_SUCCESS:
Michal Vaskoe4207652023-07-10 14:57:32 +020096 return "Success";
Michal Vaskob5b883c2023-07-10 10:36:18 +020097 case LYVE_SYNTAX:
98 return "General syntax error";
99 case LYVE_SYNTAX_YANG:
100 return "YANG syntax error";
101 case LYVE_SYNTAX_YIN:
102 return "YIN syntax error";
103 case LYVE_REFERENCE:
104 return "Reference error";
105 case LYVE_XPATH:
106 return "XPath error";
107 case LYVE_SEMANTICS:
108 return "Semantic error";
109 case LYVE_SYNTAX_XML:
110 return "XML syntax error";
111 case LYVE_SYNTAX_JSON:
112 return "JSON syntax error";
113 case LYVE_DATA:
114 return "YANG data error";
115 case LYVE_OTHER:
116 return "Another error";
117 }
118
119 /* unreachable */
Michal Vaskob87e9a12023-07-10 15:10:58 +0200120 return "Unknown";
Michal Vaskob5b883c2023-07-10 10:36:18 +0200121}
122
123LIBYANG_API_DEF const char *
Michal Vasko7a266772024-01-23 11:02:38 +0100124ly_last_logmsg(void)
Michal Vaskob5b883c2023-07-10 10:36:18 +0200125{
126 return last_msg;
127}
128
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100129LIBYANG_API_DEF LY_ERR
Michal Vasko7a266772024-01-23 11:02:38 +0100130ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *data_path, char *apptag,
131 const char *err_format, ...)
Radek Krejcie7b95092019-05-15 11:03:07 +0200132{
Radek Krejcidb0ee022021-03-15 16:53:44 +0100133 char *msg = NULL;
134 struct ly_err_item *e;
Radek Krejcie7b95092019-05-15 11:03:07 +0200135
Radek Krejcid43298b2021-03-25 16:17:15 +0100136 if (!err || (ecode == LY_SUCCESS)) {
Radek Krejcidb0ee022021-03-15 16:53:44 +0100137 /* nothing to do */
138 return ecode;
139 }
140
Michal Vasko7a266772024-01-23 11:02:38 +0100141 e = calloc(1, sizeof *e);
Radek Krejcidb0ee022021-03-15 16:53:44 +0100142 LY_CHECK_ERR_RET(!e, LOGMEM(NULL), LY_EMEM);
Michal Vasko7a266772024-01-23 11:02:38 +0100143
Radek Krejcidb0ee022021-03-15 16:53:44 +0100144 e->prev = (*err) ? (*err)->prev : e;
Radek Krejcidb0ee022021-03-15 16:53:44 +0100145 if (*err) {
146 (*err)->prev->next = e;
147 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200148
149 /* fill in the information */
Radek Krejcidb0ee022021-03-15 16:53:44 +0100150 e->level = LY_LLERR;
Michal Vasko7a266772024-01-23 11:02:38 +0100151 e->err = ecode;
Radek Krejcidb0ee022021-03-15 16:53:44 +0100152 e->vecode = vecode;
Michal Vasko7a266772024-01-23 11:02:38 +0100153 e->data_path = data_path;
Radek Krejcidb0ee022021-03-15 16:53:44 +0100154 e->apptag = apptag;
Radek Krejcie7b95092019-05-15 11:03:07 +0200155
aPiecek6d618552021-06-18 10:02:59 +0200156 if (err_format) {
Radek Krejcidb0ee022021-03-15 16:53:44 +0100157 va_list print_args;
158
aPiecek6d618552021-06-18 10:02:59 +0200159 va_start(print_args, err_format);
Radek Krejcidb0ee022021-03-15 16:53:44 +0100160
aPiecek6d618552021-06-18 10:02:59 +0200161 if (vasprintf(&msg, err_format, print_args) == -1) {
162 /* we don't have anything more to do, just set msg to NULL to avoid undefined content,
Radek Krejcidb0ee022021-03-15 16:53:44 +0100163 * still keep the information about the original error instead of LY_EMEM or other printf's error */
164 msg = NULL;
165 }
166
167 va_end(print_args);
168 }
169 e->msg = msg;
170
171 if (!(*err)) {
172 *err = e;
173 }
174
Michal Vasko7a266772024-01-23 11:02:38 +0100175 return e->err;
Radek Krejcie7b95092019-05-15 11:03:07 +0200176}
177
Michal Vasko88ccd582023-03-30 11:50:57 +0200178/**
179 * @brief Get error record from error hash table of a context for the current thread.
180 *
181 * @param[in] ctx Context to use.
182 * @return Thread error record, if any.
183 */
184static struct ly_ctx_err_rec *
185ly_err_get_rec(const struct ly_ctx *ctx)
186{
Michal Vasko4cca0092023-03-30 13:20:53 +0200187 struct ly_ctx_err_rec rec, *match;
Michal Vasko88ccd582023-03-30 11:50:57 +0200188
189 /* prepare record */
190 rec.tid = pthread_self();
191
192 /* get the pointer to the matching record */
Michal Vaskoae130f52023-04-20 14:25:16 +0200193 if (lyht_find(ctx->err_ht, &rec, lyht_hash((void *)&rec.tid, sizeof rec.tid), (void **)&match)) {
Michal Vasko4cca0092023-03-30 13:20:53 +0200194 return NULL;
195 }
Michal Vasko88ccd582023-03-30 11:50:57 +0200196
197 return match;
198}
199
Michal Vaskoe0be7452023-03-30 13:35:35 +0200200/**
201 * @brief Insert new error record to error hash table of a context for the current thread.
202 *
203 * @param[in] ctx Context to use.
204 * @return Thread error record.
205 */
206static struct ly_ctx_err_rec *
207ly_err_new_rec(const struct ly_ctx *ctx)
208{
209 struct ly_ctx_err_rec new, *rec;
210 LY_ERR r;
211
212 /* insert a new record */
213 new.err = NULL;
214 new.tid = pthread_self();
215
216 /* reuse lock */
217 /* LOCK */
218 pthread_mutex_lock((pthread_mutex_t *)&ctx->lyb_hash_lock);
219
Michal Vaskoae130f52023-04-20 14:25:16 +0200220 r = lyht_insert(ctx->err_ht, &new, lyht_hash((void *)&new.tid, sizeof new.tid), (void **)&rec);
Michal Vaskoe0be7452023-03-30 13:35:35 +0200221
222 /* UNLOCK */
223 pthread_mutex_unlock((pthread_mutex_t *)&ctx->lyb_hash_lock);
224
225 return r ? NULL : rec;
226}
227
Michal Vasko7a266772024-01-23 11:02:38 +0100228LIBYANG_API_DEF const struct ly_err_item *
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200229ly_err_first(const struct ly_ctx *ctx)
230{
Michal Vasko88ccd582023-03-30 11:50:57 +0200231 struct ly_ctx_err_rec *rec;
232
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200233 LY_CHECK_ARG_RET(NULL, ctx, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200234
Michal Vasko88ccd582023-03-30 11:50:57 +0200235 /* get the pointer to the matching record */
236 rec = ly_err_get_rec(ctx);
237
238 return rec ? rec->err : NULL;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200239}
240
Michal Vasko7a266772024-01-23 11:02:38 +0100241LIBYANG_API_DEF const struct ly_err_item *
Radek Krejci572ee602020-09-16 14:35:08 +0200242ly_err_last(const struct ly_ctx *ctx)
243{
Michal Vasko88ccd582023-03-30 11:50:57 +0200244 struct ly_ctx_err_rec *rec;
Radek Krejci572ee602020-09-16 14:35:08 +0200245
246 LY_CHECK_ARG_RET(NULL, ctx, NULL);
247
Michal Vasko88ccd582023-03-30 11:50:57 +0200248 /* get the pointer to the matching record */
249 if (!(rec = ly_err_get_rec(ctx))) {
250 return NULL;
251 }
252
253 return rec->err ? rec->err->prev : NULL;
Radek Krejci572ee602020-09-16 14:35:08 +0200254}
255
Michal Vasko9dbb91d2023-01-30 13:59:22 +0100256void
257ly_err_move(struct ly_ctx *src_ctx, struct ly_ctx *trg_ctx)
258{
Michal Vasko88ccd582023-03-30 11:50:57 +0200259 struct ly_ctx_err_rec *rec;
260 struct ly_err_item *err = NULL;
Michal Vasko9dbb91d2023-01-30 13:59:22 +0100261
Michal Vasko88ccd582023-03-30 11:50:57 +0200262 /* get and remove the errors from src */
263 rec = ly_err_get_rec(src_ctx);
264 if (rec) {
265 err = rec->err;
266 rec->err = NULL;
267 }
Michal Vasko9dbb91d2023-01-30 13:59:22 +0100268
269 /* set them for trg */
Michal Vaskoe0be7452023-03-30 13:35:35 +0200270 if (!(rec = ly_err_get_rec(trg_ctx))) {
271 if (!(rec = ly_err_new_rec(trg_ctx))) {
272 LOGINT(NULL);
273 ly_err_free(err);
274 return;
275 }
276 }
Michal Vasko88ccd582023-03-30 11:50:57 +0200277 ly_err_free(rec->err);
278 rec->err = err;
Michal Vasko9dbb91d2023-01-30 13:59:22 +0100279}
280
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100281LIBYANG_API_DEF void
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200282ly_err_free(void *ptr)
283{
Michal Vasko88ccd582023-03-30 11:50:57 +0200284 struct ly_err_item *e, *next;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200285
286 /* clean the error list */
Michal Vasko88ccd582023-03-30 11:50:57 +0200287 LY_LIST_FOR_SAFE(ptr, next, e) {
288 free(e->msg);
Michal Vasko7a266772024-01-23 11:02:38 +0100289 free(e->data_path);
290 free(e->schema_path);
Michal Vasko88ccd582023-03-30 11:50:57 +0200291 free(e->apptag);
292 free(e);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200293 }
294}
295
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100296LIBYANG_API_DEF void
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200297ly_err_clean(struct ly_ctx *ctx, struct ly_err_item *eitem)
298{
Michal Vasko88ccd582023-03-30 11:50:57 +0200299 struct ly_ctx_err_rec *rec;
300 struct ly_err_item *e;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200301
Michal Vasko88ccd582023-03-30 11:50:57 +0200302 if (!(rec = ly_err_get_rec(ctx))) {
303 return;
304 }
305 if (rec->err == eitem) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200306 eitem = NULL;
307 }
Michal Vasko88ccd582023-03-30 11:50:57 +0200308
309 if (!eitem) {
310 /* free all err */
311 ly_err_free(rec->err);
312 rec->err = NULL;
313 } else {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200314 /* disconnect the error */
Michal Vasko88ccd582023-03-30 11:50:57 +0200315 for (e = rec->err; e && (e->next != eitem); e = e->next) {}
316 assert(e);
317 e->next = NULL;
318 rec->err->prev = e;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200319 /* free this err and newer */
320 ly_err_free(eitem);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200321 }
322}
323
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100324LIBYANG_API_DEF LY_LOG_LEVEL
Radek Krejci52b6d512020-10-12 12:33:17 +0200325ly_log_level(LY_LOG_LEVEL level)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200326{
Václav Kubernátd367ad92021-11-29 09:28:56 +0100327 LY_LOG_LEVEL prev = ATOMIC_LOAD_RELAXED(ly_ll);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200328
Václav Kubernátd367ad92021-11-29 09:28:56 +0100329 ATOMIC_STORE_RELAXED(ly_ll, level);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200330 return prev;
331}
332
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100333LIBYANG_API_DEF uint32_t
Radek Krejci1deb5be2020-08-26 16:43:36 +0200334ly_log_options(uint32_t opts)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200335{
Václav Kubernátd367ad92021-11-29 09:28:56 +0100336 uint32_t prev = ATOMIC_LOAD_RELAXED(ly_log_opts);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200337
Václav Kubernátd367ad92021-11-29 09:28:56 +0100338 ATOMIC_STORE_RELAXED(ly_log_opts, opts);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200339 return prev;
340}
341
Michal Vasko52da51d2024-01-30 16:09:19 +0100342LIBYANG_API_DEF uint32_t *
Michal Vaskod4a6d042022-12-08 08:34:29 +0100343ly_temp_log_options(uint32_t *opts)
344{
Michal Vasko52da51d2024-01-30 16:09:19 +0100345 uint32_t *prev_lo = temp_ly_log_opts;
346
Michal Vaskod4a6d042022-12-08 08:34:29 +0100347 temp_ly_log_opts = opts;
Michal Vasko52da51d2024-01-30 16:09:19 +0100348
349 return prev_lo;
Michal Vaskod4a6d042022-12-08 08:34:29 +0100350}
351
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100352LIBYANG_API_DEF uint32_t
Radek Krejci68433c92020-10-12 17:03:55 +0200353ly_log_dbg_groups(uint32_t dbg_groups)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200354{
355#ifndef NDEBUG
Václav Kubernátd367ad92021-11-29 09:28:56 +0100356 uint32_t prev = ATOMIC_LOAD_RELAXED(ly_ldbg_groups);
Radek Krejciebdaed02020-11-09 13:05:06 +0100357
Václav Kubernátd367ad92021-11-29 09:28:56 +0100358 ATOMIC_STORE_RELAXED(ly_ldbg_groups, dbg_groups);
Radek Krejciebdaed02020-11-09 13:05:06 +0100359 return prev;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200360#else
361 (void)dbg_groups;
Radek Krejciebdaed02020-11-09 13:05:06 +0100362 return 0;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200363#endif
364}
365
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100366LIBYANG_API_DEF void
Michal Vasko7a266772024-01-23 11:02:38 +0100367ly_set_log_clb(ly_log_clb clb)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200368{
Michal Vaskod8085612020-08-21 12:55:23 +0200369 log_clb = clb;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200370}
371
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100372LIBYANG_API_DEF ly_log_clb
Michal Vaskod8085612020-08-21 12:55:23 +0200373ly_get_log_clb(void)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200374{
Michal Vaskod8085612020-08-21 12:55:23 +0200375 return log_clb;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200376}
377
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100378void
Michal Vasko7a266772024-01-23 11:02:38 +0100379ly_log_location(const struct lysc_node *scnode, const struct lyd_node *dnode, const char *spath, const struct ly_in *in)
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100380{
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100381 if (scnode) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100382 ly_set_add(&log_location.scnodes, (void *)scnode, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100383 }
Michal Vasko7a266772024-01-23 11:02:38 +0100384 if (dnode || (!scnode && !spath && !in)) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100385 ly_set_add(&log_location.dnodes, (void *)dnode, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100386 }
Michal Vasko7a266772024-01-23 11:02:38 +0100387 if (spath) {
388 char *s = strdup(spath);
Michal Vasko26bbb272022-08-02 14:54:33 +0200389
Radek Krejciddace2c2021-01-08 11:30:56 +0100390 LY_CHECK_ERR_RET(!s, LOGMEM(NULL), );
Michal Vasko7a266772024-01-23 11:02:38 +0100391 ly_set_add(&log_location.spaths, s, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100392 }
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100393 if (in) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100394 ly_set_add(&log_location.inputs, (void *)in, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100395 }
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100396}
397
398void
Michal Vasko7a266772024-01-23 11:02:38 +0100399ly_log_location_revert(uint32_t scnode_steps, uint32_t dnode_steps, uint32_t spath_steps, uint32_t in_steps)
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100400{
Radek Krejciddace2c2021-01-08 11:30:56 +0100401 for (uint32_t i = scnode_steps; i && log_location.scnodes.count; i--) {
402 log_location.scnodes.count--;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100403 }
404
Radek Krejciddace2c2021-01-08 11:30:56 +0100405 for (uint32_t i = dnode_steps; i && log_location.dnodes.count; i--) {
406 log_location.dnodes.count--;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100407 }
408
Michal Vasko7a266772024-01-23 11:02:38 +0100409 for (uint32_t i = spath_steps; i && log_location.spaths.count; i--) {
410 ly_set_rm_index(&log_location.spaths, log_location.spaths.count - 1, free);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100411 }
412
Radek Krejciddace2c2021-01-08 11:30:56 +0100413 for (uint32_t i = in_steps; i && log_location.inputs.count; i--) {
414 log_location.inputs.count--;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100415 }
416
Radek Krejciddace2c2021-01-08 11:30:56 +0100417 /* deallocate the empty sets */
418 if (scnode_steps && !log_location.scnodes.count) {
419 ly_set_erase(&log_location.scnodes, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100420 }
Radek Krejciddace2c2021-01-08 11:30:56 +0100421 if (dnode_steps && !log_location.dnodes.count) {
422 ly_set_erase(&log_location.dnodes, NULL);
423 }
Michal Vasko7a266772024-01-23 11:02:38 +0100424 if (spath_steps && !log_location.spaths.count) {
425 ly_set_erase(&log_location.spaths, free);
Radek Krejciddace2c2021-01-08 11:30:56 +0100426 }
427 if (in_steps && !log_location.inputs.count) {
428 ly_set_erase(&log_location.inputs, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100429 }
430}
431
Michal Vaskoa878a892023-08-18 12:22:07 +0200432const struct lyd_node *
433ly_log_location_dnode(uint32_t idx)
434{
435 if (idx < log_location.dnodes.count) {
436 return log_location.dnodes.dnodes[idx];
437 }
438
439 return NULL;
440}
441
442uint32_t
443ly_log_location_dnode_count(void)
444{
445 return log_location.dnodes.count;
446}
447
Michal Vasko88ccd582023-03-30 11:50:57 +0200448/**
449 * @brief Store generated error in a context.
450 *
451 * @param[in] ctx Context to use.
452 * @param[in] level Message log level.
Michal Vasko7a266772024-01-23 11:02:38 +0100453 * @param[in] err Error number.
Michal Vasko88ccd582023-03-30 11:50:57 +0200454 * @param[in] vecode Error validation error code.
455 * @param[in] msg Error message, always spent.
Michal Vasko7a266772024-01-23 11:02:38 +0100456 * @param[in] data_path Error data path, always spent.
457 * @param[in] schema_path Error schema path, always spent.
458 * @param[in] line Error input line, if any.
Michal Vasko88ccd582023-03-30 11:50:57 +0200459 * @param[in] apptag Error app tag, always spent.
460 * @return LY_ERR value.
461 */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200462static LY_ERR
Michal Vasko7a266772024-01-23 11:02:38 +0100463log_store(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR err, LY_VECODE vecode, char *msg, char *data_path,
464 char *schema_path, uint64_t line, char *apptag)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200465{
Michal Vaskoe0be7452023-03-30 13:35:35 +0200466 struct ly_ctx_err_rec *rec;
Michal Vasko88ccd582023-03-30 11:50:57 +0200467 struct ly_err_item *e, *last;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200468
469 assert(ctx && (level < LY_LLVRB));
470
Michal Vasko88ccd582023-03-30 11:50:57 +0200471 if (!(rec = ly_err_get_rec(ctx))) {
Michal Vaskoe0be7452023-03-30 13:35:35 +0200472 if (!(rec = ly_err_new_rec(ctx))) {
473 goto mem_fail;
Michal Vasko88ccd582023-03-30 11:50:57 +0200474 }
475 }
476
477 e = rec->err;
478 if (!e) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200479 /* if we are only to fill in path, there must have been an error stored */
480 assert(msg);
Michal Vasko7a266772024-01-23 11:02:38 +0100481 e = calloc(1, sizeof *e);
Michal Vasko88ccd582023-03-30 11:50:57 +0200482 LY_CHECK_GOTO(!e, mem_fail);
483 e->prev = e;
484 e->next = NULL;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200485
Michal Vasko88ccd582023-03-30 11:50:57 +0200486 rec->err = e;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200487 } else if (!msg) {
488 /* only filling the path */
Michal Vasko7a266772024-01-23 11:02:38 +0100489 assert(data_path || schema_path);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200490
491 /* find last error */
Michal Vasko88ccd582023-03-30 11:50:57 +0200492 e = e->prev;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200493 do {
Michal Vasko88ccd582023-03-30 11:50:57 +0200494 if (e->level == LY_LLERR) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200495 /* fill the path */
Michal Vasko7a266772024-01-23 11:02:38 +0100496 if (data_path) {
497 free(e->data_path);
498 e->data_path = data_path;
499 } else {
500 free(e->schema_path);
501 e->schema_path = schema_path;
502 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200503 return LY_SUCCESS;
504 }
Michal Vasko88ccd582023-03-30 11:50:57 +0200505 e = e->prev;
506 } while (e->prev->next);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200507 /* last error was not found */
508 assert(0);
Michal Vaskod4a6d042022-12-08 08:34:29 +0100509 } else if ((temp_ly_log_opts && ((*temp_ly_log_opts & LY_LOSTORE_LAST) == LY_LOSTORE_LAST)) ||
510 (!temp_ly_log_opts && ((ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOSTORE_LAST) == LY_LOSTORE_LAST))) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200511 /* overwrite last message */
Michal Vasko88ccd582023-03-30 11:50:57 +0200512 free(e->msg);
Michal Vasko7a266772024-01-23 11:02:38 +0100513 free(e->data_path);
514 free(e->schema_path);
Michal Vasko88ccd582023-03-30 11:50:57 +0200515 free(e->apptag);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200516 } else {
517 /* store new message */
Michal Vasko88ccd582023-03-30 11:50:57 +0200518 last = e->prev;
Michal Vasko7a266772024-01-23 11:02:38 +0100519 e->prev = calloc(1, sizeof *e);
Michal Vasko88ccd582023-03-30 11:50:57 +0200520 LY_CHECK_GOTO(!e->prev, mem_fail);
521 e = e->prev;
522 e->prev = last;
523 e->next = NULL;
524 last->next = e;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200525 }
526
527 /* fill in the information */
Michal Vasko88ccd582023-03-30 11:50:57 +0200528 e->level = level;
Michal Vasko7a266772024-01-23 11:02:38 +0100529 e->err = err;
Michal Vasko88ccd582023-03-30 11:50:57 +0200530 e->vecode = vecode;
531 e->msg = msg;
Michal Vasko7a266772024-01-23 11:02:38 +0100532 e->data_path = data_path;
533 e->schema_path = schema_path;
534 e->line = line;
Michal Vasko88ccd582023-03-30 11:50:57 +0200535 e->apptag = apptag;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200536 return LY_SUCCESS;
537
538mem_fail:
539 LOGMEM(NULL);
540 free(msg);
Michal Vasko7a266772024-01-23 11:02:38 +0100541 free(data_path);
542 free(schema_path);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200543 free(apptag);
544 return LY_EMEM;
545}
546
Michal Vasko7a266772024-01-23 11:02:38 +0100547/**
548 * @brief Log data path/schema path/line to stderr after the message has been printed.
549 *
550 * @param[in] data_path Error data path.
551 * @param[in] schema_path Error schema path.
552 * @param[in] line Error input line.
553 */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200554static void
Michal Vasko7a266772024-01-23 11:02:38 +0100555log_stderr_path_line(const char *data_path, const char *schema_path, uint64_t line)
556{
557 ly_bool par = 0;
558
559 if (data_path) {
560 fprintf(stderr, "%sdata path: %s", " (", data_path);
561 par = 1;
562 }
563
564 if (schema_path) {
565 fprintf(stderr, "%sschemadata path: %s", par ? ", " : " (", schema_path);
566 par = 1;
567 }
568
569 if (line) {
570 fprintf(stderr, "%sline: %" PRIu64, par ? ", " : " (", line);
571 par = 1;
572 }
573
574 fprintf(stderr, par ? ")\n" : "\n");
575}
576
577/**
578 * @brief Log a message.
579 *
580 * @param[in] ctx Context to use.
581 * @param[in] level Message log level.
582 * @param[in] err Error number.
583 * @param[in] vecode Error validation error code.
584 * @param[in] data_path Error data path, always spent.
585 * @param[in] schema_path Error schema path, always spent.
586 * @param[in] line Error input line, if any.
587 * @param[in] apptag Error app tag.
588 * @param[in] format Error message format.
589 * @param[in] args Error message format arguments.
590 */
591static void
592log_vprintf(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR err, LY_VECODE vecode, char *data_path,
593 char *schema_path, uint64_t line, const char *apptag, const char *format, va_list args)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200594{
595 char *msg = NULL;
Michal Vasko7a266772024-01-23 11:02:38 +0100596 ly_bool free_strs = 1, lolog, lostore;
Michal Vaskod4a6d042022-12-08 08:34:29 +0100597
598 /* learn effective logger options */
599 if (temp_ly_log_opts) {
600 lolog = *temp_ly_log_opts & LY_LOLOG;
601 lostore = *temp_ly_log_opts & LY_LOSTORE;
602 } else {
603 lolog = ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOLOG;
604 lostore = ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOSTORE;
605 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200606
Václav Kubernátd367ad92021-11-29 09:28:56 +0100607 if (level > ATOMIC_LOAD_RELAXED(ly_ll)) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200608 /* do not print or store the message */
Michal Vasko7a266772024-01-23 11:02:38 +0100609 goto cleanup;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200610 }
611
Michal Vasko7a266772024-01-23 11:02:38 +0100612 if (err == LY_EMEM) {
Michal Vasko5a016922021-05-07 08:19:15 +0200613 /* just print it, anything else would most likely fail anyway */
Michal Vaskod4a6d042022-12-08 08:34:29 +0100614 if (lolog) {
Michal Vasko5a016922021-05-07 08:19:15 +0200615 if (log_clb) {
Michal Vasko7a266772024-01-23 11:02:38 +0100616 log_clb(level, LY_EMEM_MSG, data_path, schema_path, line);
Michal Vasko5a016922021-05-07 08:19:15 +0200617 } else {
618 fprintf(stderr, "libyang[%d]: ", level);
619 vfprintf(stderr, format, args);
Michal Vasko7a266772024-01-23 11:02:38 +0100620 log_stderr_path_line(data_path, schema_path, line);
Michal Vasko5a016922021-05-07 08:19:15 +0200621 }
622 }
Michal Vasko7a266772024-01-23 11:02:38 +0100623 goto cleanup;
Michal Vasko5a016922021-05-07 08:19:15 +0200624 }
625
Michal Vasko236cbac2023-02-10 15:45:37 +0100626 /* print into a single message */
627 if (vasprintf(&msg, format, args) == -1) {
628 LOGMEM(ctx);
Michal Vasko7a266772024-01-23 11:02:38 +0100629 goto cleanup;
Michal Vasko236cbac2023-02-10 15:45:37 +0100630 }
631
632 /* store as the last message */
633 strncpy(last_msg, msg, LY_LAST_MSG_SIZE - 1);
634
635 /* store the error/warning in the context (if we need to store errors internally, it does not matter what are
636 * the user log options) */
Michal Vaskod4a6d042022-12-08 08:34:29 +0100637 if ((level < LY_LLVRB) && ctx && lostore) {
Michal Vaskofcd168e2024-02-27 09:16:21 +0100638 free_strs = 0;
Michal Vasko7a266772024-01-23 11:02:38 +0100639 if (log_store(ctx, level, err, vecode, msg, data_path, schema_path, line, apptag ? strdup(apptag) : NULL)) {
640 goto cleanup;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200641 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200642 }
643
644 /* if we are only storing errors internally, never print the message (yet) */
Michal Vaskod4a6d042022-12-08 08:34:29 +0100645 if (lolog) {
Michal Vaskod8085612020-08-21 12:55:23 +0200646 if (log_clb) {
Michal Vasko7a266772024-01-23 11:02:38 +0100647 log_clb(level, msg, data_path, schema_path, line);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200648 } else {
Michal Vasko7a266772024-01-23 11:02:38 +0100649 fprintf(stderr, "libyang[%d]: ", level);
650 fprintf(stderr, "%s", msg);
651 log_stderr_path_line(data_path, schema_path, line);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200652 }
653 }
654
Michal Vasko7a266772024-01-23 11:02:38 +0100655cleanup:
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200656 if (free_strs) {
Michal Vasko7a266772024-01-23 11:02:38 +0100657 free(data_path);
658 free(schema_path);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200659 free(msg);
660 }
661}
662
Radek Krejci4ab61562018-09-05 15:00:37 +0200663#ifndef NDEBUG
664
665void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200666ly_log_dbg(uint32_t group, const char *format, ...)
Radek Krejci4ab61562018-09-05 15:00:37 +0200667{
668 char *dbg_format;
669 const char *str_group;
670 va_list ap;
671
Václav Kubernátd367ad92021-11-29 09:28:56 +0100672 if (!(ATOMIC_LOAD_RELAXED(ly_ldbg_groups) & group)) {
Radek Krejci4ab61562018-09-05 15:00:37 +0200673 return;
674 }
675
676 switch (group) {
677 case LY_LDGDICT:
678 str_group = "DICT";
679 break;
Radek Krejci4ab61562018-09-05 15:00:37 +0200680 case LY_LDGXPATH:
681 str_group = "XPATH";
682 break;
Michal Vaskoe558f792021-07-28 08:20:15 +0200683 case LY_LDGDEPSETS:
684 str_group = "DEPSETS";
685 break;
Radek Krejci4ab61562018-09-05 15:00:37 +0200686 default:
687 LOGINT(NULL);
688 return;
689 }
690
691 if (asprintf(&dbg_format, "%s: %s", str_group, format) == -1) {
692 LOGMEM(NULL);
693 return;
694 }
695
696 va_start(ap, format);
Michal Vasko7a266772024-01-23 11:02:38 +0100697 log_vprintf(NULL, LY_LLDBG, 0, 0, NULL, NULL, 0, NULL, dbg_format, ap);
Radek Krejci4ab61562018-09-05 15:00:37 +0200698 va_end(ap);
aPiecekd6d2c042023-06-02 08:31:43 +0200699
700 free(dbg_format);
Radek Krejci4ab61562018-09-05 15:00:37 +0200701}
702
703#endif
704
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200705void
Michal Vasko7a266772024-01-23 11:02:38 +0100706ly_log(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR err, const char *format, ...)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200707{
708 va_list ap;
709
710 va_start(ap, format);
Michal Vasko7a266772024-01-23 11:02:38 +0100711 log_vprintf(ctx, level, err, 0, NULL, NULL, 0, NULL, format, ap);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200712 va_end(ap);
713}
714
Michal Vaskodbf3e652022-10-21 08:46:25 +0200715/**
716 * @brief Append a schema node name to a generated data path, only if it fits.
717 *
718 * @param[in,out] str Generated path to update.
719 * @param[in] snode Schema node to append.
720 * @param[in] parent Last printed data node.
721 * @return LY_ERR value.
722 */
723static LY_ERR
724ly_vlog_build_path_append(char **str, const struct lysc_node *snode, const struct lyd_node *parent)
725{
726 const struct lys_module *mod, *prev_mod;
727 uint32_t len, new_len;
728 void *mem;
729
730 if (snode->nodetype & (LYS_CHOICE | LYS_CASE)) {
731 /* schema-only node */
732 return LY_SUCCESS;
Michal Vaskoccd1fa62023-08-17 09:15:46 +0200733 } else if (lysc_data_parent(snode) != lyd_node_schema(parent)) {
Michal Vaskodbf3e652022-10-21 08:46:25 +0200734 /* not a direct descendant node */
735 return LY_SUCCESS;
736 }
737
738 /* get module to print, if any */
739 mod = snode->module;
Michal Vasko420cc252023-08-24 08:14:24 +0200740 prev_mod = lyd_node_module(parent);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200741 if (prev_mod == mod) {
742 mod = NULL;
743 }
744
745 /* realloc string */
Michal Vasko03205432023-09-11 10:29:49 +0200746 len = *str ? strlen(*str) : 0;
Michal Vaskodbf3e652022-10-21 08:46:25 +0200747 new_len = len + 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(snode->name);
748 mem = realloc(*str, new_len + 1);
749 LY_CHECK_ERR_RET(!mem, LOGMEM(LYD_CTX(parent)), LY_EMEM);
750 *str = mem;
751
752 /* print the last schema node */
753 sprintf(*str + len, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", snode->name);
754 return LY_SUCCESS;
755}
756
Michal Vaskocd9c00b2023-09-11 10:29:28 +0200757LY_ERR
758ly_vlog_build_data_path(const struct ly_ctx *ctx, char **path)
759{
760 LY_ERR rc = LY_SUCCESS;
761 const struct lyd_node *dnode = NULL;
762
763 *path = NULL;
764
765 if (log_location.dnodes.count) {
766 dnode = log_location.dnodes.objs[log_location.dnodes.count - 1];
Michal Vasko7a266772024-01-23 11:02:38 +0100767 if (!dnode) {
768 /* special root node */
769 assert(log_location.dnodes.count == 1);
770 *path = strdup("/");
771 LY_CHECK_ERR_GOTO(!*path, LOGMEM(ctx); rc = LY_EMEM, cleanup);
772 goto cleanup;
773 }
774
Michal Vaskocd9c00b2023-09-11 10:29:28 +0200775 if (dnode->parent || !lysc_data_parent(dnode->schema)) {
776 /* data node with all of its parents */
777 *path = lyd_path(log_location.dnodes.objs[log_location.dnodes.count - 1], LYD_PATH_STD, NULL, 0);
778 LY_CHECK_ERR_GOTO(!*path, LOGMEM(ctx); rc = LY_EMEM, cleanup);
779 } else {
780 /* data parsers put all the parent nodes in the set, but they are not connected */
781 *path = lyd_path_set(&log_location.dnodes, LYD_PATH_STD);
782 LY_CHECK_ERR_GOTO(!*path, LOGMEM(ctx); rc = LY_EMEM, cleanup);
783 }
784 }
785
786 /* sometimes the last node is not created yet and we only have the schema node */
787 if (log_location.scnodes.count) {
788 rc = ly_vlog_build_path_append(path, log_location.scnodes.objs[log_location.scnodes.count - 1], dnode);
789 LY_CHECK_GOTO(rc, cleanup);
790 }
791
792cleanup:
793 if (rc) {
794 free(*path);
795 *path = NULL;
796 }
797 return rc;
798}
799
Michal Vaskodbf3e652022-10-21 08:46:25 +0200800/**
Michal Vasko7a266772024-01-23 11:02:38 +0100801 * @brief Build log path/input line from the stored log location information.
Michal Vaskodbf3e652022-10-21 08:46:25 +0200802 *
803 * @param[in] ctx Context to use.
Michal Vasko7a266772024-01-23 11:02:38 +0100804 * @param[out] data_path Generated data path.
805 * @param[out] schema_path Generated data path.
806 * @param[out] line Input line.
Michal Vaskodbf3e652022-10-21 08:46:25 +0200807 * @return LY_ERR value.
808 */
Radek Krejci94aa9942018-09-07 17:12:17 +0200809static LY_ERR
Michal Vasko7a266772024-01-23 11:02:38 +0100810ly_vlog_build_path_line(const struct ly_ctx *ctx, char **data_path, char **schema_path, uint64_t *line)
Radek Krejci94aa9942018-09-07 17:12:17 +0200811{
Michal Vasko7a266772024-01-23 11:02:38 +0100812 *data_path = NULL;
813 *schema_path = NULL;
814 *line = 0;
Radek Krejcicb3e6472021-01-06 08:19:01 +0100815
Michal Vasko7a266772024-01-23 11:02:38 +0100816 if (log_location.spaths.count && ((const char *)(log_location.spaths.objs[log_location.spaths.count - 1]))[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100817 /* simply get what is in the provided path string */
Michal Vasko7a266772024-01-23 11:02:38 +0100818 *schema_path = strdup(log_location.spaths.objs[log_location.spaths.count - 1]);
819 LY_CHECK_ERR_RET(!*schema_path, LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100820 } else {
Michal Vaskodbf3e652022-10-21 08:46:25 +0200821 /* data/schema node */
822 if (log_location.dnodes.count) {
Michal Vasko7a266772024-01-23 11:02:38 +0100823 LY_CHECK_RET(ly_vlog_build_data_path(ctx, data_path));
Michal Vaskodbf3e652022-10-21 08:46:25 +0200824 } else if (log_location.scnodes.count) {
Michal Vasko7a266772024-01-23 11:02:38 +0100825 *schema_path = lysc_path(log_location.scnodes.objs[log_location.scnodes.count - 1], LYSC_PATH_LOG, NULL, 0);
826 LY_CHECK_ERR_RET(!*schema_path, LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100827 }
Michal Vasko273727c2023-09-11 10:30:08 +0200828 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100829
Michal Vasko273727c2023-09-11 10:30:08 +0200830 /* line */
Michal Vasko7a266772024-01-23 11:02:38 +0100831 if (log_location.inputs.count) {
832 *line = ((struct ly_in *)log_location.inputs.objs[log_location.inputs.count - 1])->line;
Radek Krejci94aa9942018-09-07 17:12:17 +0200833 }
834
Radek Krejci94aa9942018-09-07 17:12:17 +0200835 return LY_SUCCESS;
836}
837
838void
Michal Vaskoe9391c72021-10-05 10:04:56 +0200839ly_vlog(const struct ly_ctx *ctx, const char *apptag, LY_VECODE code, const char *format, ...)
Radek Krejci94aa9942018-09-07 17:12:17 +0200840{
841 va_list ap;
Michal Vasko7a266772024-01-23 11:02:38 +0100842 char *data_path = NULL, *schema_path = NULL;
843 uint64_t line = 0;
Radek Krejci94aa9942018-09-07 17:12:17 +0200844
Michal Vasko7a266772024-01-23 11:02:38 +0100845 if (ctx) {
846 ly_vlog_build_path_line(ctx, &data_path, &schema_path, &line);
Radek Krejci94aa9942018-09-07 17:12:17 +0200847 }
848
849 va_start(ap, format);
Michal Vasko7a266772024-01-23 11:02:38 +0100850 log_vprintf(ctx, LY_LLERR, LY_EVALID, code, data_path, schema_path, line, apptag, format, ap);
Radek Krejci94aa9942018-09-07 17:12:17 +0200851 /* path is spent and should not be freed! */
852 va_end(ap);
853}
854
Michal Vasko193dacd2022-10-13 08:43:05 +0200855/**
856 * @brief Print a log message from an extension plugin callback.
857 *
858 * @param[in] ctx libyang context to store the error record. If not provided, the error is just printed.
859 * @param[in] plugin_name Name of the plugin generating the message.
860 * @param[in] level Log message level (error, warning, etc.)
Michal Vasko7a266772024-01-23 11:02:38 +0100861 * @param[in] err Error type code.
862 * @param[in] data_path Error data path, always spent.
863 * @param[in] schema_path Error schema path, always spent.
864 * @param[in] line Error input line, if any.
Michal Vasko193dacd2022-10-13 08:43:05 +0200865 * @param[in] format Format string to print.
866 * @param[in] ap Var arg list for @p format.
867 */
868static void
Michal Vasko7a266772024-01-23 11:02:38 +0100869ly_ext_log(const struct ly_ctx *ctx, const char *plugin_name, LY_LOG_LEVEL level, LY_ERR err, char *data_path,
870 char *schema_path, uint64_t line, const char *format, va_list ap)
Radek Krejci0935f412019-08-20 16:15:18 +0200871{
Radek Krejci0935f412019-08-20 16:15:18 +0200872 char *plugin_msg;
Radek Krejci0935f412019-08-20 16:15:18 +0200873
Václav Kubernátd367ad92021-11-29 09:28:56 +0100874 if (ATOMIC_LOAD_RELAXED(ly_ll) < level) {
Radek Krejci0935f412019-08-20 16:15:18 +0200875 return;
876 }
Michal Vasko193dacd2022-10-13 08:43:05 +0200877 if (asprintf(&plugin_msg, "Ext plugin \"%s\": %s", plugin_name, format) == -1) {
878 LOGMEM(ctx);
Radek Krejci0935f412019-08-20 16:15:18 +0200879 return;
880 }
881
Michal Vasko7a266772024-01-23 11:02:38 +0100882 log_vprintf(ctx, level, (level == LY_LLERR ? LY_EPLUGIN : 0) | err, LYVE_OTHER, data_path, schema_path, line, NULL,
883 plugin_msg, ap);
Michal Vasko193dacd2022-10-13 08:43:05 +0200884 free(plugin_msg);
885}
886
887LIBYANG_API_DEF void
Michal Vasko7a266772024-01-23 11:02:38 +0100888lyplg_ext_parse_log(const struct lysp_ctx *pctx, const struct lysp_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err,
Michal Vasko193dacd2022-10-13 08:43:05 +0200889 const char *format, ...)
890{
891 va_list ap;
Michal Vasko7a266772024-01-23 11:02:38 +0100892 char *data_path, *schema_path;
893 uint64_t line;
Michal Vasko193dacd2022-10-13 08:43:05 +0200894
Michal Vasko7a266772024-01-23 11:02:38 +0100895 ly_vlog_build_path_line(PARSER_CTX(pctx), &data_path, &schema_path, &line);
Michal Vasko193dacd2022-10-13 08:43:05 +0200896
Radek Krejci0935f412019-08-20 16:15:18 +0200897 va_start(ap, format);
Michal Vasko7a266772024-01-23 11:02:38 +0100898 ly_ext_log(PARSER_CTX(pctx), ext->record->plugin.id, level, err, data_path, schema_path, line, format, ap);
Radek Krejci0935f412019-08-20 16:15:18 +0200899 va_end(ap);
Michal Vasko193dacd2022-10-13 08:43:05 +0200900}
901
902LIBYANG_API_DEF void
Michal Vasko7a266772024-01-23 11:02:38 +0100903lyplg_ext_compile_log(const struct lysc_ctx *cctx, const struct lysc_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err,
Michal Vasko193dacd2022-10-13 08:43:05 +0200904 const char *format, ...)
905{
906 va_list ap;
Michal Vasko7a266772024-01-23 11:02:38 +0100907 char *schema_path = NULL;
Michal Vasko6727c682023-02-17 10:40:26 +0100908
Michal Vasko7a266772024-01-23 11:02:38 +0100909 if (cctx && !(schema_path = strdup(cctx->path))) {
Michal Vasko6727c682023-02-17 10:40:26 +0100910 LOGMEM(cctx->ctx);
911 return;
912 }
Michal Vasko193dacd2022-10-13 08:43:05 +0200913
914 va_start(ap, format);
Michal Vasko7a266772024-01-23 11:02:38 +0100915 ly_ext_log(ext->module->ctx, ext->def->plugin->id, level, err, NULL, schema_path, 0, format, ap);
Michal Vasko193dacd2022-10-13 08:43:05 +0200916 va_end(ap);
917}
918
919LIBYANG_API_DEF void
Michal Vasko7a266772024-01-23 11:02:38 +0100920lyplg_ext_compile_log_path(const char *path, const struct lysc_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err,
Michal Vasko193dacd2022-10-13 08:43:05 +0200921 const char *format, ...)
922{
923 va_list ap;
Michal Vasko7a266772024-01-23 11:02:38 +0100924 char *schema_path = NULL;
Michal Vasko6727c682023-02-17 10:40:26 +0100925
Michal Vasko7a266772024-01-23 11:02:38 +0100926 if (path && !(schema_path = strdup(path))) {
Michal Vasko6727c682023-02-17 10:40:26 +0100927 LOGMEM(ext->module->ctx);
928 return;
929 }
Michal Vasko193dacd2022-10-13 08:43:05 +0200930
931 va_start(ap, format);
Michal Vasko7a266772024-01-23 11:02:38 +0100932 ly_ext_log(ext->module->ctx, ext->def->plugin->id, level, err, NULL, schema_path, 0, format, ap);
Michal Vasko193dacd2022-10-13 08:43:05 +0200933 va_end(ap);
Radek Krejci0935f412019-08-20 16:15:18 +0200934}
935
Michal Vasko177d0ed2020-11-23 16:43:03 +0100936/**
Michal Vasko6727c682023-02-17 10:40:26 +0100937 * @brief Serves only for creating ap.
938 */
939static void
Michal Vasko7a266772024-01-23 11:02:38 +0100940_lyplg_ext_compile_log_err(const struct ly_err_item *eitem, const struct lysc_ext_instance *ext, ...)
Michal Vasko6727c682023-02-17 10:40:26 +0100941{
942 va_list ap;
Michal Vasko7a266772024-01-23 11:02:38 +0100943 char *data_path = NULL, *schema_path = NULL;
944
945 if (eitem->data_path) {
946 data_path = strdup(eitem->data_path);
947 }
948 if (eitem->schema_path) {
949 schema_path = strdup(eitem->schema_path);
950 }
Michal Vasko6727c682023-02-17 10:40:26 +0100951
952 va_start(ap, ext);
Michal Vasko7a266772024-01-23 11:02:38 +0100953 ly_ext_log(ext->module->ctx, ext->def->plugin->id, eitem->level, eitem->err, data_path, schema_path, eitem->line, "%s", ap);
Michal Vasko6727c682023-02-17 10:40:26 +0100954 va_end(ap);
955}
956
957LIBYANG_API_DEF void
Michal Vasko7a266772024-01-23 11:02:38 +0100958lyplg_ext_compile_log_err(const struct ly_err_item *eitem, const struct lysc_ext_instance *ext)
Michal Vasko6727c682023-02-17 10:40:26 +0100959{
Michal Vasko7a266772024-01-23 11:02:38 +0100960 _lyplg_ext_compile_log_err(eitem, ext, eitem->msg);
Michal Vasko6727c682023-02-17 10:40:26 +0100961}
962
963/**
Michal Vaskoc78a6092021-05-07 15:27:35 +0200964 * @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 +0100965 */
966static void
Michal Vasko7a266772024-01-23 11:02:38 +0100967_ly_err_print(const struct ly_ctx *ctx, const struct ly_err_item *eitem, const char *format, ...)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200968{
Michal Vasko177d0ed2020-11-23 16:43:03 +0100969 va_list ap;
Michal Vasko7a266772024-01-23 11:02:38 +0100970 char *data_path = NULL, *schema_path = NULL;
Michal Vasko177d0ed2020-11-23 16:43:03 +0100971
972 LY_CHECK_ARG_RET(ctx, eitem, );
973
Michal Vasko7a266772024-01-23 11:02:38 +0100974 if (eitem->data_path) {
975 data_path = strdup(eitem->data_path);
976 }
977 if (eitem->schema_path) {
978 schema_path = strdup(eitem->schema_path);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200979 }
Michal Vasko177d0ed2020-11-23 16:43:03 +0100980
Michal Vaskoc78a6092021-05-07 15:27:35 +0200981 va_start(ap, format);
Michal Vasko7a266772024-01-23 11:02:38 +0100982 log_vprintf(ctx, eitem->level, eitem->err, eitem->vecode, data_path, schema_path, eitem->line, eitem->apptag, format, ap);
Michal Vasko177d0ed2020-11-23 16:43:03 +0100983 va_end(ap);
Michal Vasko177d0ed2020-11-23 16:43:03 +0100984}
985
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100986LIBYANG_API_DEF void
Michal Vasko7a266772024-01-23 11:02:38 +0100987ly_err_print(const struct ly_ctx *ctx, const struct ly_err_item *eitem)
Michal Vasko177d0ed2020-11-23 16:43:03 +0100988{
Michal Vaskoc78a6092021-05-07 15:27:35 +0200989 /* String ::ly_err_item.msg cannot be used directly because it may contain the % character */
990 _ly_err_print(ctx, eitem, "%s", eitem->msg);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200991}