blob: 9cd5fd0d9c3931512c7b8e05f0a00254400e5bc1 [file] [log] [blame]
Radek Krejci5aeea3a2018-09-05 13:29:36 +02001/**
2 * @file log.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Logger routines implementations
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Christian Hopps32874e12021-05-01 09:43:54 -040015#define _GNU_SOURCE /* asprintf, strdup */
Radek Krejci535ea9f2020-05-29 16:01:05 +020016
17#include "log.h"
Radek Krejcib7db73a2018-10-24 14:18:40 +020018
Radek Krejci5aeea3a2018-09-05 13:29:36 +020019#include <assert.h>
Radek Krejcic04f0a22018-09-21 15:49:45 +020020#include <inttypes.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include <pthread.h>
Radek Krejci5aeea3a2018-09-05 13:29:36 +020022#include <stdarg.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include <stdint.h>
Radek Krejci5aeea3a2018-09-05 13:29:36 +020024#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include <stdlib.h>
26#include <string.h>
Radek Krejci5aeea3a2018-09-05 13:29:36 +020027
Radek Krejci535ea9f2020-05-29 16:01:05 +020028#include "common.h"
Radek Krejciaa45bda2020-07-20 07:43:38 +020029#include "compat.h"
Radek Krejciaddfc9a2020-12-17 20:46:35 +010030#include "in_internal.h"
Radek Krejci0935f412019-08-20 16:15:18 +020031#include "plugins_exts.h"
Radek Krejci77114102021-03-10 15:21:57 +010032#include "set.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020033#include "tree_data.h"
34#include "tree_schema.h"
Radek Krejci5aeea3a2018-09-05 13:29:36 +020035
Radek Krejci52b6d512020-10-12 12:33:17 +020036volatile LY_LOG_LEVEL ly_ll = LY_LLWRN;
Radek Krejci1deb5be2020-08-26 16:43:36 +020037volatile uint32_t ly_log_opts = LY_LOLOG | LY_LOSTORE_LAST;
Michal Vaskod8085612020-08-21 12:55:23 +020038static ly_log_clb log_clb;
Radek Krejci857189e2020-09-01 13:26:36 +020039static volatile ly_bool path_flag = 1;
Radek Krejci5aeea3a2018-09-05 13:29:36 +020040#ifndef NDEBUG
Radek Krejci68433c92020-10-12 17:03:55 +020041volatile uint32_t ly_ldbg_groups = 0;
Radek Krejci5aeea3a2018-09-05 13:29:36 +020042#endif
43
Radek Krejciddace2c2021-01-08 11:30:56 +010044THREAD_LOCAL struct ly_log_location_s log_location = {0};
45
Radek Krejci94aa9942018-09-07 17:12:17 +020046/* how many bytes add when enlarging buffers */
47#define LY_BUF_STEP 128
48
Radek Krejcid33273d2018-10-25 14:55:52 +020049API LY_ERR
50ly_errcode(const struct ly_ctx *ctx)
51{
52 struct ly_err_item *i;
53
Radek Krejci572ee602020-09-16 14:35:08 +020054 i = ly_err_last(ctx);
Radek Krejcid33273d2018-10-25 14:55:52 +020055 if (i) {
Radek Krejci572ee602020-09-16 14:35:08 +020056 return i->no;
Radek Krejcid33273d2018-10-25 14:55:52 +020057 }
58
59 return LY_SUCCESS;
60}
61
Radek Krejci5aeea3a2018-09-05 13:29:36 +020062API LY_VECODE
63ly_vecode(const struct ly_ctx *ctx)
64{
65 struct ly_err_item *i;
66
Radek Krejci572ee602020-09-16 14:35:08 +020067 i = ly_err_last(ctx);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020068 if (i) {
Radek Krejci572ee602020-09-16 14:35:08 +020069 return i->vecode;
Radek Krejci5aeea3a2018-09-05 13:29:36 +020070 }
71
72 return LYVE_SUCCESS;
73}
74
75API const char *
76ly_errmsg(const struct ly_ctx *ctx)
77{
78 struct ly_err_item *i;
79
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020080 LY_CHECK_ARG_RET(NULL, ctx, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020081
Radek Krejci572ee602020-09-16 14:35:08 +020082 i = ly_err_last(ctx);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020083 if (i) {
Radek Krejci572ee602020-09-16 14:35:08 +020084 return i->msg;
Radek Krejci5aeea3a2018-09-05 13:29:36 +020085 }
86
87 return NULL;
88}
89
90API const char *
91ly_errpath(const struct ly_ctx *ctx)
92{
93 struct ly_err_item *i;
94
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020095 LY_CHECK_ARG_RET(NULL, ctx, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020096
Radek Krejci572ee602020-09-16 14:35:08 +020097 i = ly_err_last(ctx);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020098 if (i) {
Radek Krejci572ee602020-09-16 14:35:08 +020099 return i->path;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200100 }
101
102 return NULL;
103}
104
105API const char *
106ly_errapptag(const struct ly_ctx *ctx)
107{
108 struct ly_err_item *i;
109
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200110 LY_CHECK_ARG_RET(NULL, ctx, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200111
Radek Krejci572ee602020-09-16 14:35:08 +0200112 i = ly_err_last(ctx);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200113 if (i) {
Radek Krejci572ee602020-09-16 14:35:08 +0200114 return i->apptag;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200115 }
116
117 return NULL;
118}
119
Radek Krejcidb0ee022021-03-15 16:53:44 +0100120API LY_ERR
121ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *path, char *apptag, const char *err_msg, ...)
Radek Krejcie7b95092019-05-15 11:03:07 +0200122{
Radek Krejcidb0ee022021-03-15 16:53:44 +0100123 char *msg = NULL;
124 struct ly_err_item *e;
Radek Krejcie7b95092019-05-15 11:03:07 +0200125
Radek Krejcid43298b2021-03-25 16:17:15 +0100126 if (!err || (ecode == LY_SUCCESS)) {
Radek Krejcidb0ee022021-03-15 16:53:44 +0100127 /* nothing to do */
128 return ecode;
129 }
130
131 e = malloc(sizeof *e);
132 LY_CHECK_ERR_RET(!e, LOGMEM(NULL), LY_EMEM);
133 e->prev = (*err) ? (*err)->prev : e;
134 e->next = NULL;
135 if (*err) {
136 (*err)->prev->next = e;
137 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200138
139 /* fill in the information */
Radek Krejcidb0ee022021-03-15 16:53:44 +0100140 e->level = LY_LLERR;
141 e->no = ecode;
142 e->vecode = vecode;
143 e->path = path;
144 e->apptag = apptag;
Radek Krejcie7b95092019-05-15 11:03:07 +0200145
Radek Krejcidb0ee022021-03-15 16:53:44 +0100146 if (err_msg) {
147 va_list print_args;
148
149 va_start(print_args, err_msg);
150
151 if (vasprintf(&msg, err_msg, print_args) == -1) {
152 /* we don't have anything more to do, just set err_msg to NULL to avoid undefined content,
153 * still keep the information about the original error instead of LY_EMEM or other printf's error */
154 msg = NULL;
155 }
156
157 va_end(print_args);
158 }
159 e->msg = msg;
160
161 if (!(*err)) {
162 *err = e;
163 }
164
165 return e->no;
Radek Krejcie7b95092019-05-15 11:03:07 +0200166}
167
168API struct ly_err_item *
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200169ly_err_first(const struct ly_ctx *ctx)
170{
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200171 LY_CHECK_ARG_RET(NULL, ctx, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200172
173 return pthread_getspecific(ctx->errlist_key);
174}
175
Radek Krejci572ee602020-09-16 14:35:08 +0200176API struct ly_err_item *
177ly_err_last(const struct ly_ctx *ctx)
178{
179 const struct ly_err_item *e;
180
181 LY_CHECK_ARG_RET(NULL, ctx, NULL);
182
183 e = pthread_getspecific(ctx->errlist_key);
184 return e ? e->prev : NULL;
185}
186
Radek Krejcie7b95092019-05-15 11:03:07 +0200187API void
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200188ly_err_free(void *ptr)
189{
190 struct ly_err_item *i, *next;
191
192 /* clean the error list */
193 for (i = (struct ly_err_item *)ptr; i; i = next) {
194 next = i->next;
Radek Krejcidb0ee022021-03-15 16:53:44 +0100195 free(i->msg);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200196 free(i->path);
197 free(i->apptag);
198 free(i);
199 }
200}
201
202API void
203ly_err_clean(struct ly_ctx *ctx, struct ly_err_item *eitem)
204{
205 struct ly_err_item *i, *first;
206
207 first = ly_err_first(ctx);
208 if (first == eitem) {
209 eitem = NULL;
210 }
211 if (eitem) {
212 /* disconnect the error */
Radek Krejci1e008d22020-08-17 11:37:37 +0200213 for (i = first; i && (i->next != eitem); i = i->next) {}
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200214 assert(i);
215 i->next = NULL;
216 first->prev = i;
217 /* free this err and newer */
218 ly_err_free(eitem);
219 } else {
220 /* free all err */
221 ly_err_free(first);
222 pthread_setspecific(ctx->errlist_key, NULL);
223 }
224}
225
226API LY_LOG_LEVEL
Radek Krejci52b6d512020-10-12 12:33:17 +0200227ly_log_level(LY_LOG_LEVEL level)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200228{
Radek Krejci52b6d512020-10-12 12:33:17 +0200229 LY_LOG_LEVEL prev = ly_ll;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200230
Radek Krejci52b6d512020-10-12 12:33:17 +0200231 ly_ll = level;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200232 return prev;
233}
234
Radek Krejci1deb5be2020-08-26 16:43:36 +0200235API uint32_t
236ly_log_options(uint32_t opts)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200237{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200238 uint32_t prev = ly_log_opts;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200239
240 ly_log_opts = opts;
241 return prev;
242}
243
Radek Krejciebdaed02020-11-09 13:05:06 +0100244API uint32_t
Radek Krejci68433c92020-10-12 17:03:55 +0200245ly_log_dbg_groups(uint32_t dbg_groups)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200246{
247#ifndef NDEBUG
Radek Krejciebdaed02020-11-09 13:05:06 +0100248 uint32_t prev = ly_ldbg_groups;
249
Radek Krejci68433c92020-10-12 17:03:55 +0200250 ly_ldbg_groups = dbg_groups;
Radek Krejciebdaed02020-11-09 13:05:06 +0100251 return prev;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200252#else
253 (void)dbg_groups;
Radek Krejciebdaed02020-11-09 13:05:06 +0100254 return 0;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200255#endif
256}
257
258API void
Radek Krejci857189e2020-09-01 13:26:36 +0200259ly_set_log_clb(ly_log_clb clb, ly_bool path)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200260{
Michal Vaskod8085612020-08-21 12:55:23 +0200261 log_clb = clb;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200262 path_flag = path;
263}
264
Michal Vaskod8085612020-08-21 12:55:23 +0200265API ly_log_clb
266ly_get_log_clb(void)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200267{
Michal Vaskod8085612020-08-21 12:55:23 +0200268 return log_clb;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200269}
270
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100271void
Radek Krejciddace2c2021-01-08 11:30:56 +0100272ly_log_location(const struct lysc_node *scnode, const struct lyd_node *dnode,
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100273 const char *path, const struct ly_in *in, uint64_t line, ly_bool reset)
274{
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100275 if (scnode) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100276 ly_set_add(&log_location.scnodes, (void *)scnode, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100277 } else if (reset) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100278 ly_set_erase(&log_location.scnodes, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100279 }
280
281 if (dnode) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100282 ly_set_add(&log_location.dnodes, (void *)dnode, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100283 } else if (reset) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100284 ly_set_erase(&log_location.dnodes, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100285 }
286
287 if (path) {
288 char *s = strdup(path);
Radek Krejciddace2c2021-01-08 11:30:56 +0100289 LY_CHECK_ERR_RET(!s, LOGMEM(NULL), );
290 ly_set_add(&log_location.paths, s, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100291 } else if (reset) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100292 ly_set_erase(&log_location.paths, free);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100293 }
294
295 if (in) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100296 ly_set_add(&log_location.inputs, (void *)in, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100297 } else if (reset) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100298 ly_set_erase(&log_location.inputs, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100299 }
300
301 if (line) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100302 log_location.line = line;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100303 }
304}
305
306void
Radek Krejciddace2c2021-01-08 11:30:56 +0100307ly_log_location_revert(uint32_t scnode_steps, uint32_t dnode_steps,
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100308 uint32_t path_steps, uint32_t in_steps)
309{
Radek Krejciddace2c2021-01-08 11:30:56 +0100310 for (uint32_t i = scnode_steps; i && log_location.scnodes.count; i--) {
311 log_location.scnodes.count--;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100312 }
313
Radek Krejciddace2c2021-01-08 11:30:56 +0100314 for (uint32_t i = dnode_steps; i && log_location.dnodes.count; i--) {
315 log_location.dnodes.count--;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100316 }
317
Radek Krejciddace2c2021-01-08 11:30:56 +0100318 for (uint32_t i = path_steps; i && log_location.paths.count; i--) {
319 ly_set_rm_index(&log_location.paths, log_location.paths.count - 1, free);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100320 }
321
Radek Krejciddace2c2021-01-08 11:30:56 +0100322 for (uint32_t i = in_steps; i && log_location.inputs.count; i--) {
323 log_location.inputs.count--;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100324 }
325
Radek Krejciddace2c2021-01-08 11:30:56 +0100326 /* deallocate the empty sets */
327 if (scnode_steps && !log_location.scnodes.count) {
328 ly_set_erase(&log_location.scnodes, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100329 }
Radek Krejciddace2c2021-01-08 11:30:56 +0100330 if (dnode_steps && !log_location.dnodes.count) {
331 ly_set_erase(&log_location.dnodes, NULL);
332 }
333 if (path_steps && !log_location.paths.count) {
334 ly_set_erase(&log_location.paths, free);
335 }
336 if (in_steps && !log_location.inputs.count) {
337 ly_set_erase(&log_location.inputs, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100338 }
339}
340
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200341static LY_ERR
342log_store(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, LY_VECODE vecode, char *msg, char *path, char *apptag)
343{
344 struct ly_err_item *eitem, *last;
345
346 assert(ctx && (level < LY_LLVRB));
347
348 eitem = pthread_getspecific(ctx->errlist_key);
349 if (!eitem) {
350 /* if we are only to fill in path, there must have been an error stored */
351 assert(msg);
352 eitem = malloc(sizeof *eitem);
353 LY_CHECK_GOTO(!eitem, mem_fail);
354 eitem->prev = eitem;
355 eitem->next = NULL;
356
357 pthread_setspecific(ctx->errlist_key, eitem);
358 } else if (!msg) {
359 /* only filling the path */
360 assert(path);
361
362 /* find last error */
363 eitem = eitem->prev;
364 do {
365 if (eitem->level == LY_LLERR) {
366 /* fill the path */
367 free(eitem->path);
368 eitem->path = path;
369 return LY_SUCCESS;
370 }
371 eitem = eitem->prev;
372 } while (eitem->prev->next);
373 /* last error was not found */
374 assert(0);
Michal Vaskoed94a292019-11-06 15:43:41 +0100375 } else if ((ly_log_opts & LY_LOSTORE_LAST) == LY_LOSTORE_LAST) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200376 /* overwrite last message */
377 free(eitem->msg);
378 free(eitem->path);
379 free(eitem->apptag);
380 } else {
381 /* store new message */
382 last = eitem->prev;
383 eitem->prev = malloc(sizeof *eitem);
384 LY_CHECK_GOTO(!eitem->prev, mem_fail);
385 eitem = eitem->prev;
386 eitem->prev = last;
387 eitem->next = NULL;
388 last->next = eitem;
389 }
390
391 /* fill in the information */
392 eitem->level = level;
393 eitem->no = no;
394 eitem->vecode = vecode;
395 eitem->msg = msg;
396 eitem->path = path;
397 eitem->apptag = apptag;
398 return LY_SUCCESS;
399
400mem_fail:
401 LOGMEM(NULL);
402 free(msg);
403 free(path);
404 free(apptag);
405 return LY_EMEM;
406}
407
408static void
409log_vprintf(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, LY_VECODE vecode, char *path,
Radek Krejci0f969882020-08-21 16:56:47 +0200410 const char *format, va_list args)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200411{
412 char *msg = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200413 ly_bool free_strs;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200414
Radek Krejci52b6d512020-10-12 12:33:17 +0200415 if (level > ly_ll) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200416 /* do not print or store the message */
417 free(path);
418 return;
419 }
420
Michal Vasko5a016922021-05-07 08:19:15 +0200421 if (no == LY_EMEM) {
422 /* just print it, anything else would most likely fail anyway */
423 if (ly_log_opts & LY_LOLOG) {
424 if (log_clb) {
425 log_clb(level, LY_EMEM_MSG, path);
426 } else {
427 fprintf(stderr, "libyang[%d]: ", level);
428 vfprintf(stderr, format, args);
429 if (path) {
430 fprintf(stderr, " (path: %s)\n", path);
431 } else {
432 fprintf(stderr, "\n");
433 }
434 }
435 }
436 free(path);
437 return;
438 }
439
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200440 /* store the error/warning (if we need to store errors internally, it does not matter what are the user log options) */
Michal Vaskoed94a292019-11-06 15:43:41 +0100441 if ((level < LY_LLVRB) && ctx && (ly_log_opts & LY_LOSTORE)) {
Michal Vasko004d3152020-06-11 19:59:22 +0200442 assert(format);
443 if (vasprintf(&msg, format, args) == -1) {
444 LOGMEM(ctx);
445 free(path);
446 return;
447 }
Radek Krejcic9e64a62020-09-18 20:08:12 +0200448 if (((no & ~LY_EPLUGIN) == LY_EVALID) && (vecode == LYVE_SUCCESS)) {
449 /* assume we are inheriting the error, so inherit vecode as well */
450 vecode = ly_vecode(ctx);
451 }
Michal Vasko004d3152020-06-11 19:59:22 +0200452 if (log_store(ctx, level, no, vecode, msg, path, NULL)) {
453 return;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200454 }
455 free_strs = 0;
456 } else {
457 if (vasprintf(&msg, format, args) == -1) {
458 LOGMEM(ctx);
459 free(path);
460 return;
461 }
462 free_strs = 1;
463 }
464
465 /* if we are only storing errors internally, never print the message (yet) */
Michal Vaskoed94a292019-11-06 15:43:41 +0100466 if (ly_log_opts & LY_LOLOG) {
Michal Vaskod8085612020-08-21 12:55:23 +0200467 if (log_clb) {
468 log_clb(level, msg, path);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200469 } else {
470 fprintf(stderr, "libyang[%d]: %s%s", level, msg, path ? " " : "\n");
471 if (path) {
472 fprintf(stderr, "(path: %s)\n", path);
473 }
474 }
475 }
476
477 if (free_strs) {
478 free(path);
479 free(msg);
480 }
481}
482
Radek Krejci4ab61562018-09-05 15:00:37 +0200483#ifndef NDEBUG
484
485void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200486ly_log_dbg(uint32_t group, const char *format, ...)
Radek Krejci4ab61562018-09-05 15:00:37 +0200487{
488 char *dbg_format;
489 const char *str_group;
490 va_list ap;
491
Radek Krejci68433c92020-10-12 17:03:55 +0200492 if (!(ly_ldbg_groups & group)) {
Radek Krejci4ab61562018-09-05 15:00:37 +0200493 return;
494 }
495
496 switch (group) {
497 case LY_LDGDICT:
498 str_group = "DICT";
499 break;
Radek Krejci4ab61562018-09-05 15:00:37 +0200500 case LY_LDGXPATH:
501 str_group = "XPATH";
502 break;
Radek Krejci4ab61562018-09-05 15:00:37 +0200503 default:
504 LOGINT(NULL);
505 return;
506 }
507
508 if (asprintf(&dbg_format, "%s: %s", str_group, format) == -1) {
509 LOGMEM(NULL);
510 return;
511 }
512
513 va_start(ap, format);
514 log_vprintf(NULL, LY_LLDBG, 0, 0, NULL, dbg_format, ap);
515 va_end(ap);
516}
517
518#endif
519
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200520void
521ly_log(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, const char *format, ...)
522{
523 va_list ap;
524
525 va_start(ap, format);
526 log_vprintf(ctx, level, no, 0, NULL, format, ap);
527 va_end(ap);
528}
529
Radek Krejci94aa9942018-09-07 17:12:17 +0200530static LY_ERR
Radek Krejciddace2c2021-01-08 11:30:56 +0100531ly_vlog_build_path(const struct ly_ctx *ctx, char **path)
Radek Krejci94aa9942018-09-07 17:12:17 +0200532{
Radek Krejcic04f0a22018-09-21 15:49:45 +0200533 int rc;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100534 char *str = NULL, *prev = NULL;
Radek Krejcicb3e6472021-01-06 08:19:01 +0100535
Radek Krejci2efc45b2020-12-22 16:25:44 +0100536 *path = NULL;
Radek Krejci94aa9942018-09-07 17:12:17 +0200537
Radek Krejciddace2c2021-01-08 11:30:56 +0100538 if (log_location.paths.count && ((const char *)(log_location.paths.objs[log_location.paths.count - 1]))[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100539 /* simply get what is in the provided path string */
Radek Krejciddace2c2021-01-08 11:30:56 +0100540 *path = strdup((const char *)log_location.paths.objs[log_location.paths.count - 1]);
Radek Krejcic04f0a22018-09-21 15:49:45 +0200541 LY_CHECK_ERR_RET(!(*path), LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100542 } else {
543 /* generate location string */
Radek Krejciddace2c2021-01-08 11:30:56 +0100544 if (log_location.scnodes.count) {
545 str = lysc_path(log_location.scnodes.objs[log_location.scnodes.count - 1], LYSC_PATH_LOG, NULL, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100546 LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM);
547
548 rc = asprintf(path, "Schema location %s", str);
549 free(str);
550 LY_CHECK_ERR_RET(rc == -1, LOGMEM(ctx), LY_EMEM);
551 }
Radek Krejciddace2c2021-01-08 11:30:56 +0100552 if (log_location.dnodes.count) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100553 prev = *path;
Radek Krejciddace2c2021-01-08 11:30:56 +0100554 str = lyd_path(log_location.dnodes.objs[log_location.dnodes.count - 1], LYD_PATH_STD, NULL, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100555 LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM);
556
557 rc = asprintf(path, "%s%sata location %s", prev ? prev : "", prev ? ", d" : "D", str);
558 free(str);
559 free(prev);
560 LY_CHECK_ERR_RET(rc == -1, LOGMEM(ctx), LY_EMEM);
561 }
Radek Krejciddace2c2021-01-08 11:30:56 +0100562 if (log_location.line) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100563 prev = *path;
Radek Krejciddace2c2021-01-08 11:30:56 +0100564 rc = asprintf(path, "%s%sine number %" PRIu64, prev ? prev : "", prev ? ", l" : "L", log_location.line);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100565 free(prev);
566 LY_CHECK_ERR_RET(rc == -1, LOGMEM(ctx), LY_EMEM);
567
Radek Krejciddace2c2021-01-08 11:30:56 +0100568 log_location.line = 0;
569 } else if (log_location.inputs.count) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100570 prev = *path;
571 rc = asprintf(path, "%s%sine number %" PRIu64, prev ? prev : "", prev ? ", l" : "L",
Radek Krejciddace2c2021-01-08 11:30:56 +0100572 ((struct ly_in *)log_location.inputs.objs[log_location.inputs.count - 1])->line);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100573 free(prev);
574 LY_CHECK_ERR_RET(rc == -1, LOGMEM(ctx), LY_EMEM);
575 }
576
577 if (*path) {
578 prev = *path;
579 rc = asprintf(path, "%s.", prev);
580 free(prev);
581 LY_CHECK_ERR_RET(rc == -1, LOGMEM(ctx), LY_EMEM);
582 }
Radek Krejci94aa9942018-09-07 17:12:17 +0200583 }
584
Radek Krejci94aa9942018-09-07 17:12:17 +0200585 return LY_SUCCESS;
586}
587
588void
Radek Krejci2efc45b2020-12-22 16:25:44 +0100589ly_vlog(const struct ly_ctx *ctx, LY_VECODE code, const char *format, ...)
Radek Krejci94aa9942018-09-07 17:12:17 +0200590{
591 va_list ap;
Michal Vasko22df3f02020-08-24 13:29:22 +0200592 char *path = NULL;
Radek Krejci94aa9942018-09-07 17:12:17 +0200593
Radek Krejci2efc45b2020-12-22 16:25:44 +0100594 if (path_flag && ctx) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100595 ly_vlog_build_path(ctx, &path);
Radek Krejci94aa9942018-09-07 17:12:17 +0200596 }
597
598 va_start(ap, format);
599 log_vprintf(ctx, LY_LLERR, LY_EVALID, code, path, format, ap);
600 /* path is spent and should not be freed! */
601 va_end(ap);
602}
603
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200604API void
Radek Krejci0b013302021-03-29 15:22:32 +0200605lyplg_ext_log(const struct lysc_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no, const char *path, const char *format, ...)
Radek Krejci0935f412019-08-20 16:15:18 +0200606{
607 va_list ap;
608 char *plugin_msg;
609 int ret;
610
Radek Krejci52b6d512020-10-12 12:33:17 +0200611 if (ly_ll < level) {
Radek Krejci0935f412019-08-20 16:15:18 +0200612 return;
613 }
Radek Krejci932d3bb2021-02-09 16:29:38 +0100614 ret = asprintf(&plugin_msg, "Extension plugin \"%s\": %s", ext->def->plugin->id, format);
Radek Krejci0935f412019-08-20 16:15:18 +0200615 if (ret == -1) {
Radek Krejci28681fa2019-09-06 13:08:45 +0200616 LOGMEM(ext->module->ctx);
Radek Krejci0935f412019-08-20 16:15:18 +0200617 return;
618 }
619
620 va_start(ap, format);
Radek Krejcia4614e62020-05-15 14:19:28 +0200621 log_vprintf(ext->module->ctx, level, (level == LY_LLERR ? LY_EPLUGIN : 0) | err_no, LYVE_OTHER, path ? strdup(path) : NULL, plugin_msg, ap);
Radek Krejci0935f412019-08-20 16:15:18 +0200622 va_end(ap);
623
624 free(plugin_msg);
625}
626
Michal Vasko177d0ed2020-11-23 16:43:03 +0100627/**
Michal Vaskoc78a6092021-05-07 15:27:35 +0200628 * @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 +0100629 */
630static void
Michal Vaskoc78a6092021-05-07 15:27:35 +0200631_ly_err_print(const struct ly_ctx *ctx, struct ly_err_item *eitem, const char *format, ...)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200632{
Michal Vasko177d0ed2020-11-23 16:43:03 +0100633 va_list ap;
634 char *path_dup = NULL;
635
636 LY_CHECK_ARG_RET(ctx, eitem, );
637
638 if (eitem->path) {
639 /* duplicate path because it will be freed */
640 path_dup = strdup(eitem->path);
641 LY_CHECK_ERR_RET(!path_dup, LOGMEM(ctx), );
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200642 }
Michal Vasko177d0ed2020-11-23 16:43:03 +0100643
Michal Vaskoc78a6092021-05-07 15:27:35 +0200644 va_start(ap, format);
645 log_vprintf(ctx, eitem->level, eitem->no, eitem->vecode, path_dup, format, ap);
Michal Vasko177d0ed2020-11-23 16:43:03 +0100646 va_end(ap);
Michal Vasko177d0ed2020-11-23 16:43:03 +0100647}
648
649API void
650ly_err_print(const struct ly_ctx *ctx, struct ly_err_item *eitem)
651{
Michal Vaskoc78a6092021-05-07 15:27:35 +0200652 /* String ::ly_err_item.msg cannot be used directly because it may contain the % character */
653 _ly_err_print(ctx, eitem, "%s", eitem->msg);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200654}