Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file log.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 4 | * @author Michal Vasko <mvasko@cesnet.cz> |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 5 | * @brief Logger routines implementations |
| 6 | * |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 7 | * Copyright (c) 2015 - 2022 CESNET, z.s.p.o. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 8 | * |
| 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 Hopps | 32874e1 | 2021-05-01 09:43:54 -0400 | [diff] [blame] | 16 | #define _GNU_SOURCE /* asprintf, strdup */ |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 17 | |
| 18 | #include "log.h" |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 19 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 20 | #include <assert.h> |
Radek Krejci | c04f0a2 | 2018-09-21 15:49:45 +0200 | [diff] [blame] | 21 | #include <inttypes.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 22 | #include <pthread.h> |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 23 | #include <stdarg.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 24 | #include <stdint.h> |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 25 | #include <stdio.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 28 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 29 | #include "common.h" |
Radek Krejci | aa45bda | 2020-07-20 07:43:38 +0200 | [diff] [blame] | 30 | #include "compat.h" |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 31 | #include "in_internal.h" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 32 | #include "plugins_exts.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 33 | #include "set.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 34 | #include "tree_data.h" |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 35 | #include "tree_data_internal.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 36 | #include "tree_schema.h" |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 37 | #include "tree_schema_internal.h" |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 38 | |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 39 | ATOMIC_T ly_ll = (uint_fast32_t)LY_LLWRN; |
| 40 | ATOMIC_T ly_log_opts = (uint_fast32_t)(LY_LOLOG | LY_LOSTORE_LAST); |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 41 | THREAD_LOCAL uint32_t *temp_ly_log_opts; |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 42 | static ly_log_clb log_clb; |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 43 | static ATOMIC_T path_flag = 1; |
Michal Vasko | 236cbac | 2023-02-10 15:45:37 +0100 | [diff] [blame^] | 44 | THREAD_LOCAL char last_msg[LY_LAST_MSG_SIZE]; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 45 | #ifndef NDEBUG |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 46 | ATOMIC_T ly_ldbg_groups = 0; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 47 | #endif |
| 48 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 49 | THREAD_LOCAL struct ly_log_location_s log_location = {0}; |
| 50 | |
Michal Vasko | 236cbac | 2023-02-10 15:45:37 +0100 | [diff] [blame^] | 51 | LIBYANG_API_DEF const char * |
| 52 | ly_last_errmsg(void) |
| 53 | { |
| 54 | return last_msg; |
| 55 | } |
| 56 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 57 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 58 | ly_errcode(const struct ly_ctx *ctx) |
| 59 | { |
| 60 | struct ly_err_item *i; |
| 61 | |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 62 | i = ly_err_last(ctx); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 63 | if (i) { |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 64 | return i->no; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | return LY_SUCCESS; |
| 68 | } |
| 69 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 70 | LIBYANG_API_DEF LY_VECODE |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 71 | ly_vecode(const struct ly_ctx *ctx) |
| 72 | { |
| 73 | struct ly_err_item *i; |
| 74 | |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 75 | i = ly_err_last(ctx); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 76 | if (i) { |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 77 | return i->vecode; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | return LYVE_SUCCESS; |
| 81 | } |
| 82 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 83 | LIBYANG_API_DEF const char * |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 84 | ly_errmsg(const struct ly_ctx *ctx) |
| 85 | { |
| 86 | struct ly_err_item *i; |
| 87 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 88 | LY_CHECK_ARG_RET(NULL, ctx, NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 89 | |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 90 | i = ly_err_last(ctx); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 91 | if (i) { |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 92 | return i->msg; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | return NULL; |
| 96 | } |
| 97 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 98 | LIBYANG_API_DEF const char * |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 99 | ly_errpath(const struct ly_ctx *ctx) |
| 100 | { |
| 101 | struct ly_err_item *i; |
| 102 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 103 | LY_CHECK_ARG_RET(NULL, ctx, NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 104 | |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 105 | i = ly_err_last(ctx); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 106 | if (i) { |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 107 | return i->path; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | return NULL; |
| 111 | } |
| 112 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 113 | LIBYANG_API_DEF const char * |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 114 | ly_errapptag(const struct ly_ctx *ctx) |
| 115 | { |
| 116 | struct ly_err_item *i; |
| 117 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 118 | LY_CHECK_ARG_RET(NULL, ctx, NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 119 | |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 120 | i = ly_err_last(ctx); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 121 | if (i) { |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 122 | return i->apptag; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | return NULL; |
| 126 | } |
| 127 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 128 | LIBYANG_API_DEF LY_ERR |
aPiecek | 6d61855 | 2021-06-18 10:02:59 +0200 | [diff] [blame] | 129 | ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *path, char *apptag, const char *err_format, ...) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 130 | { |
Radek Krejci | db0ee02 | 2021-03-15 16:53:44 +0100 | [diff] [blame] | 131 | char *msg = NULL; |
| 132 | struct ly_err_item *e; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 133 | |
Radek Krejci | d43298b | 2021-03-25 16:17:15 +0100 | [diff] [blame] | 134 | if (!err || (ecode == LY_SUCCESS)) { |
Radek Krejci | db0ee02 | 2021-03-15 16:53:44 +0100 | [diff] [blame] | 135 | /* 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 Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 146 | |
| 147 | /* fill in the information */ |
Radek Krejci | db0ee02 | 2021-03-15 16:53:44 +0100 | [diff] [blame] | 148 | e->level = LY_LLERR; |
| 149 | e->no = ecode; |
| 150 | e->vecode = vecode; |
| 151 | e->path = path; |
| 152 | e->apptag = apptag; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 153 | |
aPiecek | 6d61855 | 2021-06-18 10:02:59 +0200 | [diff] [blame] | 154 | if (err_format) { |
Radek Krejci | db0ee02 | 2021-03-15 16:53:44 +0100 | [diff] [blame] | 155 | va_list print_args; |
| 156 | |
aPiecek | 6d61855 | 2021-06-18 10:02:59 +0200 | [diff] [blame] | 157 | va_start(print_args, err_format); |
Radek Krejci | db0ee02 | 2021-03-15 16:53:44 +0100 | [diff] [blame] | 158 | |
aPiecek | 6d61855 | 2021-06-18 10:02:59 +0200 | [diff] [blame] | 159 | 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 Krejci | db0ee02 | 2021-03-15 16:53:44 +0100 | [diff] [blame] | 161 | * 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 Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 174 | } |
| 175 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 176 | LIBYANG_API_DEF struct ly_err_item * |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 177 | ly_err_first(const struct ly_ctx *ctx) |
| 178 | { |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 179 | LY_CHECK_ARG_RET(NULL, ctx, NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 180 | |
| 181 | return pthread_getspecific(ctx->errlist_key); |
| 182 | } |
| 183 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 184 | LIBYANG_API_DEF struct ly_err_item * |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 185 | ly_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 Vasko | 9dbb91d | 2023-01-30 13:59:22 +0100 | [diff] [blame] | 195 | void |
| 196 | ly_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át | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 211 | LIBYANG_API_DEF void |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 212 | ly_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 Krejci | db0ee02 | 2021-03-15 16:53:44 +0100 | [diff] [blame] | 219 | free(i->msg); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 220 | free(i->path); |
| 221 | free(i->apptag); |
| 222 | free(i); |
| 223 | } |
| 224 | } |
| 225 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 226 | LIBYANG_API_DEF void |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 227 | ly_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 Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 237 | for (i = first; i && (i->next != eitem); i = i->next) {} |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 238 | 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át | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 250 | LIBYANG_API_DEF LY_LOG_LEVEL |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 251 | ly_log_level(LY_LOG_LEVEL level) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 252 | { |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 253 | LY_LOG_LEVEL prev = ATOMIC_LOAD_RELAXED(ly_ll); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 254 | |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 255 | ATOMIC_STORE_RELAXED(ly_ll, level); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 256 | return prev; |
| 257 | } |
| 258 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 259 | LIBYANG_API_DEF uint32_t |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 260 | ly_log_options(uint32_t opts) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 261 | { |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 262 | uint32_t prev = ATOMIC_LOAD_RELAXED(ly_log_opts); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 263 | |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 264 | ATOMIC_STORE_RELAXED(ly_log_opts, opts); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 265 | return prev; |
| 266 | } |
| 267 | |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 268 | LIBYANG_API_DEF void |
| 269 | ly_temp_log_options(uint32_t *opts) |
| 270 | { |
| 271 | temp_ly_log_opts = opts; |
| 272 | } |
| 273 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 274 | LIBYANG_API_DEF uint32_t |
Radek Krejci | 68433c9 | 2020-10-12 17:03:55 +0200 | [diff] [blame] | 275 | ly_log_dbg_groups(uint32_t dbg_groups) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 276 | { |
| 277 | #ifndef NDEBUG |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 278 | uint32_t prev = ATOMIC_LOAD_RELAXED(ly_ldbg_groups); |
Radek Krejci | ebdaed0 | 2020-11-09 13:05:06 +0100 | [diff] [blame] | 279 | |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 280 | ATOMIC_STORE_RELAXED(ly_ldbg_groups, dbg_groups); |
Radek Krejci | ebdaed0 | 2020-11-09 13:05:06 +0100 | [diff] [blame] | 281 | return prev; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 282 | #else |
| 283 | (void)dbg_groups; |
Radek Krejci | ebdaed0 | 2020-11-09 13:05:06 +0100 | [diff] [blame] | 284 | return 0; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 285 | #endif |
| 286 | } |
| 287 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 288 | LIBYANG_API_DEF void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 289 | ly_set_log_clb(ly_log_clb clb, ly_bool path) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 290 | { |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 291 | log_clb = clb; |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 292 | ATOMIC_STORE_RELAXED(path_flag, path); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 293 | } |
| 294 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 295 | LIBYANG_API_DEF ly_log_clb |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 296 | ly_get_log_clb(void) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 297 | { |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 298 | return log_clb; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 299 | } |
| 300 | |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 301 | void |
Michal Vasko | 59e69e7 | 2022-02-18 09:18:21 +0100 | [diff] [blame] | 302 | ly_log_location(const struct lysc_node *scnode, const struct lyd_node *dnode, const char *path, const struct ly_in *in, |
Michal Vasko | f8ebf13 | 2022-11-21 14:06:48 +0100 | [diff] [blame] | 303 | uint64_t line) |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 304 | { |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 305 | if (scnode) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 306 | ly_set_add(&log_location.scnodes, (void *)scnode, 1, NULL); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 307 | } |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 308 | if (dnode) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 309 | ly_set_add(&log_location.dnodes, (void *)dnode, 1, NULL); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 310 | } |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 311 | if (path) { |
| 312 | char *s = strdup(path); |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 313 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 314 | LY_CHECK_ERR_RET(!s, LOGMEM(NULL), ); |
| 315 | ly_set_add(&log_location.paths, s, 1, NULL); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 316 | } |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 317 | if (in) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 318 | ly_set_add(&log_location.inputs, (void *)in, 1, NULL); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 319 | } |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 320 | if (line) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 321 | log_location.line = line; |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
| 325 | void |
Michal Vasko | 59e69e7 | 2022-02-18 09:18:21 +0100 | [diff] [blame] | 326 | ly_log_location_revert(uint32_t scnode_steps, uint32_t dnode_steps, uint32_t path_steps, uint32_t in_steps) |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 327 | { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 328 | for (uint32_t i = scnode_steps; i && log_location.scnodes.count; i--) { |
| 329 | log_location.scnodes.count--; |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 330 | } |
| 331 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 332 | for (uint32_t i = dnode_steps; i && log_location.dnodes.count; i--) { |
| 333 | log_location.dnodes.count--; |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 334 | } |
| 335 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 336 | 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 Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 338 | } |
| 339 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 340 | for (uint32_t i = in_steps; i && log_location.inputs.count; i--) { |
| 341 | log_location.inputs.count--; |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 342 | } |
| 343 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 344 | /* deallocate the empty sets */ |
| 345 | if (scnode_steps && !log_location.scnodes.count) { |
| 346 | ly_set_erase(&log_location.scnodes, NULL); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 347 | } |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 348 | 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 Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 359 | static LY_ERR |
| 360 | log_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 Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 393 | } 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 Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 395 | /* 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 | |
| 419 | mem_fail: |
| 420 | LOGMEM(NULL); |
| 421 | free(msg); |
| 422 | free(path); |
| 423 | free(apptag); |
| 424 | return LY_EMEM; |
| 425 | } |
| 426 | |
| 427 | static void |
Michal Vasko | e9391c7 | 2021-10-05 10:04:56 +0200 | [diff] [blame] | 428 | log_vprintf(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, LY_VECODE vecode, char *path, const char *apptag, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 429 | const char *format, va_list args) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 430 | { |
| 431 | char *msg = NULL; |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 432 | 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 Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 442 | |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 443 | if (level > ATOMIC_LOAD_RELAXED(ly_ll)) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 444 | /* do not print or store the message */ |
| 445 | free(path); |
| 446 | return; |
| 447 | } |
| 448 | |
Michal Vasko | 5a01692 | 2021-05-07 08:19:15 +0200 | [diff] [blame] | 449 | if (no == LY_EMEM) { |
| 450 | /* just print it, anything else would most likely fail anyway */ |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 451 | if (lolog) { |
Michal Vasko | 5a01692 | 2021-05-07 08:19:15 +0200 | [diff] [blame] | 452 | 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 Vasko | 236cbac | 2023-02-10 15:45:37 +0100 | [diff] [blame^] | 468 | /* 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 Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 480 | if ((level < LY_LLVRB) && ctx && lostore) { |
Radek Krejci | c9e64a6 | 2020-09-18 20:08:12 +0200 | [diff] [blame] | 481 | 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 Vasko | e9391c7 | 2021-10-05 10:04:56 +0200 | [diff] [blame] | 485 | if (log_store(ctx, level, no, vecode, msg, path, apptag ? strdup(apptag) : NULL)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 486 | return; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 487 | } |
| 488 | free_strs = 0; |
| 489 | } else { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 490 | free_strs = 1; |
| 491 | } |
| 492 | |
| 493 | /* if we are only storing errors internally, never print the message (yet) */ |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 494 | if (lolog) { |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 495 | if (log_clb) { |
| 496 | log_clb(level, msg, path); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 497 | } 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 Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 511 | #ifndef NDEBUG |
| 512 | |
| 513 | void |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 514 | ly_log_dbg(uint32_t group, const char *format, ...) |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 515 | { |
| 516 | char *dbg_format; |
| 517 | const char *str_group; |
| 518 | va_list ap; |
| 519 | |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 520 | if (!(ATOMIC_LOAD_RELAXED(ly_ldbg_groups) & group)) { |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 521 | return; |
| 522 | } |
| 523 | |
| 524 | switch (group) { |
| 525 | case LY_LDGDICT: |
| 526 | str_group = "DICT"; |
| 527 | break; |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 528 | case LY_LDGXPATH: |
| 529 | str_group = "XPATH"; |
| 530 | break; |
Michal Vasko | e558f79 | 2021-07-28 08:20:15 +0200 | [diff] [blame] | 531 | case LY_LDGDEPSETS: |
| 532 | str_group = "DEPSETS"; |
| 533 | break; |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 534 | 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 Vasko | e9391c7 | 2021-10-05 10:04:56 +0200 | [diff] [blame] | 545 | log_vprintf(NULL, LY_LLDBG, 0, 0, NULL, NULL, dbg_format, ap); |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 546 | va_end(ap); |
| 547 | } |
| 548 | |
| 549 | #endif |
| 550 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 551 | void |
| 552 | ly_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 Vasko | e9391c7 | 2021-10-05 10:04:56 +0200 | [diff] [blame] | 557 | log_vprintf(ctx, level, no, 0, NULL, NULL, format, ap); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 558 | va_end(ap); |
| 559 | } |
| 560 | |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 561 | /** |
| 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 | */ |
| 569 | static LY_ERR |
| 570 | ly_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 Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 610 | static LY_ERR |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 611 | ly_vlog_build_path(const struct ly_ctx *ctx, char **path) |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 612 | { |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 613 | int r; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 614 | char *str = NULL, *prev = NULL; |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 615 | const struct lyd_node *dnode; |
Radek Krejci | cb3e647 | 2021-01-06 08:19:01 +0100 | [diff] [blame] | 616 | |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 617 | *path = NULL; |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 618 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 619 | if (log_location.paths.count && ((const char *)(log_location.paths.objs[log_location.paths.count - 1]))[0]) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 620 | /* simply get what is in the provided path string */ |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 621 | *path = strdup((const char *)log_location.paths.objs[log_location.paths.count - 1]); |
Radek Krejci | c04f0a2 | 2018-09-21 15:49:45 +0200 | [diff] [blame] | 622 | LY_CHECK_ERR_RET(!(*path), LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 623 | } else { |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 624 | /* data/schema node */ |
| 625 | if (log_location.dnodes.count) { |
| 626 | dnode = log_location.dnodes.objs[log_location.dnodes.count - 1]; |
Michal Vasko | a4dfb3c | 2022-10-25 14:59:31 +0200 | [diff] [blame] | 627 | 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 Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 632 | /* 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 Vasko | 3e65ee3 | 2022-10-21 10:09:51 +0200 | [diff] [blame] | 634 | LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM); |
Michal Vasko | a4dfb3c | 2022-10-25 14:59:31 +0200 | [diff] [blame] | 635 | } |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 636 | |
Michal Vasko | a4dfb3c | 2022-10-25 14:59:31 +0200 | [diff] [blame] | 637 | /* 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 Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 640 | } |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 641 | |
| 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 Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 646 | str = lysc_path(log_location.scnodes.objs[log_location.scnodes.count - 1], LYSC_PATH_LOG, NULL, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 647 | LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM); |
| 648 | |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 649 | r = asprintf(path, "Schema location \"%s\"", str); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 650 | free(str); |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 651 | LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 652 | } |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 653 | |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 654 | /* line */ |
| 655 | prev = *path; |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 656 | if (log_location.line) { |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 657 | r = asprintf(path, "%s%sine number %" PRIu64, prev ? prev : "", prev ? ", l" : "L", log_location.line); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 658 | free(prev); |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 659 | LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 660 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 661 | log_location.line = 0; |
| 662 | } else if (log_location.inputs.count) { |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 663 | r = asprintf(path, "%s%sine number %" PRIu64, prev ? prev : "", prev ? ", l" : "L", |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 664 | ((struct ly_in *)log_location.inputs.objs[log_location.inputs.count - 1])->line); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 665 | free(prev); |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 666 | LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | if (*path) { |
| 670 | prev = *path; |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 671 | r = asprintf(path, "%s.", prev); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 672 | free(prev); |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 673 | LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 674 | } |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 675 | } |
| 676 | |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 677 | return LY_SUCCESS; |
| 678 | } |
| 679 | |
| 680 | void |
Michal Vasko | e9391c7 | 2021-10-05 10:04:56 +0200 | [diff] [blame] | 681 | ly_vlog(const struct ly_ctx *ctx, const char *apptag, LY_VECODE code, const char *format, ...) |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 682 | { |
| 683 | va_list ap; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 684 | char *path = NULL; |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 685 | |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 686 | if (ATOMIC_LOAD_RELAXED(path_flag) && ctx) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 687 | ly_vlog_build_path(ctx, &path); |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | va_start(ap, format); |
Michal Vasko | e9391c7 | 2021-10-05 10:04:56 +0200 | [diff] [blame] | 691 | log_vprintf(ctx, LY_LLERR, LY_EVALID, code, path, apptag, format, ap); |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 692 | /* path is spent and should not be freed! */ |
| 693 | va_end(ap); |
| 694 | } |
| 695 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 696 | /** |
| 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. |
| 703 | * @param[in] path Optional path of the error. |
| 704 | * @param[in] format Format string to print. |
| 705 | * @param[in] ap Var arg list for @p format. |
| 706 | */ |
| 707 | static void |
| 708 | ly_ext_log(const struct ly_ctx *ctx, const char *plugin_name, LY_LOG_LEVEL level, LY_ERR err_no, const char *path, |
| 709 | const char *format, va_list ap) |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 710 | { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 711 | char *plugin_msg; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 712 | |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 713 | if (ATOMIC_LOAD_RELAXED(ly_ll) < level) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 714 | return; |
| 715 | } |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 716 | if (asprintf(&plugin_msg, "Ext plugin \"%s\": %s", plugin_name, format) == -1) { |
| 717 | LOGMEM(ctx); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 718 | return; |
| 719 | } |
| 720 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 721 | log_vprintf(ctx, level, (level == LY_LLERR ? LY_EPLUGIN : 0) | err_no, LYVE_OTHER, path ? strdup(path) : NULL, NULL, |
| 722 | plugin_msg, ap); |
| 723 | free(plugin_msg); |
| 724 | } |
| 725 | |
| 726 | LIBYANG_API_DEF void |
| 727 | lyplg_ext_parse_log(const struct lysp_ctx *pctx, const struct lysp_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no, |
| 728 | const char *format, ...) |
| 729 | { |
| 730 | va_list ap; |
| 731 | char *path = NULL; |
| 732 | |
| 733 | if (ATOMIC_LOAD_RELAXED(path_flag)) { |
| 734 | ly_vlog_build_path(PARSER_CTX(pctx), &path); |
| 735 | } |
| 736 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 737 | va_start(ap, format); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 738 | ly_ext_log(PARSER_CTX(pctx), ext->record->plugin.id, level, err_no, path, format, ap); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 739 | va_end(ap); |
| 740 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 741 | free(path); |
| 742 | } |
| 743 | |
| 744 | LIBYANG_API_DEF void |
| 745 | lyplg_ext_compile_log(const struct lysc_ctx *cctx, const struct lysc_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no, |
| 746 | const char *format, ...) |
| 747 | { |
| 748 | va_list ap; |
| 749 | |
| 750 | va_start(ap, format); |
| 751 | ly_ext_log(ext->module->ctx, ext->def->plugin->id, level, err_no, cctx ? cctx->path : NULL, format, ap); |
| 752 | va_end(ap); |
| 753 | } |
| 754 | |
| 755 | LIBYANG_API_DEF void |
| 756 | lyplg_ext_compile_log_path(const char *path, const struct lysc_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no, |
| 757 | const char *format, ...) |
| 758 | { |
| 759 | va_list ap; |
| 760 | |
| 761 | va_start(ap, format); |
| 762 | ly_ext_log(ext->module->ctx, ext->def->plugin->id, level, err_no, path, format, ap); |
| 763 | va_end(ap); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 764 | } |
| 765 | |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 766 | /** |
Michal Vasko | c78a609 | 2021-05-07 15:27:35 +0200 | [diff] [blame] | 767 | * @brief Exact same functionality as ::ly_err_print() but has variable arguments so log_vprintf() can be called. |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 768 | */ |
| 769 | static void |
Michal Vasko | c78a609 | 2021-05-07 15:27:35 +0200 | [diff] [blame] | 770 | _ly_err_print(const struct ly_ctx *ctx, struct ly_err_item *eitem, const char *format, ...) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 771 | { |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 772 | va_list ap; |
| 773 | char *path_dup = NULL; |
| 774 | |
| 775 | LY_CHECK_ARG_RET(ctx, eitem, ); |
| 776 | |
| 777 | if (eitem->path) { |
| 778 | /* duplicate path because it will be freed */ |
| 779 | path_dup = strdup(eitem->path); |
| 780 | LY_CHECK_ERR_RET(!path_dup, LOGMEM(ctx), ); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 781 | } |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 782 | |
Michal Vasko | c78a609 | 2021-05-07 15:27:35 +0200 | [diff] [blame] | 783 | va_start(ap, format); |
Michal Vasko | e9391c7 | 2021-10-05 10:04:56 +0200 | [diff] [blame] | 784 | log_vprintf(ctx, eitem->level, eitem->no, eitem->vecode, path_dup, eitem->apptag, format, ap); |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 785 | va_end(ap); |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 786 | } |
| 787 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 788 | LIBYANG_API_DEF void |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 789 | ly_err_print(const struct ly_ctx *ctx, struct ly_err_item *eitem) |
| 790 | { |
Michal Vasko | c78a609 | 2021-05-07 15:27:35 +0200 | [diff] [blame] | 791 | /* String ::ly_err_item.msg cannot be used directly because it may contain the % character */ |
| 792 | _ly_err_print(ctx, eitem, "%s", eitem->msg); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 793 | } |