blob: 90e66a08b7a90d82dd2465eb7abf0a0ada56b9d1 [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{
185 struct ly_ctx_err_rec rec, *match = NULL;
186
187 /* prepare record */
188 rec.tid = pthread_self();
189
190 /* get the pointer to the matching record */
191 lyht_find(ctx->err_ht, &rec, dict_hash((void *)&rec.tid, sizeof rec.tid), (void **)&match);
192
193 return match;
194}
195
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100196LIBYANG_API_DEF struct ly_err_item *
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200197ly_err_first(const struct ly_ctx *ctx)
198{
Michal Vasko88ccd582023-03-30 11:50:57 +0200199 struct ly_ctx_err_rec *rec;
200
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200201 LY_CHECK_ARG_RET(NULL, ctx, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200202
Michal Vasko88ccd582023-03-30 11:50:57 +0200203 /* get the pointer to the matching record */
204 rec = ly_err_get_rec(ctx);
205
206 return rec ? rec->err : NULL;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200207}
208
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100209LIBYANG_API_DEF struct ly_err_item *
Radek Krejci572ee602020-09-16 14:35:08 +0200210ly_err_last(const struct ly_ctx *ctx)
211{
Michal Vasko88ccd582023-03-30 11:50:57 +0200212 struct ly_ctx_err_rec *rec;
Radek Krejci572ee602020-09-16 14:35:08 +0200213
214 LY_CHECK_ARG_RET(NULL, ctx, NULL);
215
Michal Vasko88ccd582023-03-30 11:50:57 +0200216 /* get the pointer to the matching record */
217 if (!(rec = ly_err_get_rec(ctx))) {
218 return NULL;
219 }
220
221 return rec->err ? rec->err->prev : NULL;
Radek Krejci572ee602020-09-16 14:35:08 +0200222}
223
Michal Vasko9dbb91d2023-01-30 13:59:22 +0100224void
225ly_err_move(struct ly_ctx *src_ctx, struct ly_ctx *trg_ctx)
226{
Michal Vasko88ccd582023-03-30 11:50:57 +0200227 struct ly_ctx_err_rec *rec;
228 struct ly_err_item *err = NULL;
Michal Vasko9dbb91d2023-01-30 13:59:22 +0100229
Michal Vasko88ccd582023-03-30 11:50:57 +0200230 /* get and remove the errors from src */
231 rec = ly_err_get_rec(src_ctx);
232 if (rec) {
233 err = rec->err;
234 rec->err = NULL;
235 }
Michal Vasko9dbb91d2023-01-30 13:59:22 +0100236
237 /* set them for trg */
Michal Vasko88ccd582023-03-30 11:50:57 +0200238 rec = ly_err_get_rec(trg_ctx);
239 ly_err_free(rec->err);
240 rec->err = err;
Michal Vasko9dbb91d2023-01-30 13:59:22 +0100241}
242
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100243LIBYANG_API_DEF void
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200244ly_err_free(void *ptr)
245{
Michal Vasko88ccd582023-03-30 11:50:57 +0200246 struct ly_err_item *e, *next;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200247
248 /* clean the error list */
Michal Vasko88ccd582023-03-30 11:50:57 +0200249 LY_LIST_FOR_SAFE(ptr, next, e) {
250 free(e->msg);
251 free(e->path);
252 free(e->apptag);
253 free(e);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200254 }
255}
256
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100257LIBYANG_API_DEF void
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200258ly_err_clean(struct ly_ctx *ctx, struct ly_err_item *eitem)
259{
Michal Vasko88ccd582023-03-30 11:50:57 +0200260 struct ly_ctx_err_rec *rec;
261 struct ly_err_item *e;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200262
Michal Vasko88ccd582023-03-30 11:50:57 +0200263 if (!(rec = ly_err_get_rec(ctx))) {
264 return;
265 }
266 if (rec->err == eitem) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200267 eitem = NULL;
268 }
Michal Vasko88ccd582023-03-30 11:50:57 +0200269
270 if (!eitem) {
271 /* free all err */
272 ly_err_free(rec->err);
273 rec->err = NULL;
274 } else {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200275 /* disconnect the error */
Michal Vasko88ccd582023-03-30 11:50:57 +0200276 for (e = rec->err; e && (e->next != eitem); e = e->next) {}
277 assert(e);
278 e->next = NULL;
279 rec->err->prev = e;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200280 /* free this err and newer */
281 ly_err_free(eitem);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200282 }
283}
284
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100285LIBYANG_API_DEF LY_LOG_LEVEL
Radek Krejci52b6d512020-10-12 12:33:17 +0200286ly_log_level(LY_LOG_LEVEL level)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200287{
Václav Kubernátd367ad92021-11-29 09:28:56 +0100288 LY_LOG_LEVEL prev = ATOMIC_LOAD_RELAXED(ly_ll);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200289
Václav Kubernátd367ad92021-11-29 09:28:56 +0100290 ATOMIC_STORE_RELAXED(ly_ll, level);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200291 return prev;
292}
293
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100294LIBYANG_API_DEF uint32_t
Radek Krejci1deb5be2020-08-26 16:43:36 +0200295ly_log_options(uint32_t opts)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200296{
Václav Kubernátd367ad92021-11-29 09:28:56 +0100297 uint32_t prev = ATOMIC_LOAD_RELAXED(ly_log_opts);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200298
Václav Kubernátd367ad92021-11-29 09:28:56 +0100299 ATOMIC_STORE_RELAXED(ly_log_opts, opts);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200300 return prev;
301}
302
Michal Vaskod4a6d042022-12-08 08:34:29 +0100303LIBYANG_API_DEF void
304ly_temp_log_options(uint32_t *opts)
305{
306 temp_ly_log_opts = opts;
307}
308
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100309LIBYANG_API_DEF uint32_t
Radek Krejci68433c92020-10-12 17:03:55 +0200310ly_log_dbg_groups(uint32_t dbg_groups)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200311{
312#ifndef NDEBUG
Václav Kubernátd367ad92021-11-29 09:28:56 +0100313 uint32_t prev = ATOMIC_LOAD_RELAXED(ly_ldbg_groups);
Radek Krejciebdaed02020-11-09 13:05:06 +0100314
Václav Kubernátd367ad92021-11-29 09:28:56 +0100315 ATOMIC_STORE_RELAXED(ly_ldbg_groups, dbg_groups);
Radek Krejciebdaed02020-11-09 13:05:06 +0100316 return prev;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200317#else
318 (void)dbg_groups;
Radek Krejciebdaed02020-11-09 13:05:06 +0100319 return 0;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200320#endif
321}
322
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100323LIBYANG_API_DEF void
Radek Krejci857189e2020-09-01 13:26:36 +0200324ly_set_log_clb(ly_log_clb clb, ly_bool path)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200325{
Michal Vaskod8085612020-08-21 12:55:23 +0200326 log_clb = clb;
Václav Kubernátd367ad92021-11-29 09:28:56 +0100327 ATOMIC_STORE_RELAXED(path_flag, path);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200328}
329
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100330LIBYANG_API_DEF ly_log_clb
Michal Vaskod8085612020-08-21 12:55:23 +0200331ly_get_log_clb(void)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200332{
Michal Vaskod8085612020-08-21 12:55:23 +0200333 return log_clb;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200334}
335
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100336void
Michal Vasko59e69e72022-02-18 09:18:21 +0100337ly_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 +0100338 uint64_t line)
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100339{
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100340 if (scnode) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100341 ly_set_add(&log_location.scnodes, (void *)scnode, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100342 }
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100343 if (dnode) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100344 ly_set_add(&log_location.dnodes, (void *)dnode, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100345 }
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100346 if (path) {
347 char *s = strdup(path);
Michal Vasko26bbb272022-08-02 14:54:33 +0200348
Radek Krejciddace2c2021-01-08 11:30:56 +0100349 LY_CHECK_ERR_RET(!s, LOGMEM(NULL), );
350 ly_set_add(&log_location.paths, s, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100351 }
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100352 if (in) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100353 ly_set_add(&log_location.inputs, (void *)in, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100354 }
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100355 if (line) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100356 log_location.line = line;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100357 }
358}
359
360void
Michal Vasko59e69e72022-02-18 09:18:21 +0100361ly_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 +0100362{
Radek Krejciddace2c2021-01-08 11:30:56 +0100363 for (uint32_t i = scnode_steps; i && log_location.scnodes.count; i--) {
364 log_location.scnodes.count--;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100365 }
366
Radek Krejciddace2c2021-01-08 11:30:56 +0100367 for (uint32_t i = dnode_steps; i && log_location.dnodes.count; i--) {
368 log_location.dnodes.count--;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100369 }
370
Radek Krejciddace2c2021-01-08 11:30:56 +0100371 for (uint32_t i = path_steps; i && log_location.paths.count; i--) {
372 ly_set_rm_index(&log_location.paths, log_location.paths.count - 1, free);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100373 }
374
Radek Krejciddace2c2021-01-08 11:30:56 +0100375 for (uint32_t i = in_steps; i && log_location.inputs.count; i--) {
376 log_location.inputs.count--;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100377 }
378
Radek Krejciddace2c2021-01-08 11:30:56 +0100379 /* deallocate the empty sets */
380 if (scnode_steps && !log_location.scnodes.count) {
381 ly_set_erase(&log_location.scnodes, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100382 }
Radek Krejciddace2c2021-01-08 11:30:56 +0100383 if (dnode_steps && !log_location.dnodes.count) {
384 ly_set_erase(&log_location.dnodes, NULL);
385 }
386 if (path_steps && !log_location.paths.count) {
387 ly_set_erase(&log_location.paths, free);
388 }
389 if (in_steps && !log_location.inputs.count) {
390 ly_set_erase(&log_location.inputs, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100391 }
392}
393
Michal Vasko88ccd582023-03-30 11:50:57 +0200394/**
395 * @brief Store generated error in a context.
396 *
397 * @param[in] ctx Context to use.
398 * @param[in] level Message log level.
399 * @param[in] no Error number.
400 * @param[in] vecode Error validation error code.
401 * @param[in] msg Error message, always spent.
402 * @param[in] path Error path, always spent.
403 * @param[in] apptag Error app tag, always spent.
404 * @return LY_ERR value.
405 */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200406static LY_ERR
407log_store(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, LY_VECODE vecode, char *msg, char *path, char *apptag)
408{
Michal Vasko88ccd582023-03-30 11:50:57 +0200409 struct ly_ctx_err_rec *rec, new;
410 struct ly_err_item *e, *last;
411 LY_ERR r;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200412
413 assert(ctx && (level < LY_LLVRB));
414
Michal Vasko88ccd582023-03-30 11:50:57 +0200415 if (!(rec = ly_err_get_rec(ctx))) {
416 /* insert a new record */
417 new.err = NULL;
418 new.tid = pthread_self();
Michal Vasko48c00d82023-03-30 11:59:45 +0200419
420 /* reuse lock */
421 /* LOCK */
422 pthread_mutex_lock((pthread_mutex_t *)&ctx->lyb_hash_lock);
423
Michal Vasko88ccd582023-03-30 11:50:57 +0200424 r = lyht_insert(ctx->err_ht, &new, dict_hash((void *)&new.tid, sizeof new.tid), (void **)&rec);
Michal Vasko48c00d82023-03-30 11:59:45 +0200425
426 /* UNLOCK */
427 pthread_mutex_unlock((pthread_mutex_t *)&ctx->lyb_hash_lock);
428
Michal Vasko88ccd582023-03-30 11:50:57 +0200429 if (r) {
430 /* should never happen */
431 return r;
432 }
433 }
434
435 e = rec->err;
436 if (!e) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200437 /* if we are only to fill in path, there must have been an error stored */
438 assert(msg);
Michal Vasko88ccd582023-03-30 11:50:57 +0200439 e = malloc(sizeof *e);
440 LY_CHECK_GOTO(!e, mem_fail);
441 e->prev = e;
442 e->next = NULL;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200443
Michal Vasko88ccd582023-03-30 11:50:57 +0200444 rec->err = e;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200445 } else if (!msg) {
446 /* only filling the path */
447 assert(path);
448
449 /* find last error */
Michal Vasko88ccd582023-03-30 11:50:57 +0200450 e = e->prev;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200451 do {
Michal Vasko88ccd582023-03-30 11:50:57 +0200452 if (e->level == LY_LLERR) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200453 /* fill the path */
Michal Vasko88ccd582023-03-30 11:50:57 +0200454 free(e->path);
455 e->path = path;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200456 return LY_SUCCESS;
457 }
Michal Vasko88ccd582023-03-30 11:50:57 +0200458 e = e->prev;
459 } while (e->prev->next);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200460 /* last error was not found */
461 assert(0);
Michal Vaskod4a6d042022-12-08 08:34:29 +0100462 } else if ((temp_ly_log_opts && ((*temp_ly_log_opts & LY_LOSTORE_LAST) == LY_LOSTORE_LAST)) ||
463 (!temp_ly_log_opts && ((ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOSTORE_LAST) == LY_LOSTORE_LAST))) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200464 /* overwrite last message */
Michal Vasko88ccd582023-03-30 11:50:57 +0200465 free(e->msg);
466 free(e->path);
467 free(e->apptag);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200468 } else {
469 /* store new message */
Michal Vasko88ccd582023-03-30 11:50:57 +0200470 last = e->prev;
471 e->prev = malloc(sizeof *e);
472 LY_CHECK_GOTO(!e->prev, mem_fail);
473 e = e->prev;
474 e->prev = last;
475 e->next = NULL;
476 last->next = e;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200477 }
478
479 /* fill in the information */
Michal Vasko88ccd582023-03-30 11:50:57 +0200480 e->level = level;
481 e->no = no;
482 e->vecode = vecode;
483 e->msg = msg;
484 e->path = path;
485 e->apptag = apptag;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200486 return LY_SUCCESS;
487
488mem_fail:
489 LOGMEM(NULL);
490 free(msg);
491 free(path);
492 free(apptag);
493 return LY_EMEM;
494}
495
496static void
Michal Vaskoe9391c72021-10-05 10:04:56 +0200497log_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 +0200498 const char *format, va_list args)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200499{
500 char *msg = NULL;
Michal Vaskod4a6d042022-12-08 08:34:29 +0100501 ly_bool free_strs, lolog, lostore;
502
503 /* learn effective logger options */
504 if (temp_ly_log_opts) {
505 lolog = *temp_ly_log_opts & LY_LOLOG;
506 lostore = *temp_ly_log_opts & LY_LOSTORE;
507 } else {
508 lolog = ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOLOG;
509 lostore = ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOSTORE;
510 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200511
Václav Kubernátd367ad92021-11-29 09:28:56 +0100512 if (level > ATOMIC_LOAD_RELAXED(ly_ll)) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200513 /* do not print or store the message */
514 free(path);
515 return;
516 }
517
Michal Vasko5a016922021-05-07 08:19:15 +0200518 if (no == LY_EMEM) {
519 /* just print it, anything else would most likely fail anyway */
Michal Vaskod4a6d042022-12-08 08:34:29 +0100520 if (lolog) {
Michal Vasko5a016922021-05-07 08:19:15 +0200521 if (log_clb) {
522 log_clb(level, LY_EMEM_MSG, path);
523 } else {
524 fprintf(stderr, "libyang[%d]: ", level);
525 vfprintf(stderr, format, args);
526 if (path) {
527 fprintf(stderr, " (path: %s)\n", path);
528 } else {
529 fprintf(stderr, "\n");
530 }
531 }
532 }
533 free(path);
534 return;
535 }
536
Michal Vasko236cbac2023-02-10 15:45:37 +0100537 /* print into a single message */
538 if (vasprintf(&msg, format, args) == -1) {
539 LOGMEM(ctx);
540 free(path);
541 return;
542 }
543
544 /* store as the last message */
545 strncpy(last_msg, msg, LY_LAST_MSG_SIZE - 1);
546
547 /* store the error/warning in the context (if we need to store errors internally, it does not matter what are
548 * the user log options) */
Michal Vaskod4a6d042022-12-08 08:34:29 +0100549 if ((level < LY_LLVRB) && ctx && lostore) {
Radek Krejcic9e64a62020-09-18 20:08:12 +0200550 if (((no & ~LY_EPLUGIN) == LY_EVALID) && (vecode == LYVE_SUCCESS)) {
551 /* assume we are inheriting the error, so inherit vecode as well */
552 vecode = ly_vecode(ctx);
553 }
Michal Vaskoe9391c72021-10-05 10:04:56 +0200554 if (log_store(ctx, level, no, vecode, msg, path, apptag ? strdup(apptag) : NULL)) {
Michal Vasko004d3152020-06-11 19:59:22 +0200555 return;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200556 }
557 free_strs = 0;
558 } else {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200559 free_strs = 1;
560 }
561
562 /* if we are only storing errors internally, never print the message (yet) */
Michal Vaskod4a6d042022-12-08 08:34:29 +0100563 if (lolog) {
Michal Vaskod8085612020-08-21 12:55:23 +0200564 if (log_clb) {
565 log_clb(level, msg, path);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200566 } else {
567 fprintf(stderr, "libyang[%d]: %s%s", level, msg, path ? " " : "\n");
568 if (path) {
569 fprintf(stderr, "(path: %s)\n", path);
570 }
571 }
572 }
573
574 if (free_strs) {
575 free(path);
576 free(msg);
577 }
578}
579
Radek Krejci4ab61562018-09-05 15:00:37 +0200580#ifndef NDEBUG
581
582void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200583ly_log_dbg(uint32_t group, const char *format, ...)
Radek Krejci4ab61562018-09-05 15:00:37 +0200584{
585 char *dbg_format;
586 const char *str_group;
587 va_list ap;
588
Václav Kubernátd367ad92021-11-29 09:28:56 +0100589 if (!(ATOMIC_LOAD_RELAXED(ly_ldbg_groups) & group)) {
Radek Krejci4ab61562018-09-05 15:00:37 +0200590 return;
591 }
592
593 switch (group) {
594 case LY_LDGDICT:
595 str_group = "DICT";
596 break;
Radek Krejci4ab61562018-09-05 15:00:37 +0200597 case LY_LDGXPATH:
598 str_group = "XPATH";
599 break;
Michal Vaskoe558f792021-07-28 08:20:15 +0200600 case LY_LDGDEPSETS:
601 str_group = "DEPSETS";
602 break;
Radek Krejci4ab61562018-09-05 15:00:37 +0200603 default:
604 LOGINT(NULL);
605 return;
606 }
607
608 if (asprintf(&dbg_format, "%s: %s", str_group, format) == -1) {
609 LOGMEM(NULL);
610 return;
611 }
612
613 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200614 log_vprintf(NULL, LY_LLDBG, 0, 0, NULL, NULL, dbg_format, ap);
Radek Krejci4ab61562018-09-05 15:00:37 +0200615 va_end(ap);
616}
617
618#endif
619
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200620void
621ly_log(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, const char *format, ...)
622{
623 va_list ap;
624
625 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200626 log_vprintf(ctx, level, no, 0, NULL, NULL, format, ap);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200627 va_end(ap);
628}
629
Michal Vaskodbf3e652022-10-21 08:46:25 +0200630/**
631 * @brief Append a schema node name to a generated data path, only if it fits.
632 *
633 * @param[in,out] str Generated path to update.
634 * @param[in] snode Schema node to append.
635 * @param[in] parent Last printed data node.
636 * @return LY_ERR value.
637 */
638static LY_ERR
639ly_vlog_build_path_append(char **str, const struct lysc_node *snode, const struct lyd_node *parent)
640{
641 const struct lys_module *mod, *prev_mod;
642 uint32_t len, new_len;
643 void *mem;
644
645 if (snode->nodetype & (LYS_CHOICE | LYS_CASE)) {
646 /* schema-only node */
647 return LY_SUCCESS;
648 } else if (lysc_data_parent(snode) != parent->schema) {
649 /* not a direct descendant node */
650 return LY_SUCCESS;
651 }
652
653 /* get module to print, if any */
654 mod = snode->module;
655 prev_mod = (parent->schema) ? parent->schema->module : lyd_owner_module(parent);
656 if (prev_mod == mod) {
657 mod = NULL;
658 }
659
660 /* realloc string */
661 len = strlen(*str);
662 new_len = len + 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(snode->name);
663 mem = realloc(*str, new_len + 1);
664 LY_CHECK_ERR_RET(!mem, LOGMEM(LYD_CTX(parent)), LY_EMEM);
665 *str = mem;
666
667 /* print the last schema node */
668 sprintf(*str + len, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", snode->name);
669 return LY_SUCCESS;
670}
671
672/**
673 * @brief Build log path from the stored log location information.
674 *
675 * @param[in] ctx Context to use.
676 * @param[out] path Generated log path.
677 * @return LY_ERR value.
678 */
Radek Krejci94aa9942018-09-07 17:12:17 +0200679static LY_ERR
Radek Krejciddace2c2021-01-08 11:30:56 +0100680ly_vlog_build_path(const struct ly_ctx *ctx, char **path)
Radek Krejci94aa9942018-09-07 17:12:17 +0200681{
Michal Vaskodbf3e652022-10-21 08:46:25 +0200682 int r;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100683 char *str = NULL, *prev = NULL;
Michal Vaskodbf3e652022-10-21 08:46:25 +0200684 const struct lyd_node *dnode;
Radek Krejcicb3e6472021-01-06 08:19:01 +0100685
Radek Krejci2efc45b2020-12-22 16:25:44 +0100686 *path = NULL;
Radek Krejci94aa9942018-09-07 17:12:17 +0200687
Radek Krejciddace2c2021-01-08 11:30:56 +0100688 if (log_location.paths.count && ((const char *)(log_location.paths.objs[log_location.paths.count - 1]))[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100689 /* simply get what is in the provided path string */
Michal Vasko6727c682023-02-17 10:40:26 +0100690 r = asprintf(path, "Path \"%s\".", (const char *)log_location.paths.objs[log_location.paths.count - 1]);
691 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100692 } else {
Michal Vaskodbf3e652022-10-21 08:46:25 +0200693 /* data/schema node */
694 if (log_location.dnodes.count) {
695 dnode = log_location.dnodes.objs[log_location.dnodes.count - 1];
Michal Vaskoa4dfb3c2022-10-25 14:59:31 +0200696 if (dnode->parent || !lysc_data_parent(dnode->schema)) {
697 /* data node with all of its parents */
698 str = lyd_path(log_location.dnodes.objs[log_location.dnodes.count - 1], LYD_PATH_STD, NULL, 0);
699 LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM);
700 } else {
Michal Vaskodbf3e652022-10-21 08:46:25 +0200701 /* data parsers put all the parent nodes in the set, but they are not connected */
702 str = lyd_path_set(&log_location.dnodes, LYD_PATH_STD);
Michal Vasko3e65ee32022-10-21 10:09:51 +0200703 LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM);
Michal Vaskoa4dfb3c2022-10-25 14:59:31 +0200704 }
Michal Vaskodbf3e652022-10-21 08:46:25 +0200705
Michal Vaskoa4dfb3c2022-10-25 14:59:31 +0200706 /* sometimes the last node is not created yet and we only have the schema node */
707 if (log_location.scnodes.count) {
708 ly_vlog_build_path_append(&str, log_location.scnodes.objs[log_location.scnodes.count - 1], dnode);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200709 }
Michal Vaskodbf3e652022-10-21 08:46:25 +0200710
711 r = asprintf(path, "Data location \"%s\"", str);
712 free(str);
713 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
714 } else if (log_location.scnodes.count) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100715 str = lysc_path(log_location.scnodes.objs[log_location.scnodes.count - 1], LYSC_PATH_LOG, NULL, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100716 LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM);
717
Michal Vaskodbf3e652022-10-21 08:46:25 +0200718 r = asprintf(path, "Schema location \"%s\"", str);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100719 free(str);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200720 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100721 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100722
Michal Vaskodbf3e652022-10-21 08:46:25 +0200723 /* line */
724 prev = *path;
Radek Krejciddace2c2021-01-08 11:30:56 +0100725 if (log_location.line) {
Michal Vaskodbf3e652022-10-21 08:46:25 +0200726 r = asprintf(path, "%s%sine number %" PRIu64, prev ? prev : "", prev ? ", l" : "L", log_location.line);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100727 free(prev);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200728 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100729
Radek Krejciddace2c2021-01-08 11:30:56 +0100730 log_location.line = 0;
731 } else if (log_location.inputs.count) {
Michal Vaskodbf3e652022-10-21 08:46:25 +0200732 r = asprintf(path, "%s%sine number %" PRIu64, prev ? prev : "", prev ? ", l" : "L",
Radek Krejciddace2c2021-01-08 11:30:56 +0100733 ((struct ly_in *)log_location.inputs.objs[log_location.inputs.count - 1])->line);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100734 free(prev);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200735 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100736 }
737
738 if (*path) {
739 prev = *path;
Michal Vaskodbf3e652022-10-21 08:46:25 +0200740 r = asprintf(path, "%s.", prev);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100741 free(prev);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200742 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100743 }
Radek Krejci94aa9942018-09-07 17:12:17 +0200744 }
745
Radek Krejci94aa9942018-09-07 17:12:17 +0200746 return LY_SUCCESS;
747}
748
749void
Michal Vaskoe9391c72021-10-05 10:04:56 +0200750ly_vlog(const struct ly_ctx *ctx, const char *apptag, LY_VECODE code, const char *format, ...)
Radek Krejci94aa9942018-09-07 17:12:17 +0200751{
752 va_list ap;
Michal Vasko22df3f02020-08-24 13:29:22 +0200753 char *path = NULL;
Radek Krejci94aa9942018-09-07 17:12:17 +0200754
Václav Kubernátd367ad92021-11-29 09:28:56 +0100755 if (ATOMIC_LOAD_RELAXED(path_flag) && ctx) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100756 ly_vlog_build_path(ctx, &path);
Radek Krejci94aa9942018-09-07 17:12:17 +0200757 }
758
759 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200760 log_vprintf(ctx, LY_LLERR, LY_EVALID, code, path, apptag, format, ap);
Radek Krejci94aa9942018-09-07 17:12:17 +0200761 /* path is spent and should not be freed! */
762 va_end(ap);
763}
764
Michal Vasko193dacd2022-10-13 08:43:05 +0200765/**
766 * @brief Print a log message from an extension plugin callback.
767 *
768 * @param[in] ctx libyang context to store the error record. If not provided, the error is just printed.
769 * @param[in] plugin_name Name of the plugin generating the message.
770 * @param[in] level Log message level (error, warning, etc.)
771 * @param[in] err_no Error type code.
Michal Vasko6727c682023-02-17 10:40:26 +0100772 * @param[in] path Optional path of the error, used if set.
Michal Vasko193dacd2022-10-13 08:43:05 +0200773 * @param[in] format Format string to print.
774 * @param[in] ap Var arg list for @p format.
775 */
776static void
Michal Vasko6727c682023-02-17 10:40:26 +0100777ly_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 +0200778 const char *format, va_list ap)
Radek Krejci0935f412019-08-20 16:15:18 +0200779{
Radek Krejci0935f412019-08-20 16:15:18 +0200780 char *plugin_msg;
Radek Krejci0935f412019-08-20 16:15:18 +0200781
Václav Kubernátd367ad92021-11-29 09:28:56 +0100782 if (ATOMIC_LOAD_RELAXED(ly_ll) < level) {
Radek Krejci0935f412019-08-20 16:15:18 +0200783 return;
784 }
Michal Vasko193dacd2022-10-13 08:43:05 +0200785 if (asprintf(&plugin_msg, "Ext plugin \"%s\": %s", plugin_name, format) == -1) {
786 LOGMEM(ctx);
Radek Krejci0935f412019-08-20 16:15:18 +0200787 return;
788 }
789
Michal Vasko6727c682023-02-17 10:40:26 +0100790 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 +0200791 free(plugin_msg);
792}
793
794LIBYANG_API_DEF void
795lyplg_ext_parse_log(const struct lysp_ctx *pctx, const struct lysp_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no,
796 const char *format, ...)
797{
798 va_list ap;
799 char *path = NULL;
800
801 if (ATOMIC_LOAD_RELAXED(path_flag)) {
802 ly_vlog_build_path(PARSER_CTX(pctx), &path);
803 }
804
Radek Krejci0935f412019-08-20 16:15:18 +0200805 va_start(ap, format);
Michal Vasko193dacd2022-10-13 08:43:05 +0200806 ly_ext_log(PARSER_CTX(pctx), ext->record->plugin.id, level, err_no, path, format, ap);
Radek Krejci0935f412019-08-20 16:15:18 +0200807 va_end(ap);
Michal Vasko193dacd2022-10-13 08:43:05 +0200808}
809
810LIBYANG_API_DEF void
811lyplg_ext_compile_log(const struct lysc_ctx *cctx, const struct lysc_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no,
812 const char *format, ...)
813{
814 va_list ap;
Michal Vasko6727c682023-02-17 10:40:26 +0100815 char *path = NULL;
816
817 if (cctx && (asprintf(&path, "Path \"%s\".", cctx->path) == -1)) {
818 LOGMEM(cctx->ctx);
819 return;
820 }
Michal Vasko193dacd2022-10-13 08:43:05 +0200821
822 va_start(ap, format);
Michal Vasko6727c682023-02-17 10:40:26 +0100823 ly_ext_log(ext->module->ctx, ext->def->plugin->id, level, err_no, path, format, ap);
Michal Vasko193dacd2022-10-13 08:43:05 +0200824 va_end(ap);
825}
826
827LIBYANG_API_DEF void
828lyplg_ext_compile_log_path(const char *path, const struct lysc_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no,
829 const char *format, ...)
830{
831 va_list ap;
Michal Vasko6727c682023-02-17 10:40:26 +0100832 char *log_path = NULL;
833
834 if (path && (asprintf(&log_path, "Path \"%s\".", path) == -1)) {
835 LOGMEM(ext->module->ctx);
836 return;
837 }
Michal Vasko193dacd2022-10-13 08:43:05 +0200838
839 va_start(ap, format);
Michal Vasko6727c682023-02-17 10:40:26 +0100840 ly_ext_log(ext->module->ctx, ext->def->plugin->id, level, err_no, log_path, format, ap);
Michal Vasko193dacd2022-10-13 08:43:05 +0200841 va_end(ap);
Radek Krejci0935f412019-08-20 16:15:18 +0200842}
843
Michal Vasko177d0ed2020-11-23 16:43:03 +0100844/**
Michal Vasko6727c682023-02-17 10:40:26 +0100845 * @brief Serves only for creating ap.
846 */
847static void
848_lyplg_ext_compile_log_err(const struct ly_err_item *err, const struct lysc_ext_instance *ext, ...)
849{
850 va_list ap;
851
852 va_start(ap, ext);
853 ly_ext_log(ext->module->ctx, ext->def->plugin->id, err->level, err->no, err->path ? strdup(err->path) : NULL, "%s", ap);
854 va_end(ap);
855}
856
857LIBYANG_API_DEF void
858lyplg_ext_compile_log_err(const struct ly_err_item *err, const struct lysc_ext_instance *ext)
859{
860 _lyplg_ext_compile_log_err(err, ext, err->msg);
861}
862
863/**
Michal Vaskoc78a6092021-05-07 15:27:35 +0200864 * @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 +0100865 */
866static void
Michal Vaskoc78a6092021-05-07 15:27:35 +0200867_ly_err_print(const struct ly_ctx *ctx, struct ly_err_item *eitem, const char *format, ...)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200868{
Michal Vasko177d0ed2020-11-23 16:43:03 +0100869 va_list ap;
870 char *path_dup = NULL;
871
872 LY_CHECK_ARG_RET(ctx, eitem, );
873
874 if (eitem->path) {
875 /* duplicate path because it will be freed */
876 path_dup = strdup(eitem->path);
877 LY_CHECK_ERR_RET(!path_dup, LOGMEM(ctx), );
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200878 }
Michal Vasko177d0ed2020-11-23 16:43:03 +0100879
Michal Vaskoc78a6092021-05-07 15:27:35 +0200880 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200881 log_vprintf(ctx, eitem->level, eitem->no, eitem->vecode, path_dup, eitem->apptag, format, ap);
Michal Vasko177d0ed2020-11-23 16:43:03 +0100882 va_end(ap);
Michal Vasko177d0ed2020-11-23 16:43:03 +0100883}
884
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100885LIBYANG_API_DEF void
Michal Vasko177d0ed2020-11-23 16:43:03 +0100886ly_err_print(const struct ly_ctx *ctx, struct ly_err_item *eitem)
887{
Michal Vaskoc78a6092021-05-07 15:27:35 +0200888 /* String ::ly_err_item.msg cannot be used directly because it may contain the % character */
889 _ly_err_print(ctx, eitem, "%s", eitem->msg);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200890}