blob: b8f9f18e059b7b2dd7479344ecc22533b1295b28 [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
Michal Vasko59e69e72022-02-18 09:18:21 +0100272ly_log_location(const struct lysc_node *scnode, const struct lyd_node *dnode, const char *path, const struct ly_in *in,
273 uint64_t line, ly_bool reset)
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100274{
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
Michal Vasko59e69e72022-02-18 09:18:21 +0100307ly_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 +0100308{
Radek Krejciddace2c2021-01-08 11:30:56 +0100309 for (uint32_t i = scnode_steps; i && log_location.scnodes.count; i--) {
310 log_location.scnodes.count--;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100311 }
312
Radek Krejciddace2c2021-01-08 11:30:56 +0100313 for (uint32_t i = dnode_steps; i && log_location.dnodes.count; i--) {
314 log_location.dnodes.count--;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100315 }
316
Radek Krejciddace2c2021-01-08 11:30:56 +0100317 for (uint32_t i = path_steps; i && log_location.paths.count; i--) {
318 ly_set_rm_index(&log_location.paths, log_location.paths.count - 1, free);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100319 }
320
Radek Krejciddace2c2021-01-08 11:30:56 +0100321 for (uint32_t i = in_steps; i && log_location.inputs.count; i--) {
322 log_location.inputs.count--;
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100323 }
324
Radek Krejciddace2c2021-01-08 11:30:56 +0100325 /* deallocate the empty sets */
326 if (scnode_steps && !log_location.scnodes.count) {
327 ly_set_erase(&log_location.scnodes, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100328 }
Radek Krejciddace2c2021-01-08 11:30:56 +0100329 if (dnode_steps && !log_location.dnodes.count) {
330 ly_set_erase(&log_location.dnodes, NULL);
331 }
332 if (path_steps && !log_location.paths.count) {
333 ly_set_erase(&log_location.paths, free);
334 }
335 if (in_steps && !log_location.inputs.count) {
336 ly_set_erase(&log_location.inputs, NULL);
Radek Krejciaddfc9a2020-12-17 20:46:35 +0100337 }
338}
339
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200340static LY_ERR
341log_store(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, LY_VECODE vecode, char *msg, char *path, char *apptag)
342{
343 struct ly_err_item *eitem, *last;
344
345 assert(ctx && (level < LY_LLVRB));
346
347 eitem = pthread_getspecific(ctx->errlist_key);
348 if (!eitem) {
349 /* if we are only to fill in path, there must have been an error stored */
350 assert(msg);
351 eitem = malloc(sizeof *eitem);
352 LY_CHECK_GOTO(!eitem, mem_fail);
353 eitem->prev = eitem;
354 eitem->next = NULL;
355
356 pthread_setspecific(ctx->errlist_key, eitem);
357 } else if (!msg) {
358 /* only filling the path */
359 assert(path);
360
361 /* find last error */
362 eitem = eitem->prev;
363 do {
364 if (eitem->level == LY_LLERR) {
365 /* fill the path */
366 free(eitem->path);
367 eitem->path = path;
368 return LY_SUCCESS;
369 }
370 eitem = eitem->prev;
371 } while (eitem->prev->next);
372 /* last error was not found */
373 assert(0);
Václav Kubernátd367ad92021-11-29 09:28:56 +0100374 } else if ((ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOSTORE_LAST) == LY_LOSTORE_LAST) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200375 /* overwrite last message */
376 free(eitem->msg);
377 free(eitem->path);
378 free(eitem->apptag);
379 } else {
380 /* store new message */
381 last = eitem->prev;
382 eitem->prev = malloc(sizeof *eitem);
383 LY_CHECK_GOTO(!eitem->prev, mem_fail);
384 eitem = eitem->prev;
385 eitem->prev = last;
386 eitem->next = NULL;
387 last->next = eitem;
388 }
389
390 /* fill in the information */
391 eitem->level = level;
392 eitem->no = no;
393 eitem->vecode = vecode;
394 eitem->msg = msg;
395 eitem->path = path;
396 eitem->apptag = apptag;
397 return LY_SUCCESS;
398
399mem_fail:
400 LOGMEM(NULL);
401 free(msg);
402 free(path);
403 free(apptag);
404 return LY_EMEM;
405}
406
407static void
Michal Vaskoe9391c72021-10-05 10:04:56 +0200408log_vprintf(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, LY_VECODE vecode, char *path, const char *apptag,
Radek Krejci0f969882020-08-21 16:56:47 +0200409 const char *format, va_list args)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200410{
411 char *msg = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200412 ly_bool free_strs;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200413
Václav Kubernátd367ad92021-11-29 09:28:56 +0100414 if (level > ATOMIC_LOAD_RELAXED(ly_ll)) {
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200415 /* do not print or store the message */
416 free(path);
417 return;
418 }
419
Michal Vasko5a016922021-05-07 08:19:15 +0200420 if (no == LY_EMEM) {
421 /* just print it, anything else would most likely fail anyway */
Václav Kubernátd367ad92021-11-29 09:28:56 +0100422 if (ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOLOG) {
Michal Vasko5a016922021-05-07 08:19:15 +0200423 if (log_clb) {
424 log_clb(level, LY_EMEM_MSG, path);
425 } else {
426 fprintf(stderr, "libyang[%d]: ", level);
427 vfprintf(stderr, format, args);
428 if (path) {
429 fprintf(stderr, " (path: %s)\n", path);
430 } else {
431 fprintf(stderr, "\n");
432 }
433 }
434 }
435 free(path);
436 return;
437 }
438
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200439 /* 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 +0100440 if ((level < LY_LLVRB) && ctx && (ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOSTORE)) {
Michal Vasko004d3152020-06-11 19:59:22 +0200441 assert(format);
442 if (vasprintf(&msg, format, args) == -1) {
443 LOGMEM(ctx);
444 free(path);
445 return;
446 }
Radek Krejcic9e64a62020-09-18 20:08:12 +0200447 if (((no & ~LY_EPLUGIN) == LY_EVALID) && (vecode == LYVE_SUCCESS)) {
448 /* assume we are inheriting the error, so inherit vecode as well */
449 vecode = ly_vecode(ctx);
450 }
Michal Vaskoe9391c72021-10-05 10:04:56 +0200451 if (log_store(ctx, level, no, vecode, msg, path, apptag ? strdup(apptag) : NULL)) {
Michal Vasko004d3152020-06-11 19:59:22 +0200452 return;
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200453 }
454 free_strs = 0;
455 } else {
456 if (vasprintf(&msg, format, args) == -1) {
457 LOGMEM(ctx);
458 free(path);
459 return;
460 }
461 free_strs = 1;
462 }
463
464 /* if we are only storing errors internally, never print the message (yet) */
Václav Kubernátd367ad92021-11-29 09:28:56 +0100465 if (ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOLOG) {
Michal Vaskod8085612020-08-21 12:55:23 +0200466 if (log_clb) {
467 log_clb(level, msg, path);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200468 } else {
469 fprintf(stderr, "libyang[%d]: %s%s", level, msg, path ? " " : "\n");
470 if (path) {
471 fprintf(stderr, "(path: %s)\n", path);
472 }
473 }
474 }
475
476 if (free_strs) {
477 free(path);
478 free(msg);
479 }
480}
481
Radek Krejci4ab61562018-09-05 15:00:37 +0200482#ifndef NDEBUG
483
484void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200485ly_log_dbg(uint32_t group, const char *format, ...)
Radek Krejci4ab61562018-09-05 15:00:37 +0200486{
487 char *dbg_format;
488 const char *str_group;
489 va_list ap;
490
Václav Kubernátd367ad92021-11-29 09:28:56 +0100491 if (!(ATOMIC_LOAD_RELAXED(ly_ldbg_groups) & group)) {
Radek Krejci4ab61562018-09-05 15:00:37 +0200492 return;
493 }
494
495 switch (group) {
496 case LY_LDGDICT:
497 str_group = "DICT";
498 break;
Radek Krejci4ab61562018-09-05 15:00:37 +0200499 case LY_LDGXPATH:
500 str_group = "XPATH";
501 break;
Michal Vaskoe558f792021-07-28 08:20:15 +0200502 case LY_LDGDEPSETS:
503 str_group = "DEPSETS";
504 break;
Radek Krejci4ab61562018-09-05 15:00:37 +0200505 default:
506 LOGINT(NULL);
507 return;
508 }
509
510 if (asprintf(&dbg_format, "%s: %s", str_group, format) == -1) {
511 LOGMEM(NULL);
512 return;
513 }
514
515 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200516 log_vprintf(NULL, LY_LLDBG, 0, 0, NULL, NULL, dbg_format, ap);
Radek Krejci4ab61562018-09-05 15:00:37 +0200517 va_end(ap);
518}
519
520#endif
521
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200522void
523ly_log(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, const char *format, ...)
524{
525 va_list ap;
526
527 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200528 log_vprintf(ctx, level, no, 0, NULL, NULL, format, ap);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200529 va_end(ap);
530}
531
Radek Krejci94aa9942018-09-07 17:12:17 +0200532static LY_ERR
Radek Krejciddace2c2021-01-08 11:30:56 +0100533ly_vlog_build_path(const struct ly_ctx *ctx, char **path)
Radek Krejci94aa9942018-09-07 17:12:17 +0200534{
Radek Krejcic04f0a22018-09-21 15:49:45 +0200535 int rc;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100536 char *str = NULL, *prev = NULL;
Radek Krejcicb3e6472021-01-06 08:19:01 +0100537
Radek Krejci2efc45b2020-12-22 16:25:44 +0100538 *path = NULL;
Radek Krejci94aa9942018-09-07 17:12:17 +0200539
Radek Krejciddace2c2021-01-08 11:30:56 +0100540 if (log_location.paths.count && ((const char *)(log_location.paths.objs[log_location.paths.count - 1]))[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100541 /* simply get what is in the provided path string */
Radek Krejciddace2c2021-01-08 11:30:56 +0100542 *path = strdup((const char *)log_location.paths.objs[log_location.paths.count - 1]);
Radek Krejcic04f0a22018-09-21 15:49:45 +0200543 LY_CHECK_ERR_RET(!(*path), LOGMEM(ctx), LY_EMEM);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100544 } else {
545 /* generate location string */
Radek Krejciddace2c2021-01-08 11:30:56 +0100546 if (log_location.scnodes.count) {
547 str = lysc_path(log_location.scnodes.objs[log_location.scnodes.count - 1], LYSC_PATH_LOG, NULL, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100548 LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM);
549
550 rc = asprintf(path, "Schema location %s", str);
551 free(str);
552 LY_CHECK_ERR_RET(rc == -1, LOGMEM(ctx), LY_EMEM);
553 }
Radek Krejciddace2c2021-01-08 11:30:56 +0100554 if (log_location.dnodes.count) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100555 prev = *path;
Radek Krejciddace2c2021-01-08 11:30:56 +0100556 str = lyd_path(log_location.dnodes.objs[log_location.dnodes.count - 1], LYD_PATH_STD, NULL, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100557 LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM);
558
559 rc = asprintf(path, "%s%sata location %s", prev ? prev : "", prev ? ", d" : "D", str);
560 free(str);
561 free(prev);
562 LY_CHECK_ERR_RET(rc == -1, LOGMEM(ctx), LY_EMEM);
563 }
Radek Krejciddace2c2021-01-08 11:30:56 +0100564 if (log_location.line) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100565 prev = *path;
Radek Krejciddace2c2021-01-08 11:30:56 +0100566 rc = asprintf(path, "%s%sine number %" PRIu64, prev ? prev : "", prev ? ", l" : "L", log_location.line);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100567 free(prev);
568 LY_CHECK_ERR_RET(rc == -1, LOGMEM(ctx), LY_EMEM);
569
Radek Krejciddace2c2021-01-08 11:30:56 +0100570 log_location.line = 0;
571 } else if (log_location.inputs.count) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100572 prev = *path;
573 rc = asprintf(path, "%s%sine number %" PRIu64, prev ? prev : "", prev ? ", l" : "L",
Radek Krejciddace2c2021-01-08 11:30:56 +0100574 ((struct ly_in *)log_location.inputs.objs[log_location.inputs.count - 1])->line);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100575 free(prev);
576 LY_CHECK_ERR_RET(rc == -1, LOGMEM(ctx), LY_EMEM);
577 }
578
579 if (*path) {
580 prev = *path;
581 rc = asprintf(path, "%s.", prev);
582 free(prev);
583 LY_CHECK_ERR_RET(rc == -1, LOGMEM(ctx), LY_EMEM);
584 }
Radek Krejci94aa9942018-09-07 17:12:17 +0200585 }
586
Radek Krejci94aa9942018-09-07 17:12:17 +0200587 return LY_SUCCESS;
588}
589
590void
Michal Vaskoe9391c72021-10-05 10:04:56 +0200591ly_vlog(const struct ly_ctx *ctx, const char *apptag, LY_VECODE code, const char *format, ...)
Radek Krejci94aa9942018-09-07 17:12:17 +0200592{
593 va_list ap;
Michal Vasko22df3f02020-08-24 13:29:22 +0200594 char *path = NULL;
Radek Krejci94aa9942018-09-07 17:12:17 +0200595
Václav Kubernátd367ad92021-11-29 09:28:56 +0100596 if (ATOMIC_LOAD_RELAXED(path_flag) && ctx) {
Radek Krejciddace2c2021-01-08 11:30:56 +0100597 ly_vlog_build_path(ctx, &path);
Radek Krejci94aa9942018-09-07 17:12:17 +0200598 }
599
600 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200601 log_vprintf(ctx, LY_LLERR, LY_EVALID, code, path, apptag, format, ap);
Radek Krejci94aa9942018-09-07 17:12:17 +0200602 /* path is spent and should not be freed! */
603 va_end(ap);
604}
605
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100606LIBYANG_API_DEF void
Radek Krejci0b013302021-03-29 15:22:32 +0200607lyplg_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 +0200608{
609 va_list ap;
610 char *plugin_msg;
611 int ret;
612
Václav Kubernátd367ad92021-11-29 09:28:56 +0100613 if (ATOMIC_LOAD_RELAXED(ly_ll) < level) {
Radek Krejci0935f412019-08-20 16:15:18 +0200614 return;
615 }
Radek Krejci932d3bb2021-02-09 16:29:38 +0100616 ret = asprintf(&plugin_msg, "Extension plugin \"%s\": %s", ext->def->plugin->id, format);
Radek Krejci0935f412019-08-20 16:15:18 +0200617 if (ret == -1) {
Radek Krejci28681fa2019-09-06 13:08:45 +0200618 LOGMEM(ext->module->ctx);
Radek Krejci0935f412019-08-20 16:15:18 +0200619 return;
620 }
621
622 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200623 log_vprintf(ext->module->ctx, level, (level == LY_LLERR ? LY_EPLUGIN : 0) | err_no, LYVE_OTHER,
624 path ? strdup(path) : NULL, NULL, plugin_msg, ap);
Radek Krejci0935f412019-08-20 16:15:18 +0200625 va_end(ap);
626
627 free(plugin_msg);
628}
629
Michal Vasko177d0ed2020-11-23 16:43:03 +0100630/**
Michal Vaskoc78a6092021-05-07 15:27:35 +0200631 * @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 +0100632 */
633static void
Michal Vaskoc78a6092021-05-07 15:27:35 +0200634_ly_err_print(const struct ly_ctx *ctx, struct ly_err_item *eitem, const char *format, ...)
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200635{
Michal Vasko177d0ed2020-11-23 16:43:03 +0100636 va_list ap;
637 char *path_dup = NULL;
638
639 LY_CHECK_ARG_RET(ctx, eitem, );
640
641 if (eitem->path) {
642 /* duplicate path because it will be freed */
643 path_dup = strdup(eitem->path);
644 LY_CHECK_ERR_RET(!path_dup, LOGMEM(ctx), );
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200645 }
Michal Vasko177d0ed2020-11-23 16:43:03 +0100646
Michal Vaskoc78a6092021-05-07 15:27:35 +0200647 va_start(ap, format);
Michal Vaskoe9391c72021-10-05 10:04:56 +0200648 log_vprintf(ctx, eitem->level, eitem->no, eitem->vecode, path_dup, eitem->apptag, format, ap);
Michal Vasko177d0ed2020-11-23 16:43:03 +0100649 va_end(ap);
Michal Vasko177d0ed2020-11-23 16:43:03 +0100650}
651
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100652LIBYANG_API_DEF void
Michal Vasko177d0ed2020-11-23 16:43:03 +0100653ly_err_print(const struct ly_ctx *ctx, struct ly_err_item *eitem)
654{
Michal Vaskoc78a6092021-05-07 15:27:35 +0200655 /* String ::ly_err_item.msg cannot be used directly because it may contain the % character */
656 _ly_err_print(ctx, eitem, "%s", eitem->msg);
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200657}