blob: f66873ddf5030020445de9d70b7a821c125e145a [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 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
Jan Kundrátc53a7ec2021-12-09 16:01:19 +010051LIBYANG_API_DEF LY_ERR
Radek Krejcid33273d2018-10-25 14:55:52 +020052ly_errcode(const struct ly_ctx *ctx)
53{
54 struct ly_err_item *i;
55
Radek Krejci572ee602020-09-16 14:35:08 +020056 i = ly_err_last(ctx);
Radek Krejcid33273d2018-10-25 14:55:52 +020057 if (i) {
Radek Krejci572ee602020-09-16 14:35:08 +020058 return i->no;
Radek Krejcid33273d2018-10-25 14:55:52 +020059 }
60
61 return LY_SUCCESS;
62}
63
Michal Vaskob5b883c2023-07-10 10:36:18 +020064LIBYANG_API_DEF const char *
65ly_strerrcode(LY_ERR err)
66{
67 /* ignore plugin flag */
68 err &= ~LY_EPLUGIN;
69
70 switch (err) {
71 case LY_SUCCESS:
Michal Vaskoe4207652023-07-10 14:57:32 +020072 return "Success";
Michal Vaskob5b883c2023-07-10 10:36:18 +020073 case LY_EMEM:
74 return "Out of memory";
75 case LY_ESYS:
76 return "System call failed";
77 case LY_EINVAL:
78 return "Invalid value";
79 case LY_EEXIST:
80 return "Already exists";
81 case LY_ENOTFOUND:
82 return "Not found";
83 case LY_EINT:
84 return "Internal error";
85 case LY_EVALID:
86 return "Validation failed";
87 case LY_EDENIED:
88 return "Operation denied";
89 case LY_EINCOMPLETE:
90 return "Operation incomplete";
91 case LY_ERECOMPILE:
92 return "Recompilation required";
93 case LY_ENOT:
94 return "Negative result";
95 case LY_EOTHER:
96 return "Another failure reason";
97 case LY_EPLUGIN:
98 break;
99 }
100
101 /* unreachable */
Michal Vaskob87e9a12023-07-10 15:10:58 +0200102 return "Unknown";
Michal Vaskob5b883c2023-07-10 10:36:18 +0200103}
104
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100105LIBYANG_API_DEF LY_VECODE
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200106ly_vecode(const struct ly_ctx *ctx)
107{
108 struct ly_err_item *i;
109
Radek Krejci572ee602020-09-16 14:35:08 +0200110 i = ly_err_last(ctx);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200111 if (i) {
Radek Krejci572ee602020-09-16 14:35:08 +0200112 return i->vecode;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200113 }
114
115 return LYVE_SUCCESS;
116}
117
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100118LIBYANG_API_DEF const char *
Michal Vaskob5b883c2023-07-10 10:36:18 +0200119ly_strvecode(LY_VECODE vecode)
120{
121 switch (vecode) {
122 case LYVE_SUCCESS:
Michal Vaskoe4207652023-07-10 14:57:32 +0200123 return "Success";
Michal Vaskob5b883c2023-07-10 10:36:18 +0200124 case LYVE_SYNTAX:
125 return "General syntax error";
126 case LYVE_SYNTAX_YANG:
127 return "YANG syntax error";
128 case LYVE_SYNTAX_YIN:
129 return "YIN syntax error";
130 case LYVE_REFERENCE:
131 return "Reference error";
132 case LYVE_XPATH:
133 return "XPath error";
134 case LYVE_SEMANTICS:
135 return "Semantic error";
136 case LYVE_SYNTAX_XML:
137 return "XML syntax error";
138 case LYVE_SYNTAX_JSON:
139 return "JSON syntax error";
140 case LYVE_DATA:
141 return "YANG data error";
142 case LYVE_OTHER:
143 return "Another error";
144 }
145
146 /* unreachable */
Michal Vaskob87e9a12023-07-10 15:10:58 +0200147 return "Unknown";
Michal Vaskob5b883c2023-07-10 10:36:18 +0200148}
149
150LIBYANG_API_DEF const char *
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200151ly_errmsg(const struct ly_ctx *ctx)
152{
153 struct ly_err_item *i;
154
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200155 LY_CHECK_ARG_RET(NULL, ctx, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200156
Radek Krejci572ee602020-09-16 14:35:08 +0200157 i = ly_err_last(ctx);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200158 if (i) {
Radek Krejci572ee602020-09-16 14:35:08 +0200159 return i->msg;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200160 }
161
162 return NULL;
163}
164
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100165LIBYANG_API_DEF const char *
Michal Vaskob5b883c2023-07-10 10:36:18 +0200166ly_last_errmsg(void)
167{
168 return last_msg;
169}
170
171LIBYANG_API_DEF const char *
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200172ly_errpath(const struct ly_ctx *ctx)
173{
174 struct ly_err_item *i;
175
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200176 LY_CHECK_ARG_RET(NULL, ctx, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200177
Radek Krejci572ee602020-09-16 14:35:08 +0200178 i = ly_err_last(ctx);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200179 if (i) {
Radek Krejci572ee602020-09-16 14:35:08 +0200180 return i->path;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200181 }
182
183 return NULL;
184}
185
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100186LIBYANG_API_DEF const char *
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200187ly_errapptag(const struct ly_ctx *ctx)
188{
189 struct ly_err_item *i;
190
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200191 LY_CHECK_ARG_RET(NULL, ctx, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200192
Radek Krejci572ee602020-09-16 14:35:08 +0200193 i = ly_err_last(ctx);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200194 if (i) {
Radek Krejci572ee602020-09-16 14:35:08 +0200195 return i->apptag;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200196 }
197
198 return NULL;
199}
200
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100201LIBYANG_API_DEF LY_ERR
aPiecek6d618552021-06-18 10:02:59 +0200202ly_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 +0200203{
Radek Krejcidb0ee022021-03-15 16:53:44 +0100204 char *msg = NULL;
205 struct ly_err_item *e;
Radek Krejcie7b95092019-05-15 11:03:07 +0200206
Radek Krejcid43298b2021-03-25 16:17:15 +0100207 if (!err || (ecode == LY_SUCCESS)) {
Radek Krejcidb0ee022021-03-15 16:53:44 +0100208 /* nothing to do */
209 return ecode;
210 }
211
212 e = malloc(sizeof *e);
213 LY_CHECK_ERR_RET(!e, LOGMEM(NULL), LY_EMEM);
214 e->prev = (*err) ? (*err)->prev : e;
215 e->next = NULL;
216 if (*err) {
217 (*err)->prev->next = e;
218 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200219
220 /* fill in the information */
Radek Krejcidb0ee022021-03-15 16:53:44 +0100221 e->level = LY_LLERR;
222 e->no = ecode;
223 e->vecode = vecode;
224 e->path = path;
225 e->apptag = apptag;
Radek Krejcie7b95092019-05-15 11:03:07 +0200226
aPiecek6d618552021-06-18 10:02:59 +0200227 if (err_format) {
Radek Krejcidb0ee022021-03-15 16:53:44 +0100228 va_list print_args;
229
aPiecek6d618552021-06-18 10:02:59 +0200230 va_start(print_args, err_format);
Radek Krejcidb0ee022021-03-15 16:53:44 +0100231
aPiecek6d618552021-06-18 10:02:59 +0200232 if (vasprintf(&msg, err_format, print_args) == -1) {
233 /* we don't have anything more to do, just set msg to NULL to avoid undefined content,
Radek Krejcidb0ee022021-03-15 16:53:44 +0100234 * still keep the information about the original error instead of LY_EMEM or other printf's error */
235 msg = NULL;
236 }
237
238 va_end(print_args);
239 }
240 e->msg = msg;
241
242 if (!(*err)) {
243 *err = e;
244 }
245
246 return e->no;
Radek Krejcie7b95092019-05-15 11:03:07 +0200247}
248
Michal Vasko88ccd582023-03-30 11:50:57 +0200249/**
250 * @brief Get error record from error hash table of a context for the current thread.
251 *
252 * @param[in] ctx Context to use.
253 * @return Thread error record, if any.
254 */
255static struct ly_ctx_err_rec *
256ly_err_get_rec(const struct ly_ctx *ctx)
257{
Michal Vasko4cca0092023-03-30 13:20:53 +0200258 struct ly_ctx_err_rec rec, *match;
Michal Vasko88ccd582023-03-30 11:50:57 +0200259
260 /* prepare record */
261 rec.tid = pthread_self();
262
263 /* get the pointer to the matching record */
Michal Vaskoae130f52023-04-20 14:25:16 +0200264 if (lyht_find(ctx->err_ht, &rec, lyht_hash((void *)&rec.tid, sizeof rec.tid), (void **)&match)) {
Michal Vasko4cca0092023-03-30 13:20:53 +0200265 return NULL;
266 }
Michal Vasko88ccd582023-03-30 11:50:57 +0200267
268 return match;
269}
270
Michal Vaskoe0be7452023-03-30 13:35:35 +0200271/**
272 * @brief Insert new error record to error hash table of a context for the current thread.
273 *
274 * @param[in] ctx Context to use.
275 * @return Thread error record.
276 */
277static struct ly_ctx_err_rec *
278ly_err_new_rec(const struct ly_ctx *ctx)
279{
280 struct ly_ctx_err_rec new, *rec;
281 LY_ERR r;
282
283 /* insert a new record */
284 new.err = NULL;
285 new.tid = pthread_self();
286
287 /* reuse lock */
288 /* LOCK */
289 pthread_mutex_lock((pthread_mutex_t *)&ctx->lyb_hash_lock);
290
Michal Vaskoae130f52023-04-20 14:25:16 +0200291 r = lyht_insert(ctx->err_ht, &new, lyht_hash((void *)&new.tid, sizeof new.tid), (void **)&rec);
Michal Vaskoe0be7452023-03-30 13:35:35 +0200292
293 /* UNLOCK */
294 pthread_mutex_unlock((pthread_mutex_t *)&ctx->lyb_hash_lock);
295
296 return r ? NULL : rec;
297}
298
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100299LIBYANG_API_DEF struct ly_err_item *
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200300ly_err_first(const struct ly_ctx *ctx)
301{
Michal Vasko88ccd582023-03-30 11:50:57 +0200302 struct ly_ctx_err_rec *rec;
303
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200304 LY_CHECK_ARG_RET(NULL, ctx, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200305
Michal Vasko88ccd582023-03-30 11:50:57 +0200306 /* get the pointer to the matching record */
307 rec = ly_err_get_rec(ctx);
308
309 return rec ? rec->err : NULL;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200310}
311
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100312LIBYANG_API_DEF struct ly_err_item *
Radek Krejci572ee602020-09-16 14:35:08 +0200313ly_err_last(const struct ly_ctx *ctx)
314{
Michal Vasko88ccd582023-03-30 11:50:57 +0200315 struct ly_ctx_err_rec *rec;
Radek Krejci572ee602020-09-16 14:35:08 +0200316
317 LY_CHECK_ARG_RET(NULL, ctx, NULL);
318
Michal Vasko88ccd582023-03-30 11:50:57 +0200319 /* get the pointer to the matching record */
320 if (!(rec = ly_err_get_rec(ctx))) {
321 return NULL;
322 }
323
324 return rec->err ? rec->err->prev : NULL;
Radek Krejci572ee602020-09-16 14:35:08 +0200325}
326
Michal Vasko9dbb91d2023-01-30 13:59:22 +0100327void
328ly_err_move(struct ly_ctx *src_ctx, struct ly_ctx *trg_ctx)
329{
Michal Vasko88ccd582023-03-30 11:50:57 +0200330 struct ly_ctx_err_rec *rec;
331 struct ly_err_item *err = NULL;
Michal Vasko9dbb91d2023-01-30 13:59:22 +0100332
Michal Vasko88ccd582023-03-30 11:50:57 +0200333 /* get and remove the errors from src */
334 rec = ly_err_get_rec(src_ctx);
335 if (rec) {
336 err = rec->err;
337 rec->err = NULL;
338 }
Michal Vasko9dbb91d2023-01-30 13:59:22 +0100339
340 /* set them for trg */
Michal Vaskoe0be7452023-03-30 13:35:35 +0200341 if (!(rec = ly_err_get_rec(trg_ctx))) {
342 if (!(rec = ly_err_new_rec(trg_ctx))) {
343 LOGINT(NULL);
344 ly_err_free(err);
345 return;
346 }
347 }
Michal Vasko88ccd582023-03-30 11:50:57 +0200348 ly_err_free(rec->err);
349 rec->err = err;
Michal Vasko9dbb91d2023-01-30 13:59:22 +0100350}
351
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100352LIBYANG_API_DEF void
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200353ly_err_free(void *ptr)
354{
Michal Vasko88ccd582023-03-30 11:50:57 +0200355 struct ly_err_item *e, *next;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200356
357 /* clean the error list */
Michal Vasko88ccd582023-03-30 11:50:57 +0200358 LY_LIST_FOR_SAFE(ptr, next, e) {
359 free(e->msg);
360 free(e->path);
361 free(e->apptag);
362 free(e);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200363 }
364}
365
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100366LIBYANG_API_DEF void
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200367ly_err_clean(struct ly_ctx *ctx, struct ly_err_item *eitem)
368{
Michal Vasko88ccd582023-03-30 11:50:57 +0200369 struct ly_ctx_err_rec *rec;
370 struct ly_err_item *e;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200371
Michal Vasko88ccd582023-03-30 11:50:57 +0200372 if (!(rec = ly_err_get_rec(ctx))) {
373 return;
374 }
375 if (rec->err == eitem) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200376 eitem = NULL;
377 }
Michal Vasko88ccd582023-03-30 11:50:57 +0200378
379 if (!eitem) {
380 /* free all err */
381 ly_err_free(rec->err);
382 rec->err = NULL;
383 } else {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200384 /* disconnect the error */
Michal Vasko88ccd582023-03-30 11:50:57 +0200385 for (e = rec->err; e && (e->next != eitem); e = e->next) {}
386 assert(e);
387 e->next = NULL;
388 rec->err->prev = e;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200389 /* free this err and newer */
390 ly_err_free(eitem);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200391 }
392}
393
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100394LIBYANG_API_DEF LY_LOG_LEVEL
Radek Krejci52b6d512020-10-12 12:33:17 +0200395ly_log_level(LY_LOG_LEVEL level)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200396{
Václav Kubernátd367ad92021-11-29 09:28:56 +0100397 LY_LOG_LEVEL prev = ATOMIC_LOAD_RELAXED(ly_ll);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200398
Václav Kubernátd367ad92021-11-29 09:28:56 +0100399 ATOMIC_STORE_RELAXED(ly_ll, level);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200400 return prev;
401}
402
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100403LIBYANG_API_DEF uint32_t
Radek Krejci1deb5be2020-08-26 16:43:36 +0200404ly_log_options(uint32_t opts)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200405{
Václav Kubernátd367ad92021-11-29 09:28:56 +0100406 uint32_t prev = ATOMIC_LOAD_RELAXED(ly_log_opts);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200407
Václav Kubernátd367ad92021-11-29 09:28:56 +0100408 ATOMIC_STORE_RELAXED(ly_log_opts, opts);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200409 return prev;
410}
411
Michal Vasko52da51d2024-01-30 16:09:19 +0100412LIBYANG_API_DEF uint32_t *
Michal Vaskod4a6d042022-12-08 08:34:29 +0100413ly_temp_log_options(uint32_t *opts)
414{
Michal Vasko52da51d2024-01-30 16:09:19 +0100415 uint32_t *prev_lo = temp_ly_log_opts;
416
Michal Vaskod4a6d042022-12-08 08:34:29 +0100417 temp_ly_log_opts = opts;
Michal Vasko52da51d2024-01-30 16:09:19 +0100418
419 return prev_lo;
Michal Vaskod4a6d042022-12-08 08:34:29 +0100420}
421
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100422LIBYANG_API_DEF uint32_t
Radek Krejci68433c92020-10-12 17:03:55 +0200423ly_log_dbg_groups(uint32_t dbg_groups)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200424{
425#ifndef NDEBUG
Václav Kubernátd367ad92021-11-29 09:28:56 +0100426 uint32_t prev = ATOMIC_LOAD_RELAXED(ly_ldbg_groups);
Radek Krejciebdaed02020-11-09 13:05:06 +0100427
Václav Kubernátd367ad92021-11-29 09:28:56 +0100428 ATOMIC_STORE_RELAXED(ly_ldbg_groups, dbg_groups);
Radek Krejciebdaed02020-11-09 13:05:06 +0100429 return prev;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200430#else
431 (void)dbg_groups;
Radek Krejciebdaed02020-11-09 13:05:06 +0100432 return 0;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200433#endif
434}
435
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100436LIBYANG_API_DEF void
Radek Krejci857189e2020-09-01 13:26:36 +0200437ly_set_log_clb(ly_log_clb clb, ly_bool path)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200438{
Michal Vaskod8085612020-08-21 12:55:23 +0200439 log_clb = clb;
Václav Kubernátd367ad92021-11-29 09:28:56 +0100440 ATOMIC_STORE_RELAXED(path_flag, path);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200441}
442
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100443LIBYANG_API_DEF ly_log_clb
Michal Vaskod8085612020-08-21 12:55:23 +0200444ly_get_log_clb(void)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200445{
Michal Vaskod8085612020-08-21 12:55:23 +0200446 return log_clb;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200447}
448
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100449void
Michal Vasko59e69e72022-02-18 09:18:21 +0100450ly_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 +0100451 uint64_t line)
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100452{
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100453 if (scnode) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100454 ly_set_add(&log_location.scnodes, (void *)scnode, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100455 }
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100456 if (dnode) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100457 ly_set_add(&log_location.dnodes, (void *)dnode, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100458 }
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100459 if (path) {
460 char *s = strdup(path);
Michal Vasko26bbb272022-08-02 14:54:33 +0200461
Radek Krejciddace2c2021-01-08 11:30:56 +0100462 LY_CHECK_ERR_RET(!s, LOGMEM(NULL), );
463 ly_set_add(&log_location.paths, s, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100464 }
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100465 if (in) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100466 ly_set_add(&log_location.inputs, (void *)in, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100467 }
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100468 if (line) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100469 log_location.line = line;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100470 }
471}
472
473void
Michal Vasko59e69e72022-02-18 09:18:21 +0100474ly_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 +0100475{
Radek Krejciddace2c2021-01-08 11:30:56 +0100476 for (uint32_t i = scnode_steps; i && log_location.scnodes.count; i--) {
477 log_location.scnodes.count--;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100478 }
479
Radek Krejciddace2c2021-01-08 11:30:56 +0100480 for (uint32_t i = dnode_steps; i && log_location.dnodes.count; i--) {
481 log_location.dnodes.count--;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100482 }
483
Radek Krejciddace2c2021-01-08 11:30:56 +0100484 for (uint32_t i = path_steps; i && log_location.paths.count; i--) {
485 ly_set_rm_index(&log_location.paths, log_location.paths.count - 1, free);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100486 }
487
Radek Krejciddace2c2021-01-08 11:30:56 +0100488 for (uint32_t i = in_steps; i && log_location.inputs.count; i--) {
489 log_location.inputs.count--;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100490 }
491
Radek Krejciddace2c2021-01-08 11:30:56 +0100492 /* deallocate the empty sets */
493 if (scnode_steps && !log_location.scnodes.count) {
494 ly_set_erase(&log_location.scnodes, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100495 }
Radek Krejciddace2c2021-01-08 11:30:56 +0100496 if (dnode_steps && !log_location.dnodes.count) {
497 ly_set_erase(&log_location.dnodes, NULL);
498 }
499 if (path_steps && !log_location.paths.count) {
500 ly_set_erase(&log_location.paths, free);
501 }
502 if (in_steps && !log_location.inputs.count) {
503 ly_set_erase(&log_location.inputs, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100504 }
505}
506
Michal Vaskoa878a892023-08-18 12:22:07 +0200507const struct lyd_node *
508ly_log_location_dnode(uint32_t idx)
509{
510 if (idx < log_location.dnodes.count) {
511 return log_location.dnodes.dnodes[idx];
512 }
513
514 return NULL;
515}
516
517uint32_t
518ly_log_location_dnode_count(void)
519{
520 return log_location.dnodes.count;
521}
522
Michal Vasko88ccd582023-03-30 11:50:57 +0200523/**
524 * @brief Store generated error in a context.
525 *
526 * @param[in] ctx Context to use.
527 * @param[in] level Message log level.
528 * @param[in] no Error number.
529 * @param[in] vecode Error validation error code.
530 * @param[in] msg Error message, always spent.
531 * @param[in] path Error path, always spent.
532 * @param[in] apptag Error app tag, always spent.
533 * @return LY_ERR value.
534 */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200535static LY_ERR
536log_store(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, LY_VECODE vecode, char *msg, char *path, char *apptag)
537{
Michal Vaskoe0be7452023-03-30 13:35:35 +0200538 struct ly_ctx_err_rec *rec;
Michal Vasko88ccd582023-03-30 11:50:57 +0200539 struct ly_err_item *e, *last;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200540
541 assert(ctx && (level < LY_LLVRB));
542
Michal Vasko88ccd582023-03-30 11:50:57 +0200543 if (!(rec = ly_err_get_rec(ctx))) {
Michal Vaskoe0be7452023-03-30 13:35:35 +0200544 if (!(rec = ly_err_new_rec(ctx))) {
545 goto mem_fail;
Michal Vasko88ccd582023-03-30 11:50:57 +0200546 }
547 }
548
549 e = rec->err;
550 if (!e) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200551 /* if we are only to fill in path, there must have been an error stored */
552 assert(msg);
Michal Vasko88ccd582023-03-30 11:50:57 +0200553 e = malloc(sizeof *e);
554 LY_CHECK_GOTO(!e, mem_fail);
555 e->prev = e;
556 e->next = NULL;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200557
Michal Vasko88ccd582023-03-30 11:50:57 +0200558 rec->err = e;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200559 } else if (!msg) {
560 /* only filling the path */
561 assert(path);
562
563 /* find last error */
Michal Vasko88ccd582023-03-30 11:50:57 +0200564 e = e->prev;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200565 do {
Michal Vasko88ccd582023-03-30 11:50:57 +0200566 if (e->level == LY_LLERR) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200567 /* fill the path */
Michal Vasko88ccd582023-03-30 11:50:57 +0200568 free(e->path);
569 e->path = path;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200570 return LY_SUCCESS;
571 }
Michal Vasko88ccd582023-03-30 11:50:57 +0200572 e = e->prev;
573 } while (e->prev->next);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200574 /* last error was not found */
575 assert(0);
Michal Vaskod4a6d042022-12-08 08:34:29 +0100576 } else if ((temp_ly_log_opts && ((*temp_ly_log_opts & LY_LOSTORE_LAST) == LY_LOSTORE_LAST)) ||
577 (!temp_ly_log_opts && ((ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOSTORE_LAST) == LY_LOSTORE_LAST))) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200578 /* overwrite last message */
Michal Vasko88ccd582023-03-30 11:50:57 +0200579 free(e->msg);
580 free(e->path);
581 free(e->apptag);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200582 } else {
583 /* store new message */
Michal Vasko88ccd582023-03-30 11:50:57 +0200584 last = e->prev;
585 e->prev = malloc(sizeof *e);
586 LY_CHECK_GOTO(!e->prev, mem_fail);
587 e = e->prev;
588 e->prev = last;
589 e->next = NULL;
590 last->next = e;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200591 }
592
593 /* fill in the information */
Michal Vasko88ccd582023-03-30 11:50:57 +0200594 e->level = level;
595 e->no = no;
596 e->vecode = vecode;
597 e->msg = msg;
598 e->path = path;
599 e->apptag = apptag;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200600 return LY_SUCCESS;
601
602mem_fail:
603 LOGMEM(NULL);
604 free(msg);
605 free(path);
606 free(apptag);
607 return LY_EMEM;
608}
609
610static void
Michal Vaskoe9391c72021-10-05 10:04:56 +0200611log_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 +0200612 const char *format, va_list args)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200613{
614 char *msg = NULL;
Michal Vaskod4a6d042022-12-08 08:34:29 +0100615 ly_bool free_strs, lolog, lostore;
616
617 /* learn effective logger options */
618 if (temp_ly_log_opts) {
619 lolog = *temp_ly_log_opts & LY_LOLOG;
620 lostore = *temp_ly_log_opts & LY_LOSTORE;
621 } else {
622 lolog = ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOLOG;
623 lostore = ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOSTORE;
624 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200625
Václav Kubernátd367ad92021-11-29 09:28:56 +0100626 if (level > ATOMIC_LOAD_RELAXED(ly_ll)) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200627 /* do not print or store the message */
628 free(path);
629 return;
630 }
631
Michal Vasko5a016922021-05-07 08:19:15 +0200632 if (no == LY_EMEM) {
633 /* just print it, anything else would most likely fail anyway */
Michal Vaskod4a6d042022-12-08 08:34:29 +0100634 if (lolog) {
Michal Vasko5a016922021-05-07 08:19:15 +0200635 if (log_clb) {
636 log_clb(level, LY_EMEM_MSG, path);
637 } else {
638 fprintf(stderr, "libyang[%d]: ", level);
639 vfprintf(stderr, format, args);
640 if (path) {
641 fprintf(stderr, " (path: %s)\n", path);
642 } else {
643 fprintf(stderr, "\n");
644 }
645 }
646 }
647 free(path);
648 return;
649 }
650
Michal Vasko236cbac2023-02-10 15:45:37 +0100651 /* print into a single message */
652 if (vasprintf(&msg, format, args) == -1) {
653 LOGMEM(ctx);
654 free(path);
655 return;
656 }
657
658 /* store as the last message */
659 strncpy(last_msg, msg, LY_LAST_MSG_SIZE - 1);
660
661 /* store the error/warning in the context (if we need to store errors internally, it does not matter what are
662 * the user log options) */
Michal Vaskod4a6d042022-12-08 08:34:29 +0100663 if ((level < LY_LLVRB) && ctx && lostore) {
Radek Krejcic9e64a62020-09-18 20:08:12 +0200664 if (((no & ~LY_EPLUGIN) == LY_EVALID) && (vecode == LYVE_SUCCESS)) {
665 /* assume we are inheriting the error, so inherit vecode as well */
666 vecode = ly_vecode(ctx);
667 }
Michal Vaskoe9391c72021-10-05 10:04:56 +0200668 if (log_store(ctx, level, no, vecode, msg, path, apptag ? strdup(apptag) : NULL)) {
Michal Vasko004d3152020-06-11 19:59:22 +0200669 return;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200670 }
671 free_strs = 0;
672 } else {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200673 free_strs = 1;
674 }
675
676 /* if we are only storing errors internally, never print the message (yet) */
Michal Vaskod4a6d042022-12-08 08:34:29 +0100677 if (lolog) {
Michal Vaskod8085612020-08-21 12:55:23 +0200678 if (log_clb) {
679 log_clb(level, msg, path);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200680 } else {
681 fprintf(stderr, "libyang[%d]: %s%s", level, msg, path ? " " : "\n");
682 if (path) {
683 fprintf(stderr, "(path: %s)\n", path);
684 }
685 }
686 }
687
688 if (free_strs) {
689 free(path);
690 free(msg);
691 }
692}
693
Radek Krejci4ab61562018-09-05 15:00:37 +0200694#ifndef NDEBUG
695
696void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200697ly_log_dbg(uint32_t group, const char *format, ...)
Radek Krejci4ab61562018-09-05 15:00:37 +0200698{
699 char *dbg_format;
700 const char *str_group;
701 va_list ap;
702
Václav Kubernátd367ad92021-11-29 09:28:56 +0100703 if (!(ATOMIC_LOAD_RELAXED(ly_ldbg_groups) & group)) {
Radek Krejci4ab61562018-09-05 15:00:37 +0200704 return;
705 }
706
707 switch (group) {
708 case LY_LDGDICT:
709 str_group = "DICT";
710 break;
Radek Krejci4ab61562018-09-05 15:00:37 +0200711 case LY_LDGXPATH:
712 str_group = "XPATH";
713 break;
Michal Vaskoe558f792021-07-28 08:20:15 +0200714 case LY_LDGDEPSETS:
715 str_group = "DEPSETS";
716 break;
Radek Krejci4ab61562018-09-05 15:00:37 +0200717 default:
718 LOGINT(NULL);
719 return;
720 }
721
722 if (asprintf(&dbg_format, "%s: %s", str_group, format) == -1) {
723 LOGMEM(NULL);
724 return;
725 }
726
727 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200728 log_vprintf(NULL, LY_LLDBG, 0, 0, NULL, NULL, dbg_format, ap);
Radek Krejci4ab61562018-09-05 15:00:37 +0200729 va_end(ap);
aPiecekd6d2c042023-06-02 08:31:43 +0200730
731 free(dbg_format);
Radek Krejci4ab61562018-09-05 15:00:37 +0200732}
733
734#endif
735
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200736void
737ly_log(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, const char *format, ...)
738{
739 va_list ap;
740
741 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200742 log_vprintf(ctx, level, no, 0, NULL, NULL, format, ap);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200743 va_end(ap);
744}
745
Michal Vaskodbf3e652022-10-21 08:46:25 +0200746/**
747 * @brief Append a schema node name to a generated data path, only if it fits.
748 *
749 * @param[in,out] str Generated path to update.
750 * @param[in] snode Schema node to append.
751 * @param[in] parent Last printed data node.
752 * @return LY_ERR value.
753 */
754static LY_ERR
755ly_vlog_build_path_append(char **str, const struct lysc_node *snode, const struct lyd_node *parent)
756{
757 const struct lys_module *mod, *prev_mod;
758 uint32_t len, new_len;
759 void *mem;
760
761 if (snode->nodetype & (LYS_CHOICE | LYS_CASE)) {
762 /* schema-only node */
763 return LY_SUCCESS;
Michal Vaskoccd1fa62023-08-17 09:15:46 +0200764 } else if (lysc_data_parent(snode) != lyd_node_schema(parent)) {
Michal Vaskodbf3e652022-10-21 08:46:25 +0200765 /* not a direct descendant node */
766 return LY_SUCCESS;
767 }
768
769 /* get module to print, if any */
770 mod = snode->module;
Michal Vasko420cc252023-08-24 08:14:24 +0200771 prev_mod = lyd_node_module(parent);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200772 if (prev_mod == mod) {
773 mod = NULL;
774 }
775
776 /* realloc string */
Michal Vasko03205432023-09-11 10:29:49 +0200777 len = *str ? strlen(*str) : 0;
Michal Vaskodbf3e652022-10-21 08:46:25 +0200778 new_len = len + 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(snode->name);
779 mem = realloc(*str, new_len + 1);
780 LY_CHECK_ERR_RET(!mem, LOGMEM(LYD_CTX(parent)), LY_EMEM);
781 *str = mem;
782
783 /* print the last schema node */
784 sprintf(*str + len, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", snode->name);
785 return LY_SUCCESS;
786}
787
Michal Vaskocd9c00b2023-09-11 10:29:28 +0200788LY_ERR
789ly_vlog_build_data_path(const struct ly_ctx *ctx, char **path)
790{
791 LY_ERR rc = LY_SUCCESS;
792 const struct lyd_node *dnode = NULL;
793
794 *path = NULL;
795
796 if (log_location.dnodes.count) {
797 dnode = log_location.dnodes.objs[log_location.dnodes.count - 1];
798 if (dnode->parent || !lysc_data_parent(dnode->schema)) {
799 /* data node with all of its parents */
800 *path = lyd_path(log_location.dnodes.objs[log_location.dnodes.count - 1], LYD_PATH_STD, NULL, 0);
801 LY_CHECK_ERR_GOTO(!*path, LOGMEM(ctx); rc = LY_EMEM, cleanup);
802 } else {
803 /* data parsers put all the parent nodes in the set, but they are not connected */
804 *path = lyd_path_set(&log_location.dnodes, LYD_PATH_STD);
805 LY_CHECK_ERR_GOTO(!*path, LOGMEM(ctx); rc = LY_EMEM, cleanup);
806 }
807 }
808
809 /* sometimes the last node is not created yet and we only have the schema node */
810 if (log_location.scnodes.count) {
811 rc = ly_vlog_build_path_append(path, log_location.scnodes.objs[log_location.scnodes.count - 1], dnode);
812 LY_CHECK_GOTO(rc, cleanup);
813 }
814
815cleanup:
816 if (rc) {
817 free(*path);
818 *path = NULL;
819 }
820 return rc;
821}
822
Michal Vaskodbf3e652022-10-21 08:46:25 +0200823/**
824 * @brief Build log path from the stored log location information.
825 *
826 * @param[in] ctx Context to use.
827 * @param[out] path Generated log path.
828 * @return LY_ERR value.
829 */
Radek Krejci94aa9942018-09-07 17:12:17 +0200830static LY_ERR
Radek Krejciddace2c2021-01-08 11:30:56 +0100831ly_vlog_build_path(const struct ly_ctx *ctx, char **path)
Radek Krejci94aa9942018-09-07 17:12:17 +0200832{
Michal Vaskodbf3e652022-10-21 08:46:25 +0200833 int r;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100834 char *str = NULL, *prev = NULL;
Radek Krejcicb3e6472021-01-06 08:19:01 +0100835
Radek Krejci2efc45b2020-12-22 16:25:44 +0100836 *path = NULL;
Radek Krejci94aa9942018-09-07 17:12:17 +0200837
Radek Krejciddace2c2021-01-08 11:30:56 +0100838 if (log_location.paths.count && ((const char *)(log_location.paths.objs[log_location.paths.count - 1]))[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100839 /* simply get what is in the provided path string */
Michal Vasko273727c2023-09-11 10:30:08 +0200840 r = asprintf(path, "Path \"%s\"", (const char *)log_location.paths.objs[log_location.paths.count - 1]);
Michal Vasko6727c682023-02-17 10:40:26 +0100841 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100842 } else {
Michal Vaskodbf3e652022-10-21 08:46:25 +0200843 /* data/schema node */
844 if (log_location.dnodes.count) {
Michal Vaskocd9c00b2023-09-11 10:29:28 +0200845 LY_CHECK_RET(ly_vlog_build_data_path(ctx, &str));
Michal Vaskodbf3e652022-10-21 08:46:25 +0200846
847 r = asprintf(path, "Data location \"%s\"", str);
848 free(str);
849 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
850 } else if (log_location.scnodes.count) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100851 str = lysc_path(log_location.scnodes.objs[log_location.scnodes.count - 1], LYSC_PATH_LOG, NULL, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100852 LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM);
853
Michal Vaskodbf3e652022-10-21 08:46:25 +0200854 r = asprintf(path, "Schema location \"%s\"", str);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100855 free(str);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200856 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100857 }
Michal Vasko273727c2023-09-11 10:30:08 +0200858 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100859
Michal Vasko273727c2023-09-11 10:30:08 +0200860 /* line */
861 prev = *path;
862 if (log_location.line) {
863 r = asprintf(path, "%s%sine number %" PRIu64, prev ? prev : "", prev ? ", l" : "L", log_location.line);
864 free(prev);
865 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
866
867 log_location.line = 0;
868 } else if (log_location.inputs.count) {
869 r = asprintf(path, "%s%sine number %" PRIu64, prev ? prev : "", prev ? ", l" : "L",
870 ((struct ly_in *)log_location.inputs.objs[log_location.inputs.count - 1])->line);
871 free(prev);
872 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
873 }
874
875 if (*path) {
Michal Vaskodbf3e652022-10-21 08:46:25 +0200876 prev = *path;
Michal Vasko273727c2023-09-11 10:30:08 +0200877 r = asprintf(path, "%s.", prev);
878 free(prev);
879 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
Radek Krejci94aa9942018-09-07 17:12:17 +0200880 }
881
Radek Krejci94aa9942018-09-07 17:12:17 +0200882 return LY_SUCCESS;
883}
884
885void
Michal Vaskoe9391c72021-10-05 10:04:56 +0200886ly_vlog(const struct ly_ctx *ctx, const char *apptag, LY_VECODE code, const char *format, ...)
Radek Krejci94aa9942018-09-07 17:12:17 +0200887{
888 va_list ap;
Michal Vasko22df3f02020-08-24 13:29:22 +0200889 char *path = NULL;
Radek Krejci94aa9942018-09-07 17:12:17 +0200890
Václav Kubernátd367ad92021-11-29 09:28:56 +0100891 if (ATOMIC_LOAD_RELAXED(path_flag) && ctx) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100892 ly_vlog_build_path(ctx, &path);
Radek Krejci94aa9942018-09-07 17:12:17 +0200893 }
894
895 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200896 log_vprintf(ctx, LY_LLERR, LY_EVALID, code, path, apptag, format, ap);
Radek Krejci94aa9942018-09-07 17:12:17 +0200897 /* path is spent and should not be freed! */
898 va_end(ap);
899}
900
Michal Vasko193dacd2022-10-13 08:43:05 +0200901/**
902 * @brief Print a log message from an extension plugin callback.
903 *
904 * @param[in] ctx libyang context to store the error record. If not provided, the error is just printed.
905 * @param[in] plugin_name Name of the plugin generating the message.
906 * @param[in] level Log message level (error, warning, etc.)
907 * @param[in] err_no Error type code.
Michal Vasko6727c682023-02-17 10:40:26 +0100908 * @param[in] path Optional path of the error, used if set.
Michal Vasko193dacd2022-10-13 08:43:05 +0200909 * @param[in] format Format string to print.
910 * @param[in] ap Var arg list for @p format.
911 */
912static void
Michal Vasko6727c682023-02-17 10:40:26 +0100913ly_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 +0200914 const char *format, va_list ap)
Radek Krejci0935f412019-08-20 16:15:18 +0200915{
Radek Krejci0935f412019-08-20 16:15:18 +0200916 char *plugin_msg;
Radek Krejci0935f412019-08-20 16:15:18 +0200917
Václav Kubernátd367ad92021-11-29 09:28:56 +0100918 if (ATOMIC_LOAD_RELAXED(ly_ll) < level) {
Radek Krejci0935f412019-08-20 16:15:18 +0200919 return;
920 }
Michal Vasko193dacd2022-10-13 08:43:05 +0200921 if (asprintf(&plugin_msg, "Ext plugin \"%s\": %s", plugin_name, format) == -1) {
922 LOGMEM(ctx);
Radek Krejci0935f412019-08-20 16:15:18 +0200923 return;
924 }
925
Michal Vasko6727c682023-02-17 10:40:26 +0100926 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 +0200927 free(plugin_msg);
928}
929
930LIBYANG_API_DEF void
931lyplg_ext_parse_log(const struct lysp_ctx *pctx, const struct lysp_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no,
932 const char *format, ...)
933{
934 va_list ap;
935 char *path = NULL;
936
937 if (ATOMIC_LOAD_RELAXED(path_flag)) {
938 ly_vlog_build_path(PARSER_CTX(pctx), &path);
939 }
940
Radek Krejci0935f412019-08-20 16:15:18 +0200941 va_start(ap, format);
Michal Vasko193dacd2022-10-13 08:43:05 +0200942 ly_ext_log(PARSER_CTX(pctx), ext->record->plugin.id, level, err_no, path, format, ap);
Radek Krejci0935f412019-08-20 16:15:18 +0200943 va_end(ap);
Michal Vasko193dacd2022-10-13 08:43:05 +0200944}
945
946LIBYANG_API_DEF void
947lyplg_ext_compile_log(const struct lysc_ctx *cctx, const struct lysc_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no,
948 const char *format, ...)
949{
950 va_list ap;
Michal Vasko6727c682023-02-17 10:40:26 +0100951 char *path = NULL;
952
953 if (cctx && (asprintf(&path, "Path \"%s\".", cctx->path) == -1)) {
954 LOGMEM(cctx->ctx);
955 return;
956 }
Michal Vasko193dacd2022-10-13 08:43:05 +0200957
958 va_start(ap, format);
Michal Vasko6727c682023-02-17 10:40:26 +0100959 ly_ext_log(ext->module->ctx, ext->def->plugin->id, level, err_no, path, format, ap);
Michal Vasko193dacd2022-10-13 08:43:05 +0200960 va_end(ap);
961}
962
963LIBYANG_API_DEF void
964lyplg_ext_compile_log_path(const char *path, const struct lysc_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no,
965 const char *format, ...)
966{
967 va_list ap;
Michal Vasko6727c682023-02-17 10:40:26 +0100968 char *log_path = NULL;
969
970 if (path && (asprintf(&log_path, "Path \"%s\".", path) == -1)) {
971 LOGMEM(ext->module->ctx);
972 return;
973 }
Michal Vasko193dacd2022-10-13 08:43:05 +0200974
975 va_start(ap, format);
Michal Vasko6727c682023-02-17 10:40:26 +0100976 ly_ext_log(ext->module->ctx, ext->def->plugin->id, level, err_no, log_path, format, ap);
Michal Vasko193dacd2022-10-13 08:43:05 +0200977 va_end(ap);
Radek Krejci0935f412019-08-20 16:15:18 +0200978}
979
Michal Vasko177d0ed2020-11-23 16:43:03 +0100980/**
Michal Vasko6727c682023-02-17 10:40:26 +0100981 * @brief Serves only for creating ap.
982 */
983static void
984_lyplg_ext_compile_log_err(const struct ly_err_item *err, const struct lysc_ext_instance *ext, ...)
985{
986 va_list ap;
987
988 va_start(ap, ext);
989 ly_ext_log(ext->module->ctx, ext->def->plugin->id, err->level, err->no, err->path ? strdup(err->path) : NULL, "%s", ap);
990 va_end(ap);
991}
992
993LIBYANG_API_DEF void
994lyplg_ext_compile_log_err(const struct ly_err_item *err, const struct lysc_ext_instance *ext)
995{
996 _lyplg_ext_compile_log_err(err, ext, err->msg);
997}
998
999/**
Michal Vaskoc78a6092021-05-07 15:27:35 +02001000 * @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 +01001001 */
1002static void
Michal Vaskoc78a6092021-05-07 15:27:35 +02001003_ly_err_print(const struct ly_ctx *ctx, struct ly_err_item *eitem, const char *format, ...)
Radek Krejci5aeea3a2018-09-05 13:29:36 +02001004{
Michal Vasko177d0ed2020-11-23 16:43:03 +01001005 va_list ap;
1006 char *path_dup = NULL;
1007
1008 LY_CHECK_ARG_RET(ctx, eitem, );
1009
1010 if (eitem->path) {
1011 /* duplicate path because it will be freed */
1012 path_dup = strdup(eitem->path);
1013 LY_CHECK_ERR_RET(!path_dup, LOGMEM(ctx), );
Radek Krejci5aeea3a2018-09-05 13:29:36 +02001014 }
Michal Vasko177d0ed2020-11-23 16:43:03 +01001015
Michal Vaskoc78a6092021-05-07 15:27:35 +02001016 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +02001017 log_vprintf(ctx, eitem->level, eitem->no, eitem->vecode, path_dup, eitem->apptag, format, ap);
Michal Vasko177d0ed2020-11-23 16:43:03 +01001018 va_end(ap);
Michal Vasko177d0ed2020-11-23 16:43:03 +01001019}
1020
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001021LIBYANG_API_DEF void
Michal Vasko177d0ed2020-11-23 16:43:03 +01001022ly_err_print(const struct ly_ctx *ctx, struct ly_err_item *eitem)
1023{
Michal Vaskoc78a6092021-05-07 15:27:35 +02001024 /* String ::ly_err_item.msg cannot be used directly because it may contain the % character */
1025 _ly_err_print(ctx, eitem, "%s", eitem->msg);
Radek Krejci5aeea3a2018-09-05 13:29:36 +02001026}