blob: 2a1f86256ee8f0fca18b8353957575cacec4ed20 [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;
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
Radek Krejci94aa9942018-09-07 17:12:17 +020050/* how many bytes add when enlarging buffers */
51#define LY_BUF_STEP 128
52
Jan Kundrátc53a7ec2021-12-09 16:01:19 +010053LIBYANG_API_DEF LY_ERR
Radek Krejcid33273d2018-10-25 14:55:52 +020054ly_errcode(const struct ly_ctx *ctx)
55{
56 struct ly_err_item *i;
57
Radek Krejci572ee602020-09-16 14:35:08 +020058 i = ly_err_last(ctx);
Radek Krejcid33273d2018-10-25 14:55:52 +020059 if (i) {
Radek Krejci572ee602020-09-16 14:35:08 +020060 return i->no;
Radek Krejcid33273d2018-10-25 14:55:52 +020061 }
62
63 return LY_SUCCESS;
64}
65
Jan Kundrátc53a7ec2021-12-09 16:01:19 +010066LIBYANG_API_DEF LY_VECODE
Radek Krejci5aeea3a2018-09-05 13:29:36 +020067ly_vecode(const struct ly_ctx *ctx)
68{
69 struct ly_err_item *i;
70
Radek Krejci572ee602020-09-16 14:35:08 +020071 i = ly_err_last(ctx);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020072 if (i) {
Radek Krejci572ee602020-09-16 14:35:08 +020073 return i->vecode;
Radek Krejci5aeea3a2018-09-05 13:29:36 +020074 }
75
76 return LYVE_SUCCESS;
77}
78
Jan Kundrátc53a7ec2021-12-09 16:01:19 +010079LIBYANG_API_DEF const char *
Radek Krejci5aeea3a2018-09-05 13:29:36 +020080ly_errmsg(const struct ly_ctx *ctx)
81{
82 struct ly_err_item *i;
83
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020084 LY_CHECK_ARG_RET(NULL, ctx, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020085
Radek Krejci572ee602020-09-16 14:35:08 +020086 i = ly_err_last(ctx);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020087 if (i) {
Radek Krejci572ee602020-09-16 14:35:08 +020088 return i->msg;
Radek Krejci5aeea3a2018-09-05 13:29:36 +020089 }
90
91 return NULL;
92}
93
Jan Kundrátc53a7ec2021-12-09 16:01:19 +010094LIBYANG_API_DEF const char *
Radek Krejci5aeea3a2018-09-05 13:29:36 +020095ly_errpath(const struct ly_ctx *ctx)
96{
97 struct ly_err_item *i;
98
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020099 LY_CHECK_ARG_RET(NULL, ctx, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200100
Radek Krejci572ee602020-09-16 14:35:08 +0200101 i = ly_err_last(ctx);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200102 if (i) {
Radek Krejci572ee602020-09-16 14:35:08 +0200103 return i->path;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200104 }
105
106 return NULL;
107}
108
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100109LIBYANG_API_DEF const char *
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200110ly_errapptag(const struct ly_ctx *ctx)
111{
112 struct ly_err_item *i;
113
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200114 LY_CHECK_ARG_RET(NULL, ctx, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200115
Radek Krejci572ee602020-09-16 14:35:08 +0200116 i = ly_err_last(ctx);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200117 if (i) {
Radek Krejci572ee602020-09-16 14:35:08 +0200118 return i->apptag;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200119 }
120
121 return NULL;
122}
123
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100124LIBYANG_API_DEF LY_ERR
aPiecek6d618552021-06-18 10:02:59 +0200125ly_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 +0200126{
Radek Krejcidb0ee022021-03-15 16:53:44 +0100127 char *msg = NULL;
128 struct ly_err_item *e;
Radek Krejcie7b95092019-05-15 11:03:07 +0200129
Radek Krejcid43298b2021-03-25 16:17:15 +0100130 if (!err || (ecode == LY_SUCCESS)) {
Radek Krejcidb0ee022021-03-15 16:53:44 +0100131 /* nothing to do */
132 return ecode;
133 }
134
135 e = malloc(sizeof *e);
136 LY_CHECK_ERR_RET(!e, LOGMEM(NULL), LY_EMEM);
137 e->prev = (*err) ? (*err)->prev : e;
138 e->next = NULL;
139 if (*err) {
140 (*err)->prev->next = e;
141 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200142
143 /* fill in the information */
Radek Krejcidb0ee022021-03-15 16:53:44 +0100144 e->level = LY_LLERR;
145 e->no = ecode;
146 e->vecode = vecode;
147 e->path = path;
148 e->apptag = apptag;
Radek Krejcie7b95092019-05-15 11:03:07 +0200149
aPiecek6d618552021-06-18 10:02:59 +0200150 if (err_format) {
Radek Krejcidb0ee022021-03-15 16:53:44 +0100151 va_list print_args;
152
aPiecek6d618552021-06-18 10:02:59 +0200153 va_start(print_args, err_format);
Radek Krejcidb0ee022021-03-15 16:53:44 +0100154
aPiecek6d618552021-06-18 10:02:59 +0200155 if (vasprintf(&msg, err_format, print_args) == -1) {
156 /* we don't have anything more to do, just set msg to NULL to avoid undefined content,
Radek Krejcidb0ee022021-03-15 16:53:44 +0100157 * still keep the information about the original error instead of LY_EMEM or other printf's error */
158 msg = NULL;
159 }
160
161 va_end(print_args);
162 }
163 e->msg = msg;
164
165 if (!(*err)) {
166 *err = e;
167 }
168
169 return e->no;
Radek Krejcie7b95092019-05-15 11:03:07 +0200170}
171
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100172LIBYANG_API_DEF struct ly_err_item *
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200173ly_err_first(const struct ly_ctx *ctx)
174{
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200175 LY_CHECK_ARG_RET(NULL, ctx, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200176
177 return pthread_getspecific(ctx->errlist_key);
178}
179
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100180LIBYANG_API_DEF struct ly_err_item *
Radek Krejci572ee602020-09-16 14:35:08 +0200181ly_err_last(const struct ly_ctx *ctx)
182{
183 const struct ly_err_item *e;
184
185 LY_CHECK_ARG_RET(NULL, ctx, NULL);
186
187 e = pthread_getspecific(ctx->errlist_key);
188 return e ? e->prev : NULL;
189}
190
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100191LIBYANG_API_DEF void
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200192ly_err_free(void *ptr)
193{
194 struct ly_err_item *i, *next;
195
196 /* clean the error list */
197 for (i = (struct ly_err_item *)ptr; i; i = next) {
198 next = i->next;
Radek Krejcidb0ee022021-03-15 16:53:44 +0100199 free(i->msg);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200200 free(i->path);
201 free(i->apptag);
202 free(i);
203 }
204}
205
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100206LIBYANG_API_DEF void
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200207ly_err_clean(struct ly_ctx *ctx, struct ly_err_item *eitem)
208{
209 struct ly_err_item *i, *first;
210
211 first = ly_err_first(ctx);
212 if (first == eitem) {
213 eitem = NULL;
214 }
215 if (eitem) {
216 /* disconnect the error */
Radek Krejci1e008d22020-08-17 11:37:37 +0200217 for (i = first; i && (i->next != eitem); i = i->next) {}
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200218 assert(i);
219 i->next = NULL;
220 first->prev = i;
221 /* free this err and newer */
222 ly_err_free(eitem);
223 } else {
224 /* free all err */
225 ly_err_free(first);
226 pthread_setspecific(ctx->errlist_key, NULL);
227 }
228}
229
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100230LIBYANG_API_DEF LY_LOG_LEVEL
Radek Krejci52b6d512020-10-12 12:33:17 +0200231ly_log_level(LY_LOG_LEVEL level)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200232{
Václav Kubernátd367ad92021-11-29 09:28:56 +0100233 LY_LOG_LEVEL prev = ATOMIC_LOAD_RELAXED(ly_ll);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200234
Václav Kubernátd367ad92021-11-29 09:28:56 +0100235 ATOMIC_STORE_RELAXED(ly_ll, level);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200236 return prev;
237}
238
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100239LIBYANG_API_DEF uint32_t
Radek Krejci1deb5be2020-08-26 16:43:36 +0200240ly_log_options(uint32_t opts)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200241{
Václav Kubernátd367ad92021-11-29 09:28:56 +0100242 uint32_t prev = ATOMIC_LOAD_RELAXED(ly_log_opts);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200243
Václav Kubernátd367ad92021-11-29 09:28:56 +0100244 ATOMIC_STORE_RELAXED(ly_log_opts, opts);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200245 return prev;
246}
247
Michal Vaskod4a6d042022-12-08 08:34:29 +0100248LIBYANG_API_DEF void
249ly_temp_log_options(uint32_t *opts)
250{
251 temp_ly_log_opts = opts;
252}
253
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100254LIBYANG_API_DEF uint32_t
Radek Krejci68433c92020-10-12 17:03:55 +0200255ly_log_dbg_groups(uint32_t dbg_groups)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200256{
257#ifndef NDEBUG
Václav Kubernátd367ad92021-11-29 09:28:56 +0100258 uint32_t prev = ATOMIC_LOAD_RELAXED(ly_ldbg_groups);
Radek Krejciebdaed02020-11-09 13:05:06 +0100259
Václav Kubernátd367ad92021-11-29 09:28:56 +0100260 ATOMIC_STORE_RELAXED(ly_ldbg_groups, dbg_groups);
Radek Krejciebdaed02020-11-09 13:05:06 +0100261 return prev;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200262#else
263 (void)dbg_groups;
Radek Krejciebdaed02020-11-09 13:05:06 +0100264 return 0;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200265#endif
266}
267
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100268LIBYANG_API_DEF void
Radek Krejci857189e2020-09-01 13:26:36 +0200269ly_set_log_clb(ly_log_clb clb, ly_bool path)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200270{
Michal Vaskod8085612020-08-21 12:55:23 +0200271 log_clb = clb;
Václav Kubernátd367ad92021-11-29 09:28:56 +0100272 ATOMIC_STORE_RELAXED(path_flag, path);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200273}
274
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100275LIBYANG_API_DEF ly_log_clb
Michal Vaskod8085612020-08-21 12:55:23 +0200276ly_get_log_clb(void)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200277{
Michal Vaskod8085612020-08-21 12:55:23 +0200278 return log_clb;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200279}
280
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100281void
Michal Vasko59e69e72022-02-18 09:18:21 +0100282ly_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 +0100283 uint64_t line)
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100284{
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100285 if (scnode) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100286 ly_set_add(&log_location.scnodes, (void *)scnode, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100287 }
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100288 if (dnode) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100289 ly_set_add(&log_location.dnodes, (void *)dnode, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100290 }
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100291 if (path) {
292 char *s = strdup(path);
Michal Vasko26bbb272022-08-02 14:54:33 +0200293
Radek Krejciddace2c2021-01-08 11:30:56 +0100294 LY_CHECK_ERR_RET(!s, LOGMEM(NULL), );
295 ly_set_add(&log_location.paths, s, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100296 }
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100297 if (in) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100298 ly_set_add(&log_location.inputs, (void *)in, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100299 }
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100300 if (line) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100301 log_location.line = line;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100302 }
303}
304
305void
Michal Vasko59e69e72022-02-18 09:18:21 +0100306ly_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 +0100307{
Radek Krejciddace2c2021-01-08 11:30:56 +0100308 for (uint32_t i = scnode_steps; i && log_location.scnodes.count; i--) {
309 log_location.scnodes.count--;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100310 }
311
Radek Krejciddace2c2021-01-08 11:30:56 +0100312 for (uint32_t i = dnode_steps; i && log_location.dnodes.count; i--) {
313 log_location.dnodes.count--;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100314 }
315
Radek Krejciddace2c2021-01-08 11:30:56 +0100316 for (uint32_t i = path_steps; i && log_location.paths.count; i--) {
317 ly_set_rm_index(&log_location.paths, log_location.paths.count - 1, free);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100318 }
319
Radek Krejciddace2c2021-01-08 11:30:56 +0100320 for (uint32_t i = in_steps; i && log_location.inputs.count; i--) {
321 log_location.inputs.count--;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100322 }
323
Radek Krejciddace2c2021-01-08 11:30:56 +0100324 /* deallocate the empty sets */
325 if (scnode_steps && !log_location.scnodes.count) {
326 ly_set_erase(&log_location.scnodes, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100327 }
Radek Krejciddace2c2021-01-08 11:30:56 +0100328 if (dnode_steps && !log_location.dnodes.count) {
329 ly_set_erase(&log_location.dnodes, NULL);
330 }
331 if (path_steps && !log_location.paths.count) {
332 ly_set_erase(&log_location.paths, free);
333 }
334 if (in_steps && !log_location.inputs.count) {
335 ly_set_erase(&log_location.inputs, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100336 }
337}
338
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200339static LY_ERR
340log_store(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, LY_VECODE vecode, char *msg, char *path, char *apptag)
341{
342 struct ly_err_item *eitem, *last;
343
344 assert(ctx && (level < LY_LLVRB));
345
346 eitem = pthread_getspecific(ctx->errlist_key);
347 if (!eitem) {
348 /* if we are only to fill in path, there must have been an error stored */
349 assert(msg);
350 eitem = malloc(sizeof *eitem);
351 LY_CHECK_GOTO(!eitem, mem_fail);
352 eitem->prev = eitem;
353 eitem->next = NULL;
354
355 pthread_setspecific(ctx->errlist_key, eitem);
356 } else if (!msg) {
357 /* only filling the path */
358 assert(path);
359
360 /* find last error */
361 eitem = eitem->prev;
362 do {
363 if (eitem->level == LY_LLERR) {
364 /* fill the path */
365 free(eitem->path);
366 eitem->path = path;
367 return LY_SUCCESS;
368 }
369 eitem = eitem->prev;
370 } while (eitem->prev->next);
371 /* last error was not found */
372 assert(0);
Michal Vaskod4a6d042022-12-08 08:34:29 +0100373 } else if ((temp_ly_log_opts && ((*temp_ly_log_opts & LY_LOSTORE_LAST) == LY_LOSTORE_LAST)) ||
374 (!temp_ly_log_opts && ((ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOSTORE_LAST) == LY_LOSTORE_LAST))) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200375 /* overwrite last message */
376 free(eitem->msg);
377 free(eitem->path);
378 free(eitem->apptag);
379 } else {
380 /* store new message */
381 last = eitem->prev;
382 eitem->prev = malloc(sizeof *eitem);
383 LY_CHECK_GOTO(!eitem->prev, mem_fail);
384 eitem = eitem->prev;
385 eitem->prev = last;
386 eitem->next = NULL;
387 last->next = eitem;
388 }
389
390 /* fill in the information */
391 eitem->level = level;
392 eitem->no = no;
393 eitem->vecode = vecode;
394 eitem->msg = msg;
395 eitem->path = path;
396 eitem->apptag = apptag;
397 return LY_SUCCESS;
398
399mem_fail:
400 LOGMEM(NULL);
401 free(msg);
402 free(path);
403 free(apptag);
404 return LY_EMEM;
405}
406
407static void
Michal Vaskoe9391c72021-10-05 10:04:56 +0200408log_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 +0200409 const char *format, va_list args)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200410{
411 char *msg = NULL;
Michal Vaskod4a6d042022-12-08 08:34:29 +0100412 ly_bool free_strs, lolog, lostore;
413
414 /* learn effective logger options */
415 if (temp_ly_log_opts) {
416 lolog = *temp_ly_log_opts & LY_LOLOG;
417 lostore = *temp_ly_log_opts & LY_LOSTORE;
418 } else {
419 lolog = ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOLOG;
420 lostore = ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOSTORE;
421 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200422
Václav Kubernátd367ad92021-11-29 09:28:56 +0100423 if (level > ATOMIC_LOAD_RELAXED(ly_ll)) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200424 /* do not print or store the message */
425 free(path);
426 return;
427 }
428
Michal Vasko5a016922021-05-07 08:19:15 +0200429 if (no == LY_EMEM) {
430 /* just print it, anything else would most likely fail anyway */
Michal Vaskod4a6d042022-12-08 08:34:29 +0100431 if (lolog) {
Michal Vasko5a016922021-05-07 08:19:15 +0200432 if (log_clb) {
433 log_clb(level, LY_EMEM_MSG, path);
434 } else {
435 fprintf(stderr, "libyang[%d]: ", level);
436 vfprintf(stderr, format, args);
437 if (path) {
438 fprintf(stderr, " (path: %s)\n", path);
439 } else {
440 fprintf(stderr, "\n");
441 }
442 }
443 }
444 free(path);
445 return;
446 }
447
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200448 /* store the error/warning (if we need to store errors internally, it does not matter what are the user log options) */
Michal Vaskod4a6d042022-12-08 08:34:29 +0100449 if ((level < LY_LLVRB) && ctx && lostore) {
Michal Vasko004d3152020-06-11 19:59:22 +0200450 assert(format);
451 if (vasprintf(&msg, format, args) == -1) {
452 LOGMEM(ctx);
453 free(path);
454 return;
455 }
Radek Krejcic9e64a62020-09-18 20:08:12 +0200456 if (((no & ~LY_EPLUGIN) == LY_EVALID) && (vecode == LYVE_SUCCESS)) {
457 /* assume we are inheriting the error, so inherit vecode as well */
458 vecode = ly_vecode(ctx);
459 }
Michal Vaskoe9391c72021-10-05 10:04:56 +0200460 if (log_store(ctx, level, no, vecode, msg, path, apptag ? strdup(apptag) : NULL)) {
Michal Vasko004d3152020-06-11 19:59:22 +0200461 return;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200462 }
463 free_strs = 0;
464 } else {
465 if (vasprintf(&msg, format, args) == -1) {
466 LOGMEM(ctx);
467 free(path);
468 return;
469 }
470 free_strs = 1;
471 }
472
473 /* if we are only storing errors internally, never print the message (yet) */
Michal Vaskod4a6d042022-12-08 08:34:29 +0100474 if (lolog) {
Michal Vaskod8085612020-08-21 12:55:23 +0200475 if (log_clb) {
476 log_clb(level, msg, path);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200477 } else {
478 fprintf(stderr, "libyang[%d]: %s%s", level, msg, path ? " " : "\n");
479 if (path) {
480 fprintf(stderr, "(path: %s)\n", path);
481 }
482 }
483 }
484
485 if (free_strs) {
486 free(path);
487 free(msg);
488 }
489}
490
Radek Krejci4ab61562018-09-05 15:00:37 +0200491#ifndef NDEBUG
492
493void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200494ly_log_dbg(uint32_t group, const char *format, ...)
Radek Krejci4ab61562018-09-05 15:00:37 +0200495{
496 char *dbg_format;
497 const char *str_group;
498 va_list ap;
499
Václav Kubernátd367ad92021-11-29 09:28:56 +0100500 if (!(ATOMIC_LOAD_RELAXED(ly_ldbg_groups) & group)) {
Radek Krejci4ab61562018-09-05 15:00:37 +0200501 return;
502 }
503
504 switch (group) {
505 case LY_LDGDICT:
506 str_group = "DICT";
507 break;
Radek Krejci4ab61562018-09-05 15:00:37 +0200508 case LY_LDGXPATH:
509 str_group = "XPATH";
510 break;
Michal Vaskoe558f792021-07-28 08:20:15 +0200511 case LY_LDGDEPSETS:
512 str_group = "DEPSETS";
513 break;
Radek Krejci4ab61562018-09-05 15:00:37 +0200514 default:
515 LOGINT(NULL);
516 return;
517 }
518
519 if (asprintf(&dbg_format, "%s: %s", str_group, format) == -1) {
520 LOGMEM(NULL);
521 return;
522 }
523
524 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200525 log_vprintf(NULL, LY_LLDBG, 0, 0, NULL, NULL, dbg_format, ap);
Radek Krejci4ab61562018-09-05 15:00:37 +0200526 va_end(ap);
527}
528
529#endif
530
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200531void
532ly_log(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, const char *format, ...)
533{
534 va_list ap;
535
536 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200537 log_vprintf(ctx, level, no, 0, NULL, NULL, format, ap);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200538 va_end(ap);
539}
540
Michal Vaskodbf3e652022-10-21 08:46:25 +0200541/**
542 * @brief Append a schema node name to a generated data path, only if it fits.
543 *
544 * @param[in,out] str Generated path to update.
545 * @param[in] snode Schema node to append.
546 * @param[in] parent Last printed data node.
547 * @return LY_ERR value.
548 */
549static LY_ERR
550ly_vlog_build_path_append(char **str, const struct lysc_node *snode, const struct lyd_node *parent)
551{
552 const struct lys_module *mod, *prev_mod;
553 uint32_t len, new_len;
554 void *mem;
555
556 if (snode->nodetype & (LYS_CHOICE | LYS_CASE)) {
557 /* schema-only node */
558 return LY_SUCCESS;
559 } else if (lysc_data_parent(snode) != parent->schema) {
560 /* not a direct descendant node */
561 return LY_SUCCESS;
562 }
563
564 /* get module to print, if any */
565 mod = snode->module;
566 prev_mod = (parent->schema) ? parent->schema->module : lyd_owner_module(parent);
567 if (prev_mod == mod) {
568 mod = NULL;
569 }
570
571 /* realloc string */
572 len = strlen(*str);
573 new_len = len + 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(snode->name);
574 mem = realloc(*str, new_len + 1);
575 LY_CHECK_ERR_RET(!mem, LOGMEM(LYD_CTX(parent)), LY_EMEM);
576 *str = mem;
577
578 /* print the last schema node */
579 sprintf(*str + len, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", snode->name);
580 return LY_SUCCESS;
581}
582
583/**
584 * @brief Build log path from the stored log location information.
585 *
586 * @param[in] ctx Context to use.
587 * @param[out] path Generated log path.
588 * @return LY_ERR value.
589 */
Radek Krejci94aa9942018-09-07 17:12:17 +0200590static LY_ERR
Radek Krejciddace2c2021-01-08 11:30:56 +0100591ly_vlog_build_path(const struct ly_ctx *ctx, char **path)
Radek Krejci94aa9942018-09-07 17:12:17 +0200592{
Michal Vaskodbf3e652022-10-21 08:46:25 +0200593 int r;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100594 char *str = NULL, *prev = NULL;
Michal Vaskodbf3e652022-10-21 08:46:25 +0200595 const struct lyd_node *dnode;
Radek Krejcicb3e6472021-01-06 08:19:01 +0100596
Radek Krejci2efc45b2020-12-22 16:25:44 +0100597 *path = NULL;
Radek Krejci94aa9942018-09-07 17:12:17 +0200598
Radek Krejciddace2c2021-01-08 11:30:56 +0100599 if (log_location.paths.count && ((const char *)(log_location.paths.objs[log_location.paths.count - 1]))[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100600 /* simply get what is in the provided path string */
Radek Krejciddace2c2021-01-08 11:30:56 +0100601 *path = strdup((const char *)log_location.paths.objs[log_location.paths.count - 1]);
Radek Krejcic04f0a22018-09-21 15:49:45 +0200602 LY_CHECK_ERR_RET(!(*path), LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100603 } else {
Michal Vaskodbf3e652022-10-21 08:46:25 +0200604 /* data/schema node */
605 if (log_location.dnodes.count) {
606 dnode = log_location.dnodes.objs[log_location.dnodes.count - 1];
Michal Vaskoa4dfb3c2022-10-25 14:59:31 +0200607 if (dnode->parent || !lysc_data_parent(dnode->schema)) {
608 /* data node with all of its parents */
609 str = lyd_path(log_location.dnodes.objs[log_location.dnodes.count - 1], LYD_PATH_STD, NULL, 0);
610 LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM);
611 } else {
Michal Vaskodbf3e652022-10-21 08:46:25 +0200612 /* data parsers put all the parent nodes in the set, but they are not connected */
613 str = lyd_path_set(&log_location.dnodes, LYD_PATH_STD);
Michal Vasko3e65ee32022-10-21 10:09:51 +0200614 LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM);
Michal Vaskoa4dfb3c2022-10-25 14:59:31 +0200615 }
Michal Vaskodbf3e652022-10-21 08:46:25 +0200616
Michal Vaskoa4dfb3c2022-10-25 14:59:31 +0200617 /* sometimes the last node is not created yet and we only have the schema node */
618 if (log_location.scnodes.count) {
619 ly_vlog_build_path_append(&str, log_location.scnodes.objs[log_location.scnodes.count - 1], dnode);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200620 }
Michal Vaskodbf3e652022-10-21 08:46:25 +0200621
622 r = asprintf(path, "Data location \"%s\"", str);
623 free(str);
624 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
625 } else if (log_location.scnodes.count) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100626 str = lysc_path(log_location.scnodes.objs[log_location.scnodes.count - 1], LYSC_PATH_LOG, NULL, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100627 LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM);
628
Michal Vaskodbf3e652022-10-21 08:46:25 +0200629 r = asprintf(path, "Schema location \"%s\"", str);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100630 free(str);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200631 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100632 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100633
Michal Vaskodbf3e652022-10-21 08:46:25 +0200634 /* line */
635 prev = *path;
Radek Krejciddace2c2021-01-08 11:30:56 +0100636 if (log_location.line) {
Michal Vaskodbf3e652022-10-21 08:46:25 +0200637 r = asprintf(path, "%s%sine number %" PRIu64, prev ? prev : "", prev ? ", l" : "L", log_location.line);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100638 free(prev);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200639 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100640
Radek Krejciddace2c2021-01-08 11:30:56 +0100641 log_location.line = 0;
642 } else if (log_location.inputs.count) {
Michal Vaskodbf3e652022-10-21 08:46:25 +0200643 r = asprintf(path, "%s%sine number %" PRIu64, prev ? prev : "", prev ? ", l" : "L",
Radek Krejciddace2c2021-01-08 11:30:56 +0100644 ((struct ly_in *)log_location.inputs.objs[log_location.inputs.count - 1])->line);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100645 free(prev);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200646 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100647 }
648
649 if (*path) {
650 prev = *path;
Michal Vaskodbf3e652022-10-21 08:46:25 +0200651 r = asprintf(path, "%s.", prev);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100652 free(prev);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200653 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100654 }
Radek Krejci94aa9942018-09-07 17:12:17 +0200655 }
656
Radek Krejci94aa9942018-09-07 17:12:17 +0200657 return LY_SUCCESS;
658}
659
660void
Michal Vaskoe9391c72021-10-05 10:04:56 +0200661ly_vlog(const struct ly_ctx *ctx, const char *apptag, LY_VECODE code, const char *format, ...)
Radek Krejci94aa9942018-09-07 17:12:17 +0200662{
663 va_list ap;
Michal Vasko22df3f02020-08-24 13:29:22 +0200664 char *path = NULL;
Radek Krejci94aa9942018-09-07 17:12:17 +0200665
Václav Kubernátd367ad92021-11-29 09:28:56 +0100666 if (ATOMIC_LOAD_RELAXED(path_flag) && ctx) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100667 ly_vlog_build_path(ctx, &path);
Radek Krejci94aa9942018-09-07 17:12:17 +0200668 }
669
670 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200671 log_vprintf(ctx, LY_LLERR, LY_EVALID, code, path, apptag, format, ap);
Radek Krejci94aa9942018-09-07 17:12:17 +0200672 /* path is spent and should not be freed! */
673 va_end(ap);
674}
675
Michal Vasko193dacd2022-10-13 08:43:05 +0200676/**
677 * @brief Print a log message from an extension plugin callback.
678 *
679 * @param[in] ctx libyang context to store the error record. If not provided, the error is just printed.
680 * @param[in] plugin_name Name of the plugin generating the message.
681 * @param[in] level Log message level (error, warning, etc.)
682 * @param[in] err_no Error type code.
683 * @param[in] path Optional path of the error.
684 * @param[in] format Format string to print.
685 * @param[in] ap Var arg list for @p format.
686 */
687static void
688ly_ext_log(const struct ly_ctx *ctx, const char *plugin_name, LY_LOG_LEVEL level, LY_ERR err_no, const char *path,
689 const char *format, va_list ap)
Radek Krejci0935f412019-08-20 16:15:18 +0200690{
Radek Krejci0935f412019-08-20 16:15:18 +0200691 char *plugin_msg;
Radek Krejci0935f412019-08-20 16:15:18 +0200692
Václav Kubernátd367ad92021-11-29 09:28:56 +0100693 if (ATOMIC_LOAD_RELAXED(ly_ll) < level) {
Radek Krejci0935f412019-08-20 16:15:18 +0200694 return;
695 }
Michal Vasko193dacd2022-10-13 08:43:05 +0200696 if (asprintf(&plugin_msg, "Ext plugin \"%s\": %s", plugin_name, format) == -1) {
697 LOGMEM(ctx);
Radek Krejci0935f412019-08-20 16:15:18 +0200698 return;
699 }
700
Michal Vasko193dacd2022-10-13 08:43:05 +0200701 log_vprintf(ctx, level, (level == LY_LLERR ? LY_EPLUGIN : 0) | err_no, LYVE_OTHER, path ? strdup(path) : NULL, NULL,
702 plugin_msg, ap);
703 free(plugin_msg);
704}
705
706LIBYANG_API_DEF void
707lyplg_ext_parse_log(const struct lysp_ctx *pctx, const struct lysp_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no,
708 const char *format, ...)
709{
710 va_list ap;
711 char *path = NULL;
712
713 if (ATOMIC_LOAD_RELAXED(path_flag)) {
714 ly_vlog_build_path(PARSER_CTX(pctx), &path);
715 }
716
Radek Krejci0935f412019-08-20 16:15:18 +0200717 va_start(ap, format);
Michal Vasko193dacd2022-10-13 08:43:05 +0200718 ly_ext_log(PARSER_CTX(pctx), ext->record->plugin.id, level, err_no, path, format, ap);
Radek Krejci0935f412019-08-20 16:15:18 +0200719 va_end(ap);
720
Michal Vasko193dacd2022-10-13 08:43:05 +0200721 free(path);
722}
723
724LIBYANG_API_DEF void
725lyplg_ext_compile_log(const struct lysc_ctx *cctx, const struct lysc_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no,
726 const char *format, ...)
727{
728 va_list ap;
729
730 va_start(ap, format);
731 ly_ext_log(ext->module->ctx, ext->def->plugin->id, level, err_no, cctx ? cctx->path : NULL, format, ap);
732 va_end(ap);
733}
734
735LIBYANG_API_DEF void
736lyplg_ext_compile_log_path(const char *path, const struct lysc_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no,
737 const char *format, ...)
738{
739 va_list ap;
740
741 va_start(ap, format);
742 ly_ext_log(ext->module->ctx, ext->def->plugin->id, level, err_no, path, format, ap);
743 va_end(ap);
Radek Krejci0935f412019-08-20 16:15:18 +0200744}
745
Michal Vasko177d0ed2020-11-23 16:43:03 +0100746/**
Michal Vaskoc78a6092021-05-07 15:27:35 +0200747 * @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 +0100748 */
749static void
Michal Vaskoc78a6092021-05-07 15:27:35 +0200750_ly_err_print(const struct ly_ctx *ctx, struct ly_err_item *eitem, const char *format, ...)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200751{
Michal Vasko177d0ed2020-11-23 16:43:03 +0100752 va_list ap;
753 char *path_dup = NULL;
754
755 LY_CHECK_ARG_RET(ctx, eitem, );
756
757 if (eitem->path) {
758 /* duplicate path because it will be freed */
759 path_dup = strdup(eitem->path);
760 LY_CHECK_ERR_RET(!path_dup, LOGMEM(ctx), );
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200761 }
Michal Vasko177d0ed2020-11-23 16:43:03 +0100762
Michal Vaskoc78a6092021-05-07 15:27:35 +0200763 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200764 log_vprintf(ctx, eitem->level, eitem->no, eitem->vecode, path_dup, eitem->apptag, format, ap);
Michal Vasko177d0ed2020-11-23 16:43:03 +0100765 va_end(ap);
Michal Vasko177d0ed2020-11-23 16:43:03 +0100766}
767
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100768LIBYANG_API_DEF void
Michal Vasko177d0ed2020-11-23 16:43:03 +0100769ly_err_print(const struct ly_ctx *ctx, struct ly_err_item *eitem)
770{
Michal Vaskoc78a6092021-05-07 15:27:35 +0200771 /* String ::ly_err_item.msg cannot be used directly because it may contain the % character */
772 _ly_err_print(ctx, eitem, "%s", eitem->msg);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200773}