blob: 74407d818a185f5e8f788cec0ea3d1fda8866bd5 [file] [log] [blame]
Radek Krejci5aeea3a2018-09-05 13:29:36 +02001/**
2 * @file log.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko193dacd2022-10-13 08:43:05 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
Radek Krejci5aeea3a2018-09-05 13:29:36 +02005 * @brief Logger routines implementations
6 *
Michal Vasko193dacd2022-10-13 08:43:05 +02007 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
Radek Krejci5aeea3a2018-09-05 13:29:36 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
Christian Hopps32874e12021-05-01 09:43:54 -040016#define _GNU_SOURCE /* asprintf, strdup */
Radek Krejci535ea9f2020-05-29 16:01:05 +020017
18#include "log.h"
Radek Krejcib7db73a2018-10-24 14:18:40 +020019
Radek Krejci5aeea3a2018-09-05 13:29:36 +020020#include <assert.h>
Radek Krejcic04f0a22018-09-21 15:49:45 +020021#include <inttypes.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <pthread.h>
Radek Krejci5aeea3a2018-09-05 13:29:36 +020023#include <stdarg.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020024#include <stdint.h>
Radek Krejci5aeea3a2018-09-05 13:29:36 +020025#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include <stdlib.h>
27#include <string.h>
Radek Krejci5aeea3a2018-09-05 13:29:36 +020028
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "common.h"
Radek Krejciaa45bda2020-07-20 07:43:38 +020030#include "compat.h"
Radek Krejciaddfc9a2020-12-17 20:46:35 +010031#include "in_internal.h"
Radek Krejci0935f412019-08-20 16:15:18 +020032#include "plugins_exts.h"
Radek Krejci77114102021-03-10 15:21:57 +010033#include "set.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020034#include "tree_data.h"
Michal Vaskodbf3e652022-10-21 08:46:25 +020035#include "tree_data_internal.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "tree_schema.h"
Michal Vasko193dacd2022-10-13 08:43:05 +020037#include "tree_schema_internal.h"
Radek Krejci5aeea3a2018-09-05 13:29:36 +020038
Václav Kubernátd367ad92021-11-29 09:28:56 +010039ATOMIC_T ly_ll = (uint_fast32_t)LY_LLWRN;
40ATOMIC_T ly_log_opts = (uint_fast32_t)(LY_LOLOG | LY_LOSTORE_LAST);
Michal Vaskod4a6d042022-12-08 08:34:29 +010041THREAD_LOCAL uint32_t *temp_ly_log_opts;
Michal Vaskod8085612020-08-21 12:55:23 +020042static ly_log_clb log_clb;
Václav Kubernátd367ad92021-11-29 09:28:56 +010043static ATOMIC_T path_flag = 1;
Michal Vasko236cbac2023-02-10 15:45:37 +010044THREAD_LOCAL char last_msg[LY_LAST_MSG_SIZE];
Radek Krejci5aeea3a2018-09-05 13:29:36 +020045#ifndef NDEBUG
Václav Kubernátd367ad92021-11-29 09:28:56 +010046ATOMIC_T ly_ldbg_groups = 0;
Radek Krejci5aeea3a2018-09-05 13:29:36 +020047#endif
48
Radek Krejciddace2c2021-01-08 11:30:56 +010049THREAD_LOCAL struct ly_log_location_s log_location = {0};
50
Michal Vasko236cbac2023-02-10 15:45:37 +010051LIBYANG_API_DEF const char *
52ly_last_errmsg(void)
53{
54 return last_msg;
55}
56
Jan Kundrátc53a7ec2021-12-09 16:01:19 +010057LIBYANG_API_DEF LY_ERR
Radek Krejcid33273d2018-10-25 14:55:52 +020058ly_errcode(const struct ly_ctx *ctx)
59{
60 struct ly_err_item *i;
61
Radek Krejci572ee602020-09-16 14:35:08 +020062 i = ly_err_last(ctx);
Radek Krejcid33273d2018-10-25 14:55:52 +020063 if (i) {
Radek Krejci572ee602020-09-16 14:35:08 +020064 return i->no;
Radek Krejcid33273d2018-10-25 14:55:52 +020065 }
66
67 return LY_SUCCESS;
68}
69
Jan Kundrátc53a7ec2021-12-09 16:01:19 +010070LIBYANG_API_DEF LY_VECODE
Radek Krejci5aeea3a2018-09-05 13:29:36 +020071ly_vecode(const struct ly_ctx *ctx)
72{
73 struct ly_err_item *i;
74
Radek Krejci572ee602020-09-16 14:35:08 +020075 i = ly_err_last(ctx);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020076 if (i) {
Radek Krejci572ee602020-09-16 14:35:08 +020077 return i->vecode;
Radek Krejci5aeea3a2018-09-05 13:29:36 +020078 }
79
80 return LYVE_SUCCESS;
81}
82
Jan Kundrátc53a7ec2021-12-09 16:01:19 +010083LIBYANG_API_DEF const char *
Radek Krejci5aeea3a2018-09-05 13:29:36 +020084ly_errmsg(const struct ly_ctx *ctx)
85{
86 struct ly_err_item *i;
87
Michal Vaskob3d0d6b2018-09-07 10:17:33 +020088 LY_CHECK_ARG_RET(NULL, ctx, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020089
Radek Krejci572ee602020-09-16 14:35:08 +020090 i = ly_err_last(ctx);
Radek Krejci5aeea3a2018-09-05 13:29:36 +020091 if (i) {
Radek Krejci572ee602020-09-16 14:35:08 +020092 return i->msg;
Radek Krejci5aeea3a2018-09-05 13:29:36 +020093 }
94
95 return NULL;
96}
97
Jan Kundrátc53a7ec2021-12-09 16:01:19 +010098LIBYANG_API_DEF const char *
Radek Krejci5aeea3a2018-09-05 13:29:36 +020099ly_errpath(const struct ly_ctx *ctx)
100{
101 struct ly_err_item *i;
102
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200103 LY_CHECK_ARG_RET(NULL, ctx, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200104
Radek Krejci572ee602020-09-16 14:35:08 +0200105 i = ly_err_last(ctx);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200106 if (i) {
Radek Krejci572ee602020-09-16 14:35:08 +0200107 return i->path;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200108 }
109
110 return NULL;
111}
112
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100113LIBYANG_API_DEF const char *
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200114ly_errapptag(const struct ly_ctx *ctx)
115{
116 struct ly_err_item *i;
117
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200118 LY_CHECK_ARG_RET(NULL, ctx, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200119
Radek Krejci572ee602020-09-16 14:35:08 +0200120 i = ly_err_last(ctx);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200121 if (i) {
Radek Krejci572ee602020-09-16 14:35:08 +0200122 return i->apptag;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200123 }
124
125 return NULL;
126}
127
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100128LIBYANG_API_DEF LY_ERR
aPiecek6d618552021-06-18 10:02:59 +0200129ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *path, char *apptag, const char *err_format, ...)
Radek Krejcie7b95092019-05-15 11:03:07 +0200130{
Radek Krejcidb0ee022021-03-15 16:53:44 +0100131 char *msg = NULL;
132 struct ly_err_item *e;
Radek Krejcie7b95092019-05-15 11:03:07 +0200133
Radek Krejcid43298b2021-03-25 16:17:15 +0100134 if (!err || (ecode == LY_SUCCESS)) {
Radek Krejcidb0ee022021-03-15 16:53:44 +0100135 /* nothing to do */
136 return ecode;
137 }
138
139 e = malloc(sizeof *e);
140 LY_CHECK_ERR_RET(!e, LOGMEM(NULL), LY_EMEM);
141 e->prev = (*err) ? (*err)->prev : e;
142 e->next = NULL;
143 if (*err) {
144 (*err)->prev->next = e;
145 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200146
147 /* fill in the information */
Radek Krejcidb0ee022021-03-15 16:53:44 +0100148 e->level = LY_LLERR;
149 e->no = ecode;
150 e->vecode = vecode;
151 e->path = path;
152 e->apptag = apptag;
Radek Krejcie7b95092019-05-15 11:03:07 +0200153
aPiecek6d618552021-06-18 10:02:59 +0200154 if (err_format) {
Radek Krejcidb0ee022021-03-15 16:53:44 +0100155 va_list print_args;
156
aPiecek6d618552021-06-18 10:02:59 +0200157 va_start(print_args, err_format);
Radek Krejcidb0ee022021-03-15 16:53:44 +0100158
aPiecek6d618552021-06-18 10:02:59 +0200159 if (vasprintf(&msg, err_format, print_args) == -1) {
160 /* we don't have anything more to do, just set msg to NULL to avoid undefined content,
Radek Krejcidb0ee022021-03-15 16:53:44 +0100161 * still keep the information about the original error instead of LY_EMEM or other printf's error */
162 msg = NULL;
163 }
164
165 va_end(print_args);
166 }
167 e->msg = msg;
168
169 if (!(*err)) {
170 *err = e;
171 }
172
173 return e->no;
Radek Krejcie7b95092019-05-15 11:03:07 +0200174}
175
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100176LIBYANG_API_DEF struct ly_err_item *
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200177ly_err_first(const struct ly_ctx *ctx)
178{
Michal Vaskob3d0d6b2018-09-07 10:17:33 +0200179 LY_CHECK_ARG_RET(NULL, ctx, NULL);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200180
181 return pthread_getspecific(ctx->errlist_key);
182}
183
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100184LIBYANG_API_DEF struct ly_err_item *
Radek Krejci572ee602020-09-16 14:35:08 +0200185ly_err_last(const struct ly_ctx *ctx)
186{
187 const struct ly_err_item *e;
188
189 LY_CHECK_ARG_RET(NULL, ctx, NULL);
190
191 e = pthread_getspecific(ctx->errlist_key);
192 return e ? e->prev : NULL;
193}
194
Michal Vasko9dbb91d2023-01-30 13:59:22 +0100195void
196ly_err_move(struct ly_ctx *src_ctx, struct ly_ctx *trg_ctx)
197{
198 const struct ly_err_item *e;
199
200 /* clear any current errors */
201 ly_err_clean(trg_ctx, NULL);
202
203 /* get the errors in src */
204 e = pthread_getspecific(src_ctx->errlist_key);
205 pthread_setspecific(src_ctx->errlist_key, NULL);
206
207 /* set them for trg */
208 pthread_setspecific(trg_ctx->errlist_key, e);
209}
210
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100211LIBYANG_API_DEF void
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200212ly_err_free(void *ptr)
213{
214 struct ly_err_item *i, *next;
215
216 /* clean the error list */
217 for (i = (struct ly_err_item *)ptr; i; i = next) {
218 next = i->next;
Radek Krejcidb0ee022021-03-15 16:53:44 +0100219 free(i->msg);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200220 free(i->path);
221 free(i->apptag);
222 free(i);
223 }
224}
225
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100226LIBYANG_API_DEF void
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200227ly_err_clean(struct ly_ctx *ctx, struct ly_err_item *eitem)
228{
229 struct ly_err_item *i, *first;
230
231 first = ly_err_first(ctx);
232 if (first == eitem) {
233 eitem = NULL;
234 }
235 if (eitem) {
236 /* disconnect the error */
Radek Krejci1e008d22020-08-17 11:37:37 +0200237 for (i = first; i && (i->next != eitem); i = i->next) {}
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200238 assert(i);
239 i->next = NULL;
240 first->prev = i;
241 /* free this err and newer */
242 ly_err_free(eitem);
243 } else {
244 /* free all err */
245 ly_err_free(first);
246 pthread_setspecific(ctx->errlist_key, NULL);
247 }
248}
249
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100250LIBYANG_API_DEF LY_LOG_LEVEL
Radek Krejci52b6d512020-10-12 12:33:17 +0200251ly_log_level(LY_LOG_LEVEL level)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200252{
Václav Kubernátd367ad92021-11-29 09:28:56 +0100253 LY_LOG_LEVEL prev = ATOMIC_LOAD_RELAXED(ly_ll);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200254
Václav Kubernátd367ad92021-11-29 09:28:56 +0100255 ATOMIC_STORE_RELAXED(ly_ll, level);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200256 return prev;
257}
258
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100259LIBYANG_API_DEF uint32_t
Radek Krejci1deb5be2020-08-26 16:43:36 +0200260ly_log_options(uint32_t opts)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200261{
Václav Kubernátd367ad92021-11-29 09:28:56 +0100262 uint32_t prev = ATOMIC_LOAD_RELAXED(ly_log_opts);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200263
Václav Kubernátd367ad92021-11-29 09:28:56 +0100264 ATOMIC_STORE_RELAXED(ly_log_opts, opts);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200265 return prev;
266}
267
Michal Vaskod4a6d042022-12-08 08:34:29 +0100268LIBYANG_API_DEF void
269ly_temp_log_options(uint32_t *opts)
270{
271 temp_ly_log_opts = opts;
272}
273
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100274LIBYANG_API_DEF uint32_t
Radek Krejci68433c92020-10-12 17:03:55 +0200275ly_log_dbg_groups(uint32_t dbg_groups)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200276{
277#ifndef NDEBUG
Václav Kubernátd367ad92021-11-29 09:28:56 +0100278 uint32_t prev = ATOMIC_LOAD_RELAXED(ly_ldbg_groups);
Radek Krejciebdaed02020-11-09 13:05:06 +0100279
Václav Kubernátd367ad92021-11-29 09:28:56 +0100280 ATOMIC_STORE_RELAXED(ly_ldbg_groups, dbg_groups);
Radek Krejciebdaed02020-11-09 13:05:06 +0100281 return prev;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200282#else
283 (void)dbg_groups;
Radek Krejciebdaed02020-11-09 13:05:06 +0100284 return 0;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200285#endif
286}
287
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100288LIBYANG_API_DEF void
Radek Krejci857189e2020-09-01 13:26:36 +0200289ly_set_log_clb(ly_log_clb clb, ly_bool path)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200290{
Michal Vaskod8085612020-08-21 12:55:23 +0200291 log_clb = clb;
Václav Kubernátd367ad92021-11-29 09:28:56 +0100292 ATOMIC_STORE_RELAXED(path_flag, path);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200293}
294
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100295LIBYANG_API_DEF ly_log_clb
Michal Vaskod8085612020-08-21 12:55:23 +0200296ly_get_log_clb(void)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200297{
Michal Vaskod8085612020-08-21 12:55:23 +0200298 return log_clb;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200299}
300
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100301void
Michal Vasko59e69e72022-02-18 09:18:21 +0100302ly_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 +0100303 uint64_t line)
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100304{
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100305 if (scnode) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100306 ly_set_add(&log_location.scnodes, (void *)scnode, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100307 }
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100308 if (dnode) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100309 ly_set_add(&log_location.dnodes, (void *)dnode, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100310 }
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100311 if (path) {
312 char *s = strdup(path);
Michal Vasko26bbb272022-08-02 14:54:33 +0200313
Radek Krejciddace2c2021-01-08 11:30:56 +0100314 LY_CHECK_ERR_RET(!s, LOGMEM(NULL), );
315 ly_set_add(&log_location.paths, s, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100316 }
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100317 if (in) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100318 ly_set_add(&log_location.inputs, (void *)in, 1, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100319 }
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100320 if (line) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100321 log_location.line = line;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100322 }
323}
324
325void
Michal Vasko59e69e72022-02-18 09:18:21 +0100326ly_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 +0100327{
Radek Krejciddace2c2021-01-08 11:30:56 +0100328 for (uint32_t i = scnode_steps; i && log_location.scnodes.count; i--) {
329 log_location.scnodes.count--;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100330 }
331
Radek Krejciddace2c2021-01-08 11:30:56 +0100332 for (uint32_t i = dnode_steps; i && log_location.dnodes.count; i--) {
333 log_location.dnodes.count--;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100334 }
335
Radek Krejciddace2c2021-01-08 11:30:56 +0100336 for (uint32_t i = path_steps; i && log_location.paths.count; i--) {
337 ly_set_rm_index(&log_location.paths, log_location.paths.count - 1, free);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100338 }
339
Radek Krejciddace2c2021-01-08 11:30:56 +0100340 for (uint32_t i = in_steps; i && log_location.inputs.count; i--) {
341 log_location.inputs.count--;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100342 }
343
Radek Krejciddace2c2021-01-08 11:30:56 +0100344 /* deallocate the empty sets */
345 if (scnode_steps && !log_location.scnodes.count) {
346 ly_set_erase(&log_location.scnodes, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100347 }
Radek Krejciddace2c2021-01-08 11:30:56 +0100348 if (dnode_steps && !log_location.dnodes.count) {
349 ly_set_erase(&log_location.dnodes, NULL);
350 }
351 if (path_steps && !log_location.paths.count) {
352 ly_set_erase(&log_location.paths, free);
353 }
354 if (in_steps && !log_location.inputs.count) {
355 ly_set_erase(&log_location.inputs, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100356 }
357}
358
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200359static LY_ERR
360log_store(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, LY_VECODE vecode, char *msg, char *path, char *apptag)
361{
362 struct ly_err_item *eitem, *last;
363
364 assert(ctx && (level < LY_LLVRB));
365
366 eitem = pthread_getspecific(ctx->errlist_key);
367 if (!eitem) {
368 /* if we are only to fill in path, there must have been an error stored */
369 assert(msg);
370 eitem = malloc(sizeof *eitem);
371 LY_CHECK_GOTO(!eitem, mem_fail);
372 eitem->prev = eitem;
373 eitem->next = NULL;
374
375 pthread_setspecific(ctx->errlist_key, eitem);
376 } else if (!msg) {
377 /* only filling the path */
378 assert(path);
379
380 /* find last error */
381 eitem = eitem->prev;
382 do {
383 if (eitem->level == LY_LLERR) {
384 /* fill the path */
385 free(eitem->path);
386 eitem->path = path;
387 return LY_SUCCESS;
388 }
389 eitem = eitem->prev;
390 } while (eitem->prev->next);
391 /* last error was not found */
392 assert(0);
Michal Vaskod4a6d042022-12-08 08:34:29 +0100393 } else if ((temp_ly_log_opts && ((*temp_ly_log_opts & LY_LOSTORE_LAST) == LY_LOSTORE_LAST)) ||
394 (!temp_ly_log_opts && ((ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOSTORE_LAST) == LY_LOSTORE_LAST))) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200395 /* overwrite last message */
396 free(eitem->msg);
397 free(eitem->path);
398 free(eitem->apptag);
399 } else {
400 /* store new message */
401 last = eitem->prev;
402 eitem->prev = malloc(sizeof *eitem);
403 LY_CHECK_GOTO(!eitem->prev, mem_fail);
404 eitem = eitem->prev;
405 eitem->prev = last;
406 eitem->next = NULL;
407 last->next = eitem;
408 }
409
410 /* fill in the information */
411 eitem->level = level;
412 eitem->no = no;
413 eitem->vecode = vecode;
414 eitem->msg = msg;
415 eitem->path = path;
416 eitem->apptag = apptag;
417 return LY_SUCCESS;
418
419mem_fail:
420 LOGMEM(NULL);
421 free(msg);
422 free(path);
423 free(apptag);
424 return LY_EMEM;
425}
426
427static void
Michal Vaskoe9391c72021-10-05 10:04:56 +0200428log_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 +0200429 const char *format, va_list args)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200430{
431 char *msg = NULL;
Michal Vaskod4a6d042022-12-08 08:34:29 +0100432 ly_bool free_strs, lolog, lostore;
433
434 /* learn effective logger options */
435 if (temp_ly_log_opts) {
436 lolog = *temp_ly_log_opts & LY_LOLOG;
437 lostore = *temp_ly_log_opts & LY_LOSTORE;
438 } else {
439 lolog = ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOLOG;
440 lostore = ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOSTORE;
441 }
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200442
Václav Kubernátd367ad92021-11-29 09:28:56 +0100443 if (level > ATOMIC_LOAD_RELAXED(ly_ll)) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200444 /* do not print or store the message */
445 free(path);
446 return;
447 }
448
Michal Vasko5a016922021-05-07 08:19:15 +0200449 if (no == LY_EMEM) {
450 /* just print it, anything else would most likely fail anyway */
Michal Vaskod4a6d042022-12-08 08:34:29 +0100451 if (lolog) {
Michal Vasko5a016922021-05-07 08:19:15 +0200452 if (log_clb) {
453 log_clb(level, LY_EMEM_MSG, path);
454 } else {
455 fprintf(stderr, "libyang[%d]: ", level);
456 vfprintf(stderr, format, args);
457 if (path) {
458 fprintf(stderr, " (path: %s)\n", path);
459 } else {
460 fprintf(stderr, "\n");
461 }
462 }
463 }
464 free(path);
465 return;
466 }
467
Michal Vasko236cbac2023-02-10 15:45:37 +0100468 /* print into a single message */
469 if (vasprintf(&msg, format, args) == -1) {
470 LOGMEM(ctx);
471 free(path);
472 return;
473 }
474
475 /* store as the last message */
476 strncpy(last_msg, msg, LY_LAST_MSG_SIZE - 1);
477
478 /* store the error/warning in the context (if we need to store errors internally, it does not matter what are
479 * the user log options) */
Michal Vaskod4a6d042022-12-08 08:34:29 +0100480 if ((level < LY_LLVRB) && ctx && lostore) {
Radek Krejcic9e64a62020-09-18 20:08:12 +0200481 if (((no & ~LY_EPLUGIN) == LY_EVALID) && (vecode == LYVE_SUCCESS)) {
482 /* assume we are inheriting the error, so inherit vecode as well */
483 vecode = ly_vecode(ctx);
484 }
Michal Vaskoe9391c72021-10-05 10:04:56 +0200485 if (log_store(ctx, level, no, vecode, msg, path, apptag ? strdup(apptag) : NULL)) {
Michal Vasko004d3152020-06-11 19:59:22 +0200486 return;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200487 }
488 free_strs = 0;
489 } else {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200490 free_strs = 1;
491 }
492
493 /* if we are only storing errors internally, never print the message (yet) */
Michal Vaskod4a6d042022-12-08 08:34:29 +0100494 if (lolog) {
Michal Vaskod8085612020-08-21 12:55:23 +0200495 if (log_clb) {
496 log_clb(level, msg, path);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200497 } else {
498 fprintf(stderr, "libyang[%d]: %s%s", level, msg, path ? " " : "\n");
499 if (path) {
500 fprintf(stderr, "(path: %s)\n", path);
501 }
502 }
503 }
504
505 if (free_strs) {
506 free(path);
507 free(msg);
508 }
509}
510
Radek Krejci4ab61562018-09-05 15:00:37 +0200511#ifndef NDEBUG
512
513void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200514ly_log_dbg(uint32_t group, const char *format, ...)
Radek Krejci4ab61562018-09-05 15:00:37 +0200515{
516 char *dbg_format;
517 const char *str_group;
518 va_list ap;
519
Václav Kubernátd367ad92021-11-29 09:28:56 +0100520 if (!(ATOMIC_LOAD_RELAXED(ly_ldbg_groups) & group)) {
Radek Krejci4ab61562018-09-05 15:00:37 +0200521 return;
522 }
523
524 switch (group) {
525 case LY_LDGDICT:
526 str_group = "DICT";
527 break;
Radek Krejci4ab61562018-09-05 15:00:37 +0200528 case LY_LDGXPATH:
529 str_group = "XPATH";
530 break;
Michal Vaskoe558f792021-07-28 08:20:15 +0200531 case LY_LDGDEPSETS:
532 str_group = "DEPSETS";
533 break;
Radek Krejci4ab61562018-09-05 15:00:37 +0200534 default:
535 LOGINT(NULL);
536 return;
537 }
538
539 if (asprintf(&dbg_format, "%s: %s", str_group, format) == -1) {
540 LOGMEM(NULL);
541 return;
542 }
543
544 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200545 log_vprintf(NULL, LY_LLDBG, 0, 0, NULL, NULL, dbg_format, ap);
Radek Krejci4ab61562018-09-05 15:00:37 +0200546 va_end(ap);
547}
548
549#endif
550
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200551void
552ly_log(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, const char *format, ...)
553{
554 va_list ap;
555
556 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200557 log_vprintf(ctx, level, no, 0, NULL, NULL, format, ap);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200558 va_end(ap);
559}
560
Michal Vaskodbf3e652022-10-21 08:46:25 +0200561/**
562 * @brief Append a schema node name to a generated data path, only if it fits.
563 *
564 * @param[in,out] str Generated path to update.
565 * @param[in] snode Schema node to append.
566 * @param[in] parent Last printed data node.
567 * @return LY_ERR value.
568 */
569static LY_ERR
570ly_vlog_build_path_append(char **str, const struct lysc_node *snode, const struct lyd_node *parent)
571{
572 const struct lys_module *mod, *prev_mod;
573 uint32_t len, new_len;
574 void *mem;
575
576 if (snode->nodetype & (LYS_CHOICE | LYS_CASE)) {
577 /* schema-only node */
578 return LY_SUCCESS;
579 } else if (lysc_data_parent(snode) != parent->schema) {
580 /* not a direct descendant node */
581 return LY_SUCCESS;
582 }
583
584 /* get module to print, if any */
585 mod = snode->module;
586 prev_mod = (parent->schema) ? parent->schema->module : lyd_owner_module(parent);
587 if (prev_mod == mod) {
588 mod = NULL;
589 }
590
591 /* realloc string */
592 len = strlen(*str);
593 new_len = len + 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(snode->name);
594 mem = realloc(*str, new_len + 1);
595 LY_CHECK_ERR_RET(!mem, LOGMEM(LYD_CTX(parent)), LY_EMEM);
596 *str = mem;
597
598 /* print the last schema node */
599 sprintf(*str + len, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", snode->name);
600 return LY_SUCCESS;
601}
602
603/**
604 * @brief Build log path from the stored log location information.
605 *
606 * @param[in] ctx Context to use.
607 * @param[out] path Generated log path.
608 * @return LY_ERR value.
609 */
Radek Krejci94aa9942018-09-07 17:12:17 +0200610static LY_ERR
Radek Krejciddace2c2021-01-08 11:30:56 +0100611ly_vlog_build_path(const struct ly_ctx *ctx, char **path)
Radek Krejci94aa9942018-09-07 17:12:17 +0200612{
Michal Vaskodbf3e652022-10-21 08:46:25 +0200613 int r;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100614 char *str = NULL, *prev = NULL;
Michal Vaskodbf3e652022-10-21 08:46:25 +0200615 const struct lyd_node *dnode;
Radek Krejcicb3e6472021-01-06 08:19:01 +0100616
Radek Krejci2efc45b2020-12-22 16:25:44 +0100617 *path = NULL;
Radek Krejci94aa9942018-09-07 17:12:17 +0200618
Radek Krejciddace2c2021-01-08 11:30:56 +0100619 if (log_location.paths.count && ((const char *)(log_location.paths.objs[log_location.paths.count - 1]))[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100620 /* simply get what is in the provided path string */
Michal Vasko6727c682023-02-17 10:40:26 +0100621 r = asprintf(path, "Path \"%s\".", (const char *)log_location.paths.objs[log_location.paths.count - 1]);
622 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100623 } else {
Michal Vaskodbf3e652022-10-21 08:46:25 +0200624 /* data/schema node */
625 if (log_location.dnodes.count) {
626 dnode = log_location.dnodes.objs[log_location.dnodes.count - 1];
Michal Vaskoa4dfb3c2022-10-25 14:59:31 +0200627 if (dnode->parent || !lysc_data_parent(dnode->schema)) {
628 /* data node with all of its parents */
629 str = lyd_path(log_location.dnodes.objs[log_location.dnodes.count - 1], LYD_PATH_STD, NULL, 0);
630 LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM);
631 } else {
Michal Vaskodbf3e652022-10-21 08:46:25 +0200632 /* data parsers put all the parent nodes in the set, but they are not connected */
633 str = lyd_path_set(&log_location.dnodes, LYD_PATH_STD);
Michal Vasko3e65ee32022-10-21 10:09:51 +0200634 LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM);
Michal Vaskoa4dfb3c2022-10-25 14:59:31 +0200635 }
Michal Vaskodbf3e652022-10-21 08:46:25 +0200636
Michal Vaskoa4dfb3c2022-10-25 14:59:31 +0200637 /* sometimes the last node is not created yet and we only have the schema node */
638 if (log_location.scnodes.count) {
639 ly_vlog_build_path_append(&str, log_location.scnodes.objs[log_location.scnodes.count - 1], dnode);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200640 }
Michal Vaskodbf3e652022-10-21 08:46:25 +0200641
642 r = asprintf(path, "Data location \"%s\"", str);
643 free(str);
644 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
645 } else if (log_location.scnodes.count) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100646 str = lysc_path(log_location.scnodes.objs[log_location.scnodes.count - 1], LYSC_PATH_LOG, NULL, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100647 LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM);
648
Michal Vaskodbf3e652022-10-21 08:46:25 +0200649 r = asprintf(path, "Schema location \"%s\"", str);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100650 free(str);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200651 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100652 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100653
Michal Vaskodbf3e652022-10-21 08:46:25 +0200654 /* line */
655 prev = *path;
Radek Krejciddace2c2021-01-08 11:30:56 +0100656 if (log_location.line) {
Michal Vaskodbf3e652022-10-21 08:46:25 +0200657 r = asprintf(path, "%s%sine number %" PRIu64, prev ? prev : "", prev ? ", l" : "L", log_location.line);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100658 free(prev);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200659 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100660
Radek Krejciddace2c2021-01-08 11:30:56 +0100661 log_location.line = 0;
662 } else if (log_location.inputs.count) {
Michal Vaskodbf3e652022-10-21 08:46:25 +0200663 r = asprintf(path, "%s%sine number %" PRIu64, prev ? prev : "", prev ? ", l" : "L",
Radek Krejciddace2c2021-01-08 11:30:56 +0100664 ((struct ly_in *)log_location.inputs.objs[log_location.inputs.count - 1])->line);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100665 free(prev);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200666 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100667 }
668
669 if (*path) {
670 prev = *path;
Michal Vaskodbf3e652022-10-21 08:46:25 +0200671 r = asprintf(path, "%s.", prev);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100672 free(prev);
Michal Vaskodbf3e652022-10-21 08:46:25 +0200673 LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100674 }
Radek Krejci94aa9942018-09-07 17:12:17 +0200675 }
676
Radek Krejci94aa9942018-09-07 17:12:17 +0200677 return LY_SUCCESS;
678}
679
680void
Michal Vaskoe9391c72021-10-05 10:04:56 +0200681ly_vlog(const struct ly_ctx *ctx, const char *apptag, LY_VECODE code, const char *format, ...)
Radek Krejci94aa9942018-09-07 17:12:17 +0200682{
683 va_list ap;
Michal Vasko22df3f02020-08-24 13:29:22 +0200684 char *path = NULL;
Radek Krejci94aa9942018-09-07 17:12:17 +0200685
Václav Kubernátd367ad92021-11-29 09:28:56 +0100686 if (ATOMIC_LOAD_RELAXED(path_flag) && ctx) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100687 ly_vlog_build_path(ctx, &path);
Radek Krejci94aa9942018-09-07 17:12:17 +0200688 }
689
690 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200691 log_vprintf(ctx, LY_LLERR, LY_EVALID, code, path, apptag, format, ap);
Radek Krejci94aa9942018-09-07 17:12:17 +0200692 /* path is spent and should not be freed! */
693 va_end(ap);
694}
695
Michal Vasko193dacd2022-10-13 08:43:05 +0200696/**
697 * @brief Print a log message from an extension plugin callback.
698 *
699 * @param[in] ctx libyang context to store the error record. If not provided, the error is just printed.
700 * @param[in] plugin_name Name of the plugin generating the message.
701 * @param[in] level Log message level (error, warning, etc.)
702 * @param[in] err_no Error type code.
Michal Vasko6727c682023-02-17 10:40:26 +0100703 * @param[in] path Optional path of the error, used if set.
Michal Vasko193dacd2022-10-13 08:43:05 +0200704 * @param[in] format Format string to print.
705 * @param[in] ap Var arg list for @p format.
706 */
707static void
Michal Vasko6727c682023-02-17 10:40:26 +0100708ly_ext_log(const struct ly_ctx *ctx, const char *plugin_name, LY_LOG_LEVEL level, LY_ERR err_no, char *path,
Michal Vasko193dacd2022-10-13 08:43:05 +0200709 const char *format, va_list ap)
Radek Krejci0935f412019-08-20 16:15:18 +0200710{
Radek Krejci0935f412019-08-20 16:15:18 +0200711 char *plugin_msg;
Radek Krejci0935f412019-08-20 16:15:18 +0200712
Václav Kubernátd367ad92021-11-29 09:28:56 +0100713 if (ATOMIC_LOAD_RELAXED(ly_ll) < level) {
Radek Krejci0935f412019-08-20 16:15:18 +0200714 return;
715 }
Michal Vasko193dacd2022-10-13 08:43:05 +0200716 if (asprintf(&plugin_msg, "Ext plugin \"%s\": %s", plugin_name, format) == -1) {
717 LOGMEM(ctx);
Radek Krejci0935f412019-08-20 16:15:18 +0200718 return;
719 }
720
Michal Vasko6727c682023-02-17 10:40:26 +0100721 log_vprintf(ctx, level, (level == LY_LLERR ? LY_EPLUGIN : 0) | err_no, LYVE_OTHER, path, NULL, plugin_msg, ap);
Michal Vasko193dacd2022-10-13 08:43:05 +0200722 free(plugin_msg);
723}
724
725LIBYANG_API_DEF void
726lyplg_ext_parse_log(const struct lysp_ctx *pctx, const struct lysp_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no,
727 const char *format, ...)
728{
729 va_list ap;
730 char *path = NULL;
731
732 if (ATOMIC_LOAD_RELAXED(path_flag)) {
733 ly_vlog_build_path(PARSER_CTX(pctx), &path);
734 }
735
Radek Krejci0935f412019-08-20 16:15:18 +0200736 va_start(ap, format);
Michal Vasko193dacd2022-10-13 08:43:05 +0200737 ly_ext_log(PARSER_CTX(pctx), ext->record->plugin.id, level, err_no, path, format, ap);
Radek Krejci0935f412019-08-20 16:15:18 +0200738 va_end(ap);
Michal Vasko193dacd2022-10-13 08:43:05 +0200739}
740
741LIBYANG_API_DEF void
742lyplg_ext_compile_log(const struct lysc_ctx *cctx, const struct lysc_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no,
743 const char *format, ...)
744{
745 va_list ap;
Michal Vasko6727c682023-02-17 10:40:26 +0100746 char *path = NULL;
747
748 if (cctx && (asprintf(&path, "Path \"%s\".", cctx->path) == -1)) {
749 LOGMEM(cctx->ctx);
750 return;
751 }
Michal Vasko193dacd2022-10-13 08:43:05 +0200752
753 va_start(ap, format);
Michal Vasko6727c682023-02-17 10:40:26 +0100754 ly_ext_log(ext->module->ctx, ext->def->plugin->id, level, err_no, path, format, ap);
Michal Vasko193dacd2022-10-13 08:43:05 +0200755 va_end(ap);
756}
757
758LIBYANG_API_DEF void
759lyplg_ext_compile_log_path(const char *path, const struct lysc_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no,
760 const char *format, ...)
761{
762 va_list ap;
Michal Vasko6727c682023-02-17 10:40:26 +0100763 char *log_path = NULL;
764
765 if (path && (asprintf(&log_path, "Path \"%s\".", path) == -1)) {
766 LOGMEM(ext->module->ctx);
767 return;
768 }
Michal Vasko193dacd2022-10-13 08:43:05 +0200769
770 va_start(ap, format);
Michal Vasko6727c682023-02-17 10:40:26 +0100771 ly_ext_log(ext->module->ctx, ext->def->plugin->id, level, err_no, log_path, format, ap);
Michal Vasko193dacd2022-10-13 08:43:05 +0200772 va_end(ap);
Radek Krejci0935f412019-08-20 16:15:18 +0200773}
774
Michal Vasko177d0ed2020-11-23 16:43:03 +0100775/**
Michal Vasko6727c682023-02-17 10:40:26 +0100776 * @brief Serves only for creating ap.
777 */
778static void
779_lyplg_ext_compile_log_err(const struct ly_err_item *err, const struct lysc_ext_instance *ext, ...)
780{
781 va_list ap;
782
783 va_start(ap, ext);
784 ly_ext_log(ext->module->ctx, ext->def->plugin->id, err->level, err->no, err->path ? strdup(err->path) : NULL, "%s", ap);
785 va_end(ap);
786}
787
788LIBYANG_API_DEF void
789lyplg_ext_compile_log_err(const struct ly_err_item *err, const struct lysc_ext_instance *ext)
790{
791 _lyplg_ext_compile_log_err(err, ext, err->msg);
792}
793
794/**
Michal Vaskoc78a6092021-05-07 15:27:35 +0200795 * @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 +0100796 */
797static void
Michal Vaskoc78a6092021-05-07 15:27:35 +0200798_ly_err_print(const struct ly_ctx *ctx, struct ly_err_item *eitem, const char *format, ...)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200799{
Michal Vasko177d0ed2020-11-23 16:43:03 +0100800 va_list ap;
801 char *path_dup = NULL;
802
803 LY_CHECK_ARG_RET(ctx, eitem, );
804
805 if (eitem->path) {
806 /* duplicate path because it will be freed */
807 path_dup = strdup(eitem->path);
808 LY_CHECK_ERR_RET(!path_dup, LOGMEM(ctx), );
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200809 }
Michal Vasko177d0ed2020-11-23 16:43:03 +0100810
Michal Vaskoc78a6092021-05-07 15:27:35 +0200811 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200812 log_vprintf(ctx, eitem->level, eitem->no, eitem->vecode, path_dup, eitem->apptag, format, ap);
Michal Vasko177d0ed2020-11-23 16:43:03 +0100813 va_end(ap);
Michal Vasko177d0ed2020-11-23 16:43:03 +0100814}
815
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100816LIBYANG_API_DEF void
Michal Vasko177d0ed2020-11-23 16:43:03 +0100817ly_err_print(const struct ly_ctx *ctx, struct ly_err_item *eitem)
818{
Michal Vaskoc78a6092021-05-07 15:27:35 +0200819 /* String ::ly_err_item.msg cannot be used directly because it may contain the % character */
820 _ly_err_print(ctx, eitem, "%s", eitem->msg);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200821}