blob: d94301781779b2cc2ee0d888d1456bd6991c7c8d [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
Václav Kubernátd367ad92021-11-29 09:28:56 +010036ATOMIC_T ly_ll = (uint_fast32_t)LY_LLWRN;
37ATOMIC_T ly_log_opts = (uint_fast32_t)(LY_LOLOG | LY_LOSTORE_LAST);
Michal Vaskod8085612020-08-21 12:55:23 +020038static ly_log_clb log_clb;
Václav Kubernátd367ad92021-11-29 09:28:56 +010039static ATOMIC_T path_flag = 1;
Radek Krejci5aeea3a2018-09-05 13:29:36 +020040#ifndef NDEBUG
Václav Kubernátd367ad92021-11-29 09:28:56 +010041ATOMIC_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
Jan Kundrátc53a7ec2021-12-09 16:01:19 +010049LIBYANG_API_DEF LY_ERR
Radek Krejcid33273d2018-10-25 14:55:52 +020050ly_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
Jan Kundrátc53a7ec2021-12-09 16:01:19 +010062LIBYANG_API_DEF LY_VECODE
Radek Krejci5aeea3a2018-09-05 13:29:36 +020063ly_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
Jan Kundrátc53a7ec2021-12-09 16:01:19 +010075LIBYANG_API_DEF const char *
Radek Krejci5aeea3a2018-09-05 13:29:36 +020076ly_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
Jan Kundrátc53a7ec2021-12-09 16:01:19 +010090LIBYANG_API_DEF const char *
Radek Krejci5aeea3a2018-09-05 13:29:36 +020091ly_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
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100105LIBYANG_API_DEF const char *
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200106ly_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
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100120LIBYANG_API_DEF LY_ERR
aPiecek6d618552021-06-18 10:02:59 +0200121ly_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 +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
aPiecek6d618552021-06-18 10:02:59 +0200146 if (err_format) {
Radek Krejcidb0ee022021-03-15 16:53:44 +0100147 va_list print_args;
148
aPiecek6d618552021-06-18 10:02:59 +0200149 va_start(print_args, err_format);
Radek Krejcidb0ee022021-03-15 16:53:44 +0100150
aPiecek6d618552021-06-18 10:02:59 +0200151 if (vasprintf(&msg, err_format, print_args) == -1) {
152 /* we don't have anything more to do, just set msg to NULL to avoid undefined content,
Radek Krejcidb0ee022021-03-15 16:53:44 +0100153 * 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
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100168LIBYANG_API_DEF 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
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100176LIBYANG_API_DEF struct ly_err_item *
Radek Krejci572ee602020-09-16 14:35:08 +0200177ly_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
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100187LIBYANG_API_DEF 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
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100202LIBYANG_API_DEF void
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200203ly_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
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100226LIBYANG_API_DEF 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{
Václav Kubernátd367ad92021-11-29 09:28:56 +0100229 LY_LOG_LEVEL prev = ATOMIC_LOAD_RELAXED(ly_ll);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200230
Václav Kubernátd367ad92021-11-29 09:28:56 +0100231 ATOMIC_STORE_RELAXED(ly_ll, level);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200232 return prev;
233}
234
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100235LIBYANG_API_DEF uint32_t
Radek Krejci1deb5be2020-08-26 16:43:36 +0200236ly_log_options(uint32_t opts)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200237{
Václav Kubernátd367ad92021-11-29 09:28:56 +0100238 uint32_t prev = ATOMIC_LOAD_RELAXED(ly_log_opts);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200239
Václav Kubernátd367ad92021-11-29 09:28:56 +0100240 ATOMIC_STORE_RELAXED(ly_log_opts, opts);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200241 return prev;
242}
243
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100244LIBYANG_API_DEF 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
Václav Kubernátd367ad92021-11-29 09:28:56 +0100248 uint32_t prev = ATOMIC_LOAD_RELAXED(ly_ldbg_groups);
Radek Krejciebdaed02020-11-09 13:05:06 +0100249
Václav Kubernátd367ad92021-11-29 09:28:56 +0100250 ATOMIC_STORE_RELAXED(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
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100258LIBYANG_API_DEF 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;
Václav Kubernátd367ad92021-11-29 09:28:56 +0100262 ATOMIC_STORE_RELAXED(path_flag, path);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200263}
264
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100265LIBYANG_API_DEF ly_log_clb
Michal Vaskod8085612020-08-21 12:55:23 +0200266ly_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);
Václav Kubernátd367ad92021-11-29 09:28:56 +0100375 } else if ((ATOMIC_LOAD_RELAXED(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
Michal Vaskoe9391c72021-10-05 10:04:56 +0200409log_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 +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
Václav Kubernátd367ad92021-11-29 09:28:56 +0100415 if (level > ATOMIC_LOAD_RELAXED(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 */
Václav Kubernátd367ad92021-11-29 09:28:56 +0100423 if (ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOLOG) {
Michal Vasko5a016922021-05-07 08:19:15 +0200424 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) */
Václav Kubernátd367ad92021-11-29 09:28:56 +0100441 if ((level < LY_LLVRB) && ctx && (ATOMIC_LOAD_RELAXED(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 Vaskoe9391c72021-10-05 10:04:56 +0200452 if (log_store(ctx, level, no, vecode, msg, path, apptag ? strdup(apptag) : NULL)) {
Michal Vasko004d3152020-06-11 19:59:22 +0200453 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) */
Václav Kubernátd367ad92021-11-29 09:28:56 +0100466 if (ATOMIC_LOAD_RELAXED(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
Václav Kubernátd367ad92021-11-29 09:28:56 +0100492 if (!(ATOMIC_LOAD_RELAXED(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;
Michal Vaskoe558f792021-07-28 08:20:15 +0200503 case LY_LDGDEPSETS:
504 str_group = "DEPSETS";
505 break;
Radek Krejci4ab61562018-09-05 15:00:37 +0200506 default:
507 LOGINT(NULL);
508 return;
509 }
510
511 if (asprintf(&dbg_format, "%s: %s", str_group, format) == -1) {
512 LOGMEM(NULL);
513 return;
514 }
515
516 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200517 log_vprintf(NULL, LY_LLDBG, 0, 0, NULL, NULL, dbg_format, ap);
Radek Krejci4ab61562018-09-05 15:00:37 +0200518 va_end(ap);
519}
520
521#endif
522
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200523void
524ly_log(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, const char *format, ...)
525{
526 va_list ap;
527
528 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200529 log_vprintf(ctx, level, no, 0, NULL, NULL, format, ap);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200530 va_end(ap);
531}
532
Radek Krejci94aa9942018-09-07 17:12:17 +0200533static LY_ERR
Radek Krejciddace2c2021-01-08 11:30:56 +0100534ly_vlog_build_path(const struct ly_ctx *ctx, char **path)
Radek Krejci94aa9942018-09-07 17:12:17 +0200535{
Radek Krejcic04f0a22018-09-21 15:49:45 +0200536 int rc;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100537 char *str = NULL, *prev = NULL;
Radek Krejcicb3e6472021-01-06 08:19:01 +0100538
Radek Krejci2efc45b2020-12-22 16:25:44 +0100539 *path = NULL;
Radek Krejci94aa9942018-09-07 17:12:17 +0200540
Radek Krejciddace2c2021-01-08 11:30:56 +0100541 if (log_location.paths.count && ((const char *)(log_location.paths.objs[log_location.paths.count - 1]))[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100542 /* simply get what is in the provided path string */
Radek Krejciddace2c2021-01-08 11:30:56 +0100543 *path = strdup((const char *)log_location.paths.objs[log_location.paths.count - 1]);
Radek Krejcic04f0a22018-09-21 15:49:45 +0200544 LY_CHECK_ERR_RET(!(*path), LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100545 } else {
546 /* generate location string */
Radek Krejciddace2c2021-01-08 11:30:56 +0100547 if (log_location.scnodes.count) {
548 str = lysc_path(log_location.scnodes.objs[log_location.scnodes.count - 1], LYSC_PATH_LOG, NULL, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100549 LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM);
550
551 rc = asprintf(path, "Schema location %s", str);
552 free(str);
553 LY_CHECK_ERR_RET(rc == -1, LOGMEM(ctx), LY_EMEM);
554 }
Radek Krejciddace2c2021-01-08 11:30:56 +0100555 if (log_location.dnodes.count) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100556 prev = *path;
Radek Krejciddace2c2021-01-08 11:30:56 +0100557 str = lyd_path(log_location.dnodes.objs[log_location.dnodes.count - 1], LYD_PATH_STD, NULL, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100558 LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM);
559
560 rc = asprintf(path, "%s%sata location %s", prev ? prev : "", prev ? ", d" : "D", str);
561 free(str);
562 free(prev);
563 LY_CHECK_ERR_RET(rc == -1, LOGMEM(ctx), LY_EMEM);
564 }
Radek Krejciddace2c2021-01-08 11:30:56 +0100565 if (log_location.line) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100566 prev = *path;
Radek Krejciddace2c2021-01-08 11:30:56 +0100567 rc = asprintf(path, "%s%sine number %" PRIu64, prev ? prev : "", prev ? ", l" : "L", log_location.line);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100568 free(prev);
569 LY_CHECK_ERR_RET(rc == -1, LOGMEM(ctx), LY_EMEM);
570
Radek Krejciddace2c2021-01-08 11:30:56 +0100571 log_location.line = 0;
572 } else if (log_location.inputs.count) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100573 prev = *path;
574 rc = asprintf(path, "%s%sine number %" PRIu64, prev ? prev : "", prev ? ", l" : "L",
Radek Krejciddace2c2021-01-08 11:30:56 +0100575 ((struct ly_in *)log_location.inputs.objs[log_location.inputs.count - 1])->line);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100576 free(prev);
577 LY_CHECK_ERR_RET(rc == -1, LOGMEM(ctx), LY_EMEM);
578 }
579
580 if (*path) {
581 prev = *path;
582 rc = asprintf(path, "%s.", prev);
583 free(prev);
584 LY_CHECK_ERR_RET(rc == -1, LOGMEM(ctx), LY_EMEM);
585 }
Radek Krejci94aa9942018-09-07 17:12:17 +0200586 }
587
Radek Krejci94aa9942018-09-07 17:12:17 +0200588 return LY_SUCCESS;
589}
590
591void
Michal Vaskoe9391c72021-10-05 10:04:56 +0200592ly_vlog(const struct ly_ctx *ctx, const char *apptag, LY_VECODE code, const char *format, ...)
Radek Krejci94aa9942018-09-07 17:12:17 +0200593{
594 va_list ap;
Michal Vasko22df3f02020-08-24 13:29:22 +0200595 char *path = NULL;
Radek Krejci94aa9942018-09-07 17:12:17 +0200596
Václav Kubernátd367ad92021-11-29 09:28:56 +0100597 if (ATOMIC_LOAD_RELAXED(path_flag) && ctx) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100598 ly_vlog_build_path(ctx, &path);
Radek Krejci94aa9942018-09-07 17:12:17 +0200599 }
600
601 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200602 log_vprintf(ctx, LY_LLERR, LY_EVALID, code, path, apptag, format, ap);
Radek Krejci94aa9942018-09-07 17:12:17 +0200603 /* path is spent and should not be freed! */
604 va_end(ap);
605}
606
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100607LIBYANG_API_DEF void
Radek Krejci0b013302021-03-29 15:22:32 +0200608lyplg_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 +0200609{
610 va_list ap;
611 char *plugin_msg;
612 int ret;
613
Václav Kubernátd367ad92021-11-29 09:28:56 +0100614 if (ATOMIC_LOAD_RELAXED(ly_ll) < level) {
Radek Krejci0935f412019-08-20 16:15:18 +0200615 return;
616 }
Radek Krejci932d3bb2021-02-09 16:29:38 +0100617 ret = asprintf(&plugin_msg, "Extension plugin \"%s\": %s", ext->def->plugin->id, format);
Radek Krejci0935f412019-08-20 16:15:18 +0200618 if (ret == -1) {
Radek Krejci28681fa2019-09-06 13:08:45 +0200619 LOGMEM(ext->module->ctx);
Radek Krejci0935f412019-08-20 16:15:18 +0200620 return;
621 }
622
623 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200624 log_vprintf(ext->module->ctx, level, (level == LY_LLERR ? LY_EPLUGIN : 0) | err_no, LYVE_OTHER,
625 path ? strdup(path) : NULL, NULL, plugin_msg, ap);
Radek Krejci0935f412019-08-20 16:15:18 +0200626 va_end(ap);
627
628 free(plugin_msg);
629}
630
Michal Vasko177d0ed2020-11-23 16:43:03 +0100631/**
Michal Vaskoc78a6092021-05-07 15:27:35 +0200632 * @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 +0100633 */
634static void
Michal Vaskoc78a6092021-05-07 15:27:35 +0200635_ly_err_print(const struct ly_ctx *ctx, struct ly_err_item *eitem, const char *format, ...)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200636{
Michal Vasko177d0ed2020-11-23 16:43:03 +0100637 va_list ap;
638 char *path_dup = NULL;
639
640 LY_CHECK_ARG_RET(ctx, eitem, );
641
642 if (eitem->path) {
643 /* duplicate path because it will be freed */
644 path_dup = strdup(eitem->path);
645 LY_CHECK_ERR_RET(!path_dup, LOGMEM(ctx), );
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200646 }
Michal Vasko177d0ed2020-11-23 16:43:03 +0100647
Michal Vaskoc78a6092021-05-07 15:27:35 +0200648 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200649 log_vprintf(ctx, eitem->level, eitem->no, eitem->vecode, path_dup, eitem->apptag, format, ap);
Michal Vasko177d0ed2020-11-23 16:43:03 +0100650 va_end(ap);
Michal Vasko177d0ed2020-11-23 16:43:03 +0100651}
652
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100653LIBYANG_API_DEF void
Michal Vasko177d0ed2020-11-23 16:43:03 +0100654ly_err_print(const struct ly_ctx *ctx, struct ly_err_item *eitem)
655{
Michal Vaskoc78a6092021-05-07 15:27:35 +0200656 /* String ::ly_err_item.msg cannot be used directly because it may contain the % character */
657 _ly_err_print(ctx, eitem, "%s", eitem->msg);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200658}