blob: 237138ef69b4017dd1be3608a58128dc48125057 [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
Michal Vasko9dbb91d2023-01-30 13:59:22 +0100191void
192ly_err_move(struct ly_ctx *src_ctx, struct ly_ctx *trg_ctx)
193{
194 const struct ly_err_item *e;
195
196 /* clear any current errors */
197 ly_err_clean(trg_ctx, NULL);
198
199 /* get the errors in src */
200 e = pthread_getspecific(src_ctx->errlist_key);
201 pthread_setspecific(src_ctx->errlist_key, NULL);
202
203 /* set them for trg */
204 pthread_setspecific(trg_ctx->errlist_key, e);
205}
206
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100207LIBYANG_API_DEF void
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200208ly_err_free(void *ptr)
209{
210 struct ly_err_item *i, *next;
211
212 /* clean the error list */
213 for (i = (struct ly_err_item *)ptr; i; i = next) {
214 next = i->next;
Radek Krejcidb0ee022021-03-15 16:53:44 +0100215 free(i->msg);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200216 free(i->path);
217 free(i->apptag);
218 free(i);
219 }
220}
221
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100222LIBYANG_API_DEF void
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200223ly_err_clean(struct ly_ctx *ctx, struct ly_err_item *eitem)
224{
225 struct ly_err_item *i, *first;
226
227 first = ly_err_first(ctx);
228 if (first == eitem) {
229 eitem = NULL;
230 }
231 if (eitem) {
232 /* disconnect the error */
Radek Krejci1e008d22020-08-17 11:37:37 +0200233 for (i = first; i && (i->next != eitem); i = i->next) {}
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200234 assert(i);
235 i->next = NULL;
236 first->prev = i;
237 /* free this err and newer */
238 ly_err_free(eitem);
239 } else {
240 /* free all err */
241 ly_err_free(first);
242 pthread_setspecific(ctx->errlist_key, NULL);
243 }
244}
245
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100246LIBYANG_API_DEF LY_LOG_LEVEL
Radek Krejci52b6d512020-10-12 12:33:17 +0200247ly_log_level(LY_LOG_LEVEL level)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200248{
Václav Kubernátd367ad92021-11-29 09:28:56 +0100249 LY_LOG_LEVEL prev = ATOMIC_LOAD_RELAXED(ly_ll);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200250
Václav Kubernátd367ad92021-11-29 09:28:56 +0100251 ATOMIC_STORE_RELAXED(ly_ll, level);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200252 return prev;
253}
254
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100255LIBYANG_API_DEF uint32_t
Radek Krejci1deb5be2020-08-26 16:43:36 +0200256ly_log_options(uint32_t opts)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200257{
Václav Kubernátd367ad92021-11-29 09:28:56 +0100258 uint32_t prev = ATOMIC_LOAD_RELAXED(ly_log_opts);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200259
Václav Kubernátd367ad92021-11-29 09:28:56 +0100260 ATOMIC_STORE_RELAXED(ly_log_opts, opts);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200261 return prev;
262}
263
Michal Vaskod4a6d042022-12-08 08:34:29 +0100264LIBYANG_API_DEF void
265ly_temp_log_options(uint32_t *opts)
266{
267 temp_ly_log_opts = opts;
268}
269
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100270LIBYANG_API_DEF uint32_t
Radek Krejci68433c92020-10-12 17:03:55 +0200271ly_log_dbg_groups(uint32_t dbg_groups)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200272{
273#ifndef NDEBUG
Václav Kubernátd367ad92021-11-29 09:28:56 +0100274 uint32_t prev = ATOMIC_LOAD_RELAXED(ly_ldbg_groups);
Radek Krejciebdaed02020-11-09 13:05:06 +0100275
Václav Kubernátd367ad92021-11-29 09:28:56 +0100276 ATOMIC_STORE_RELAXED(ly_ldbg_groups, dbg_groups);
Radek Krejciebdaed02020-11-09 13:05:06 +0100277 return prev;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200278#else
279 (void)dbg_groups;
Radek Krejciebdaed02020-11-09 13:05:06 +0100280 return 0;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200281#endif
282}
283
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100284LIBYANG_API_DEF void
Radek Krejci857189e2020-09-01 13:26:36 +0200285ly_set_log_clb(ly_log_clb clb, ly_bool path)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200286{
Michal Vaskod8085612020-08-21 12:55:23 +0200287 log_clb = clb;
Václav Kubernátd367ad92021-11-29 09:28:56 +0100288 ATOMIC_STORE_RELAXED(path_flag, path);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200289}
290
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100291LIBYANG_API_DEF ly_log_clb
Michal Vaskod8085612020-08-21 12:55:23 +0200292ly_get_log_clb(void)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200293{
Michal Vaskod8085612020-08-21 12:55:23 +0200294 return log_clb;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200295}
296
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100297void
Michal Vasko59e69e72022-02-18 09:18:21 +0100298ly_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 +0100299 uint64_t line)
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100300{
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100301 if (scnode) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100302 ly_set_add(&log_location.scnodes, (void *)scnode, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100303 }
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100304 if (dnode) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100305 ly_set_add(&log_location.dnodes, (void *)dnode, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100306 }
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100307 if (path) {
308 char *s = strdup(path);
Michal Vasko26bbb272022-08-02 14:54:33 +0200309
Radek Krejciddace2c2021-01-08 11:30:56 +0100310 LY_CHECK_ERR_RET(!s, LOGMEM(NULL), );
311 ly_set_add(&log_location.paths, s, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100312 }
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100313 if (in) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100314 ly_set_add(&log_location.inputs, (void *)in, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100315 }
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100316 if (line) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100317 log_location.line = line;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100318 }
319}
320
321void
Michal Vasko59e69e72022-02-18 09:18:21 +0100322ly_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 +0100323{
Radek Krejciddace2c2021-01-08 11:30:56 +0100324 for (uint32_t i = scnode_steps; i && log_location.scnodes.count; i--) {
325 log_location.scnodes.count--;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100326 }
327
Radek Krejciddace2c2021-01-08 11:30:56 +0100328 for (uint32_t i = dnode_steps; i && log_location.dnodes.count; i--) {
329 log_location.dnodes.count--;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100330 }
331
Radek Krejciddace2c2021-01-08 11:30:56 +0100332 for (uint32_t i = path_steps; i && log_location.paths.count; i--) {
333 ly_set_rm_index(&log_location.paths, log_location.paths.count - 1, free);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100334 }
335
Radek Krejciddace2c2021-01-08 11:30:56 +0100336 for (uint32_t i = in_steps; i && log_location.inputs.count; i--) {
337 log_location.inputs.count--;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100338 }
339
Radek Krejciddace2c2021-01-08 11:30:56 +0100340 /* deallocate the empty sets */
341 if (scnode_steps && !log_location.scnodes.count) {
342 ly_set_erase(&log_location.scnodes, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100343 }
Radek Krejciddace2c2021-01-08 11:30:56 +0100344 if (dnode_steps && !log_location.dnodes.count) {
345 ly_set_erase(&log_location.dnodes, NULL);
346 }
347 if (path_steps && !log_location.paths.count) {
348 ly_set_erase(&log_location.paths, free);
349 }
350 if (in_steps && !log_location.inputs.count) {
351 ly_set_erase(&log_location.inputs, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100352 }
353}
354
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200355static LY_ERR
356log_store(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, LY_VECODE vecode, char *msg, char *path, char *apptag)
357{
358 struct ly_err_item *eitem, *last;
359
360 assert(ctx && (level < LY_LLVRB));
361
362 eitem = pthread_getspecific(ctx->errlist_key);
363 if (!eitem) {
364 /* if we are only to fill in path, there must have been an error stored */
365 assert(msg);
366 eitem = malloc(sizeof *eitem);
367 LY_CHECK_GOTO(!eitem, mem_fail);
368 eitem->prev = eitem;
369 eitem->next = NULL;
370
371 pthread_setspecific(ctx->errlist_key, eitem);
372 } else if (!msg) {
373 /* only filling the path */
374 assert(path);
375
376 /* find last error */
377 eitem = eitem->prev;
378 do {
379 if (eitem->level == LY_LLERR) {
380 /* fill the path */
381 free(eitem->path);
382 eitem->path = path;
383 return LY_SUCCESS;
384 }
385 eitem = eitem->prev;
386 } while (eitem->prev->next);
387 /* last error was not found */
388 assert(0);
Michal Vaskod4a6d042022-12-08 08:34:29 +0100389 } else if ((temp_ly_log_opts && ((*temp_ly_log_opts & LY_LOSTORE_LAST) == LY_LOSTORE_LAST)) ||
390 (!temp_ly_log_opts && ((ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOSTORE_LAST) == LY_LOSTORE_LAST))) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200391 /* overwrite last message */
392 free(eitem->msg);
393 free(eitem->path);
394 free(eitem->apptag);
395 } else {
396 /* store new message */
397 last = eitem->prev;
398 eitem->prev = malloc(sizeof *eitem);
399 LY_CHECK_GOTO(!eitem->prev, mem_fail);
400 eitem = eitem->prev;
401 eitem->prev = last;
402 eitem->next = NULL;
403 last->next = eitem;
404 }
405
406 /* fill in the information */
407 eitem->level = level;
408 eitem->no = no;
409 eitem->vecode = vecode;
410 eitem->msg = msg;
411 eitem->path = path;
412 eitem->apptag = apptag;
413 return LY_SUCCESS;
414
415mem_fail:
416 LOGMEM(NULL);
417 free(msg);
418 free(path);
419 free(apptag);
420 return LY_EMEM;
421}
422
423static void
Michal Vaskoe9391c72021-10-05 10:04:56 +0200424log_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 +0200425 const char *format, va_list args)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200426{
427 char *msg = NULL;
Michal Vaskod4a6d042022-12-08 08:34:29 +0100428 ly_bool free_strs, lolog, lostore;
429
430 /* learn effective logger options */
431 if (temp_ly_log_opts) {
432 lolog = *temp_ly_log_opts & LY_LOLOG;
433 lostore = *temp_ly_log_opts & LY_LOSTORE;
434 } else {
435 lolog = ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOLOG;
436 lostore = ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOSTORE;
437 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200438
Václav Kubernátd367ad92021-11-29 09:28:56 +0100439 if (level > ATOMIC_LOAD_RELAXED(ly_ll)) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200440 /* do not print or store the message */
441 free(path);
442 return;
443 }
444
Michal Vasko5a016922021-05-07 08:19:15 +0200445 if (no == LY_EMEM) {
446 /* just print it, anything else would most likely fail anyway */
Michal Vaskod4a6d042022-12-08 08:34:29 +0100447 if (lolog) {
Michal Vasko5a016922021-05-07 08:19:15 +0200448 if (log_clb) {
449 log_clb(level, LY_EMEM_MSG, path);
450 } else {
451 fprintf(stderr, "libyang[%d]: ", level);
452 vfprintf(stderr, format, args);
453 if (path) {
454 fprintf(stderr, " (path: %s)\n", path);
455 } else {
456 fprintf(stderr, "\n");
457 }
458 }
459 }
460 free(path);
461 return;
462 }
463
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200464 /* 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 +0100465 if ((level < LY_LLVRB) && ctx && lostore) {
Michal Vasko004d3152020-06-11 19:59:22 +0200466 assert(format);
467 if (vasprintf(&msg, format, args) == -1) {
468 LOGMEM(ctx);
469 free(path);
470 return;
471 }
Radek Krejcic9e64a62020-09-18 20:08:12 +0200472 if (((no & ~LY_EPLUGIN) == LY_EVALID) && (vecode == LYVE_SUCCESS)) {
473 /* assume we are inheriting the error, so inherit vecode as well */
474 vecode = ly_vecode(ctx);
475 }
Michal Vaskoe9391c72021-10-05 10:04:56 +0200476 if (log_store(ctx, level, no, vecode, msg, path, apptag ? strdup(apptag) : NULL)) {
Michal Vasko004d3152020-06-11 19:59:22 +0200477 return;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200478 }
479 free_strs = 0;
480 } else {
481 if (vasprintf(&msg, format, args) == -1) {
482 LOGMEM(ctx);
483 free(path);
484 return;
485 }
486 free_strs = 1;
487 }
488
489 /* if we are only storing errors internally, never print the message (yet) */
Michal Vaskod4a6d042022-12-08 08:34:29 +0100490 if (lolog) {
Michal Vaskod8085612020-08-21 12:55:23 +0200491 if (log_clb) {
492 log_clb(level, msg, path);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200493 } else {
494 fprintf(stderr, "libyang[%d]: %s%s", level, msg, path ? " " : "\n");
495 if (path) {
496 fprintf(stderr, "(path: %s)\n", path);
497 }
498 }
499 }
500
501 if (free_strs) {
502 free(path);
503 free(msg);
504 }
505}
506
Radek Krejci4ab61562018-09-05 15:00:37 +0200507#ifndef NDEBUG
508
509void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200510ly_log_dbg(uint32_t group, const char *format, ...)
Radek Krejci4ab61562018-09-05 15:00:37 +0200511{
512 char *dbg_format;
513 const char *str_group;
514 va_list ap;
515
Václav Kubernátd367ad92021-11-29 09:28:56 +0100516 if (!(ATOMIC_LOAD_RELAXED(ly_ldbg_groups) & group)) {
Radek Krejci4ab61562018-09-05 15:00:37 +0200517 return;
518 }
519
520 switch (group) {
521 case LY_LDGDICT:
522 str_group = "DICT";
523 break;
Radek Krejci4ab61562018-09-05 15:00:37 +0200524 case LY_LDGXPATH:
525 str_group = "XPATH";
526 break;
Michal Vaskoe558f792021-07-28 08:20:15 +0200527 case LY_LDGDEPSETS:
528 str_group = "DEPSETS";
529 break;
Radek Krejci4ab61562018-09-05 15:00:37 +0200530 default:
531 LOGINT(NULL);
532 return;
533 }
534
535 if (asprintf(&dbg_format, "%s: %s", str_group, format) == -1) {
536 LOGMEM(NULL);
537 return;
538 }
539
540 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200541 log_vprintf(NULL, LY_LLDBG, 0, 0, NULL, NULL, dbg_format, ap);
Radek Krejci4ab61562018-09-05 15:00:37 +0200542 va_end(ap);
543}
544
545#endif
546
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200547void
548ly_log(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, const char *format, ...)
549{
550 va_list ap;
551
552 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200553 log_vprintf(ctx, level, no, 0, NULL, NULL, format, ap);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200554 va_end(ap);
555}
556
Michal Vaskodbf3e652022-10-21 08:46:25 +0200557/**
558 * @brief Append a schema node name to a generated data path, only if it fits.
559 *
560 * @param[in,out] str Generated path to update.
561 * @param[in] snode Schema node to append.
562 * @param[in] parent Last printed data node.
563 * @return LY_ERR value.
564 */
565static LY_ERR
566ly_vlog_build_path_append(char **str, const struct lysc_node *snode, const struct lyd_node *parent)
567{
568 const struct lys_module *mod, *prev_mod;
569 uint32_t len, new_len;
570 void *mem;
571
572 if (snode->nodetype & (LYS_CHOICE | LYS_CASE)) {
573 /* schema-only node */
574 return LY_SUCCESS;
575 } else if (lysc_data_parent(snode) != parent->schema) {
576 /* not a direct descendant node */
577 return LY_SUCCESS;
578 }
579
580 /* get module to print, if any */
581 mod = snode->module;
582 prev_mod = (parent->schema) ? parent->schema->module : lyd_owner_module(parent);
583 if (prev_mod == mod) {
584 mod = NULL;
585 }
586
587 /* realloc string */
588 len = strlen(*str);
589 new_len = len + 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(snode->name);
590 mem = realloc(*str, new_len + 1);
591 LY_CHECK_ERR_RET(!mem, LOGMEM(LYD_CTX(parent)), LY_EMEM);
592 *str = mem;
593
594 /* print the last schema node */
595 sprintf(*str + len, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", snode->name);
596 return LY_SUCCESS;
597}
598
599/**
600 * @brief Build log path from the stored log location information.
601 *
602 * @param[in] ctx Context to use.
603 * @param[out] path Generated log path.
604 * @return LY_ERR value.
605 */
Radek Krejci94aa9942018-09-07 17:12:17 +0200606static LY_ERR
Radek Krejciddace2c2021-01-08 11:30:56 +0100607ly_vlog_build_path(const struct ly_ctx *ctx, char **path)
Radek Krejci94aa9942018-09-07 17:12:17 +0200608{
Michal Vaskodbf3e652022-10-21 08:46:25 +0200609 int r;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100610 char *str = NULL, *prev = NULL;
Michal Vaskodbf3e652022-10-21 08:46:25 +0200611 const struct lyd_node *dnode;
Radek Krejcicb3e6472021-01-06 08:19:01 +0100612
Radek Krejci2efc45b2020-12-22 16:25:44 +0100613 *path = NULL;
Radek Krejci94aa9942018-09-07 17:12:17 +0200614
Radek Krejciddace2c2021-01-08 11:30:56 +0100615 if (log_location.paths.count && ((const char *)(log_location.paths.objs[log_location.paths.count - 1]))[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100616 /* simply get what is in the provided path string */
Radek Krejciddace2c2021-01-08 11:30:56 +0100617 *path = strdup((const char *)log_location.paths.objs[log_location.paths.count - 1]);
Radek Krejcic04f0a22018-09-21 15:49:45 +0200618 LY_CHECK_ERR_RET(!(*path), LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100619 } else {
Michal Vaskodbf3e652022-10-21 08:46:25 +0200620 /* data/schema node */
621 if (log_location.dnodes.count) {
622 dnode = log_location.dnodes.objs[log_location.dnodes.count - 1];
Michal Vaskoa4dfb3c2022-10-25 14:59:31 +0200623 if (dnode->parent || !lysc_data_parent(dnode->schema)) {
624 /* data node with all of its parents */
625 str = lyd_path(log_location.dnodes.objs[log_location.dnodes.count - 1], LYD_PATH_STD, NULL, 0);
626 LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM);
627 } else {
Michal Vaskodbf3e652022-10-21 08:46:25 +0200628 /* data parsers put all the parent nodes in the set, but they are not connected */
629 str = lyd_path_set(&log_location.dnodes, LYD_PATH_STD);
Michal Vasko3e65ee32022-10-21 10:09:51 +0200630 LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM);
Michal Vaskoa4dfb3c2022-10-25 14:59:31 +0200631 }
Michal Vaskodbf3e652022-10-21 08:46:25 +0200632
Michal Vaskoa4dfb3c2022-10-25 14:59:31 +0200633 /* sometimes the last node is not created yet and we only have the schema node */
634 if (log_location.scnodes.count) {
635 ly_vlog_build_path_append(&str, log_location.scnodes.objs[log_location.scnodes.count - 1], dnode);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200636 }
Michal Vaskodbf3e652022-10-21 08:46:25 +0200637
638 r = asprintf(path, "Data location \"%s\"", str);
639 free(str);
640 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
641 } else if (log_location.scnodes.count) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100642 str = lysc_path(log_location.scnodes.objs[log_location.scnodes.count - 1], LYSC_PATH_LOG, NULL, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100643 LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM);
644
Michal Vaskodbf3e652022-10-21 08:46:25 +0200645 r = asprintf(path, "Schema location \"%s\"", str);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100646 free(str);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200647 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100648 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100649
Michal Vaskodbf3e652022-10-21 08:46:25 +0200650 /* line */
651 prev = *path;
Radek Krejciddace2c2021-01-08 11:30:56 +0100652 if (log_location.line) {
Michal Vaskodbf3e652022-10-21 08:46:25 +0200653 r = asprintf(path, "%s%sine number %" PRIu64, prev ? prev : "", prev ? ", l" : "L", log_location.line);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100654 free(prev);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200655 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100656
Radek Krejciddace2c2021-01-08 11:30:56 +0100657 log_location.line = 0;
658 } else if (log_location.inputs.count) {
Michal Vaskodbf3e652022-10-21 08:46:25 +0200659 r = asprintf(path, "%s%sine number %" PRIu64, prev ? prev : "", prev ? ", l" : "L",
Radek Krejciddace2c2021-01-08 11:30:56 +0100660 ((struct ly_in *)log_location.inputs.objs[log_location.inputs.count - 1])->line);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100661 free(prev);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200662 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100663 }
664
665 if (*path) {
666 prev = *path;
Michal Vaskodbf3e652022-10-21 08:46:25 +0200667 r = asprintf(path, "%s.", prev);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100668 free(prev);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200669 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100670 }
Radek Krejci94aa9942018-09-07 17:12:17 +0200671 }
672
Radek Krejci94aa9942018-09-07 17:12:17 +0200673 return LY_SUCCESS;
674}
675
676void
Michal Vaskoe9391c72021-10-05 10:04:56 +0200677ly_vlog(const struct ly_ctx *ctx, const char *apptag, LY_VECODE code, const char *format, ...)
Radek Krejci94aa9942018-09-07 17:12:17 +0200678{
679 va_list ap;
Michal Vasko22df3f02020-08-24 13:29:22 +0200680 char *path = NULL;
Radek Krejci94aa9942018-09-07 17:12:17 +0200681
Václav Kubernátd367ad92021-11-29 09:28:56 +0100682 if (ATOMIC_LOAD_RELAXED(path_flag) && ctx) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100683 ly_vlog_build_path(ctx, &path);
Radek Krejci94aa9942018-09-07 17:12:17 +0200684 }
685
686 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200687 log_vprintf(ctx, LY_LLERR, LY_EVALID, code, path, apptag, format, ap);
Radek Krejci94aa9942018-09-07 17:12:17 +0200688 /* path is spent and should not be freed! */
689 va_end(ap);
690}
691
Michal Vasko193dacd2022-10-13 08:43:05 +0200692/**
693 * @brief Print a log message from an extension plugin callback.
694 *
695 * @param[in] ctx libyang context to store the error record. If not provided, the error is just printed.
696 * @param[in] plugin_name Name of the plugin generating the message.
697 * @param[in] level Log message level (error, warning, etc.)
698 * @param[in] err_no Error type code.
699 * @param[in] path Optional path of the error.
700 * @param[in] format Format string to print.
701 * @param[in] ap Var arg list for @p format.
702 */
703static void
704ly_ext_log(const struct ly_ctx *ctx, const char *plugin_name, LY_LOG_LEVEL level, LY_ERR err_no, const char *path,
705 const char *format, va_list ap)
Radek Krejci0935f412019-08-20 16:15:18 +0200706{
Radek Krejci0935f412019-08-20 16:15:18 +0200707 char *plugin_msg;
Radek Krejci0935f412019-08-20 16:15:18 +0200708
Václav Kubernátd367ad92021-11-29 09:28:56 +0100709 if (ATOMIC_LOAD_RELAXED(ly_ll) < level) {
Radek Krejci0935f412019-08-20 16:15:18 +0200710 return;
711 }
Michal Vasko193dacd2022-10-13 08:43:05 +0200712 if (asprintf(&plugin_msg, "Ext plugin \"%s\": %s", plugin_name, format) == -1) {
713 LOGMEM(ctx);
Radek Krejci0935f412019-08-20 16:15:18 +0200714 return;
715 }
716
Michal Vasko193dacd2022-10-13 08:43:05 +0200717 log_vprintf(ctx, level, (level == LY_LLERR ? LY_EPLUGIN : 0) | err_no, LYVE_OTHER, path ? strdup(path) : NULL, NULL,
718 plugin_msg, ap);
719 free(plugin_msg);
720}
721
722LIBYANG_API_DEF void
723lyplg_ext_parse_log(const struct lysp_ctx *pctx, const struct lysp_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no,
724 const char *format, ...)
725{
726 va_list ap;
727 char *path = NULL;
728
729 if (ATOMIC_LOAD_RELAXED(path_flag)) {
730 ly_vlog_build_path(PARSER_CTX(pctx), &path);
731 }
732
Radek Krejci0935f412019-08-20 16:15:18 +0200733 va_start(ap, format);
Michal Vasko193dacd2022-10-13 08:43:05 +0200734 ly_ext_log(PARSER_CTX(pctx), ext->record->plugin.id, level, err_no, path, format, ap);
Radek Krejci0935f412019-08-20 16:15:18 +0200735 va_end(ap);
736
Michal Vasko193dacd2022-10-13 08:43:05 +0200737 free(path);
738}
739
740LIBYANG_API_DEF void
741lyplg_ext_compile_log(const struct lysc_ctx *cctx, const struct lysc_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no,
742 const char *format, ...)
743{
744 va_list ap;
745
746 va_start(ap, format);
747 ly_ext_log(ext->module->ctx, ext->def->plugin->id, level, err_no, cctx ? cctx->path : NULL, format, ap);
748 va_end(ap);
749}
750
751LIBYANG_API_DEF void
752lyplg_ext_compile_log_path(const char *path, const struct lysc_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no,
753 const char *format, ...)
754{
755 va_list ap;
756
757 va_start(ap, format);
758 ly_ext_log(ext->module->ctx, ext->def->plugin->id, level, err_no, path, format, ap);
759 va_end(ap);
Radek Krejci0935f412019-08-20 16:15:18 +0200760}
761
Michal Vasko177d0ed2020-11-23 16:43:03 +0100762/**
Michal Vaskoc78a6092021-05-07 15:27:35 +0200763 * @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 +0100764 */
765static void
Michal Vaskoc78a6092021-05-07 15:27:35 +0200766_ly_err_print(const struct ly_ctx *ctx, struct ly_err_item *eitem, const char *format, ...)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200767{
Michal Vasko177d0ed2020-11-23 16:43:03 +0100768 va_list ap;
769 char *path_dup = NULL;
770
771 LY_CHECK_ARG_RET(ctx, eitem, );
772
773 if (eitem->path) {
774 /* duplicate path because it will be freed */
775 path_dup = strdup(eitem->path);
776 LY_CHECK_ERR_RET(!path_dup, LOGMEM(ctx), );
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200777 }
Michal Vasko177d0ed2020-11-23 16:43:03 +0100778
Michal Vaskoc78a6092021-05-07 15:27:35 +0200779 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200780 log_vprintf(ctx, eitem->level, eitem->no, eitem->vecode, path_dup, eitem->apptag, format, ap);
Michal Vasko177d0ed2020-11-23 16:43:03 +0100781 va_end(ap);
Michal Vasko177d0ed2020-11-23 16:43:03 +0100782}
783
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100784LIBYANG_API_DEF void
Michal Vasko177d0ed2020-11-23 16:43:03 +0100785ly_err_print(const struct ly_ctx *ctx, struct ly_err_item *eitem)
786{
Michal Vaskoc78a6092021-05-07 15:27:35 +0200787 /* String ::ly_err_item.msg cannot be used directly because it may contain the % character */
788 _ly_err_print(ctx, eitem, "%s", eitem->msg);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200789}