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 | b5b883c | 2023-07-10 10:36:18 +0200 | [diff] [blame] | 7 | * Copyright (c) 2015 - 2023 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 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 51 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 52 | ly_errcode(const struct ly_ctx *ctx) |
| 53 | { |
| 54 | struct ly_err_item *i; |
| 55 | |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 56 | i = ly_err_last(ctx); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 57 | if (i) { |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 58 | return i->no; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | return LY_SUCCESS; |
| 62 | } |
| 63 | |
Michal Vasko | b5b883c | 2023-07-10 10:36:18 +0200 | [diff] [blame] | 64 | LIBYANG_API_DEF const char * |
| 65 | ly_strerrcode(LY_ERR err) |
| 66 | { |
| 67 | /* ignore plugin flag */ |
| 68 | err &= ~LY_EPLUGIN; |
| 69 | |
| 70 | switch (err) { |
| 71 | case LY_SUCCESS: |
Michal Vasko | e420765 | 2023-07-10 14:57:32 +0200 | [diff] [blame] | 72 | return "Success"; |
Michal Vasko | b5b883c | 2023-07-10 10:36:18 +0200 | [diff] [blame] | 73 | case LY_EMEM: |
| 74 | return "Out of memory"; |
| 75 | case LY_ESYS: |
| 76 | return "System call failed"; |
| 77 | case LY_EINVAL: |
| 78 | return "Invalid value"; |
| 79 | case LY_EEXIST: |
| 80 | return "Already exists"; |
| 81 | case LY_ENOTFOUND: |
| 82 | return "Not found"; |
| 83 | case LY_EINT: |
| 84 | return "Internal error"; |
| 85 | case LY_EVALID: |
| 86 | return "Validation failed"; |
| 87 | case LY_EDENIED: |
| 88 | return "Operation denied"; |
| 89 | case LY_EINCOMPLETE: |
| 90 | return "Operation incomplete"; |
| 91 | case LY_ERECOMPILE: |
| 92 | return "Recompilation required"; |
| 93 | case LY_ENOT: |
| 94 | return "Negative result"; |
| 95 | case LY_EOTHER: |
| 96 | return "Another failure reason"; |
| 97 | case LY_EPLUGIN: |
| 98 | break; |
| 99 | } |
| 100 | |
| 101 | /* unreachable */ |
Michal Vasko | b87e9a1 | 2023-07-10 15:10:58 +0200 | [diff] [blame] | 102 | return "Unknown"; |
Michal Vasko | b5b883c | 2023-07-10 10:36:18 +0200 | [diff] [blame] | 103 | } |
| 104 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 105 | LIBYANG_API_DEF LY_VECODE |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 106 | ly_vecode(const struct ly_ctx *ctx) |
| 107 | { |
| 108 | struct ly_err_item *i; |
| 109 | |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 110 | i = ly_err_last(ctx); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 111 | if (i) { |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 112 | return i->vecode; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | return LYVE_SUCCESS; |
| 116 | } |
| 117 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 118 | LIBYANG_API_DEF const char * |
Michal Vasko | b5b883c | 2023-07-10 10:36:18 +0200 | [diff] [blame] | 119 | ly_strvecode(LY_VECODE vecode) |
| 120 | { |
| 121 | switch (vecode) { |
| 122 | case LYVE_SUCCESS: |
Michal Vasko | e420765 | 2023-07-10 14:57:32 +0200 | [diff] [blame] | 123 | return "Success"; |
Michal Vasko | b5b883c | 2023-07-10 10:36:18 +0200 | [diff] [blame] | 124 | case LYVE_SYNTAX: |
| 125 | return "General syntax error"; |
| 126 | case LYVE_SYNTAX_YANG: |
| 127 | return "YANG syntax error"; |
| 128 | case LYVE_SYNTAX_YIN: |
| 129 | return "YIN syntax error"; |
| 130 | case LYVE_REFERENCE: |
| 131 | return "Reference error"; |
| 132 | case LYVE_XPATH: |
| 133 | return "XPath error"; |
| 134 | case LYVE_SEMANTICS: |
| 135 | return "Semantic error"; |
| 136 | case LYVE_SYNTAX_XML: |
| 137 | return "XML syntax error"; |
| 138 | case LYVE_SYNTAX_JSON: |
| 139 | return "JSON syntax error"; |
| 140 | case LYVE_DATA: |
| 141 | return "YANG data error"; |
| 142 | case LYVE_OTHER: |
| 143 | return "Another error"; |
| 144 | } |
| 145 | |
| 146 | /* unreachable */ |
Michal Vasko | b87e9a1 | 2023-07-10 15:10:58 +0200 | [diff] [blame] | 147 | return "Unknown"; |
Michal Vasko | b5b883c | 2023-07-10 10:36:18 +0200 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | LIBYANG_API_DEF const char * |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 151 | ly_errmsg(const struct ly_ctx *ctx) |
| 152 | { |
| 153 | struct ly_err_item *i; |
| 154 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 155 | LY_CHECK_ARG_RET(NULL, ctx, NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 156 | |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 157 | i = ly_err_last(ctx); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 158 | if (i) { |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 159 | return i->msg; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | return NULL; |
| 163 | } |
| 164 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 165 | LIBYANG_API_DEF const char * |
Michal Vasko | b5b883c | 2023-07-10 10:36:18 +0200 | [diff] [blame] | 166 | ly_last_errmsg(void) |
| 167 | { |
| 168 | return last_msg; |
| 169 | } |
| 170 | |
| 171 | LIBYANG_API_DEF const char * |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 172 | ly_errpath(const struct ly_ctx *ctx) |
| 173 | { |
| 174 | struct ly_err_item *i; |
| 175 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 176 | LY_CHECK_ARG_RET(NULL, ctx, NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 177 | |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 178 | i = ly_err_last(ctx); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 179 | if (i) { |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 180 | return i->path; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | return NULL; |
| 184 | } |
| 185 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 186 | LIBYANG_API_DEF const char * |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 187 | ly_errapptag(const struct ly_ctx *ctx) |
| 188 | { |
| 189 | struct ly_err_item *i; |
| 190 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 191 | LY_CHECK_ARG_RET(NULL, ctx, NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 192 | |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 193 | i = ly_err_last(ctx); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 194 | if (i) { |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 195 | return i->apptag; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | return NULL; |
| 199 | } |
| 200 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 201 | LIBYANG_API_DEF LY_ERR |
aPiecek | 6d61855 | 2021-06-18 10:02:59 +0200 | [diff] [blame] | 202 | 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] | 203 | { |
Radek Krejci | db0ee02 | 2021-03-15 16:53:44 +0100 | [diff] [blame] | 204 | char *msg = NULL; |
| 205 | struct ly_err_item *e; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 206 | |
Radek Krejci | d43298b | 2021-03-25 16:17:15 +0100 | [diff] [blame] | 207 | if (!err || (ecode == LY_SUCCESS)) { |
Radek Krejci | db0ee02 | 2021-03-15 16:53:44 +0100 | [diff] [blame] | 208 | /* nothing to do */ |
| 209 | return ecode; |
| 210 | } |
| 211 | |
| 212 | e = malloc(sizeof *e); |
| 213 | LY_CHECK_ERR_RET(!e, LOGMEM(NULL), LY_EMEM); |
| 214 | e->prev = (*err) ? (*err)->prev : e; |
| 215 | e->next = NULL; |
| 216 | if (*err) { |
| 217 | (*err)->prev->next = e; |
| 218 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 219 | |
| 220 | /* fill in the information */ |
Radek Krejci | db0ee02 | 2021-03-15 16:53:44 +0100 | [diff] [blame] | 221 | e->level = LY_LLERR; |
| 222 | e->no = ecode; |
| 223 | e->vecode = vecode; |
| 224 | e->path = path; |
| 225 | e->apptag = apptag; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 226 | |
aPiecek | 6d61855 | 2021-06-18 10:02:59 +0200 | [diff] [blame] | 227 | if (err_format) { |
Radek Krejci | db0ee02 | 2021-03-15 16:53:44 +0100 | [diff] [blame] | 228 | va_list print_args; |
| 229 | |
aPiecek | 6d61855 | 2021-06-18 10:02:59 +0200 | [diff] [blame] | 230 | va_start(print_args, err_format); |
Radek Krejci | db0ee02 | 2021-03-15 16:53:44 +0100 | [diff] [blame] | 231 | |
aPiecek | 6d61855 | 2021-06-18 10:02:59 +0200 | [diff] [blame] | 232 | if (vasprintf(&msg, err_format, print_args) == -1) { |
| 233 | /* 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] | 234 | * still keep the information about the original error instead of LY_EMEM or other printf's error */ |
| 235 | msg = NULL; |
| 236 | } |
| 237 | |
| 238 | va_end(print_args); |
| 239 | } |
| 240 | e->msg = msg; |
| 241 | |
| 242 | if (!(*err)) { |
| 243 | *err = e; |
| 244 | } |
| 245 | |
| 246 | return e->no; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 247 | } |
| 248 | |
Michal Vasko | 88ccd58 | 2023-03-30 11:50:57 +0200 | [diff] [blame] | 249 | /** |
| 250 | * @brief Get error record from error hash table of a context for the current thread. |
| 251 | * |
| 252 | * @param[in] ctx Context to use. |
| 253 | * @return Thread error record, if any. |
| 254 | */ |
| 255 | static struct ly_ctx_err_rec * |
| 256 | ly_err_get_rec(const struct ly_ctx *ctx) |
| 257 | { |
Michal Vasko | 4cca009 | 2023-03-30 13:20:53 +0200 | [diff] [blame] | 258 | struct ly_ctx_err_rec rec, *match; |
Michal Vasko | 88ccd58 | 2023-03-30 11:50:57 +0200 | [diff] [blame] | 259 | |
| 260 | /* prepare record */ |
| 261 | rec.tid = pthread_self(); |
| 262 | |
| 263 | /* get the pointer to the matching record */ |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 264 | if (lyht_find(ctx->err_ht, &rec, lyht_hash((void *)&rec.tid, sizeof rec.tid), (void **)&match)) { |
Michal Vasko | 4cca009 | 2023-03-30 13:20:53 +0200 | [diff] [blame] | 265 | return NULL; |
| 266 | } |
Michal Vasko | 88ccd58 | 2023-03-30 11:50:57 +0200 | [diff] [blame] | 267 | |
| 268 | return match; |
| 269 | } |
| 270 | |
Michal Vasko | e0be745 | 2023-03-30 13:35:35 +0200 | [diff] [blame] | 271 | /** |
| 272 | * @brief Insert new error record to error hash table of a context for the current thread. |
| 273 | * |
| 274 | * @param[in] ctx Context to use. |
| 275 | * @return Thread error record. |
| 276 | */ |
| 277 | static struct ly_ctx_err_rec * |
| 278 | ly_err_new_rec(const struct ly_ctx *ctx) |
| 279 | { |
| 280 | struct ly_ctx_err_rec new, *rec; |
| 281 | LY_ERR r; |
| 282 | |
| 283 | /* insert a new record */ |
| 284 | new.err = NULL; |
| 285 | new.tid = pthread_self(); |
| 286 | |
| 287 | /* reuse lock */ |
| 288 | /* LOCK */ |
| 289 | pthread_mutex_lock((pthread_mutex_t *)&ctx->lyb_hash_lock); |
| 290 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 291 | r = lyht_insert(ctx->err_ht, &new, lyht_hash((void *)&new.tid, sizeof new.tid), (void **)&rec); |
Michal Vasko | e0be745 | 2023-03-30 13:35:35 +0200 | [diff] [blame] | 292 | |
| 293 | /* UNLOCK */ |
| 294 | pthread_mutex_unlock((pthread_mutex_t *)&ctx->lyb_hash_lock); |
| 295 | |
| 296 | return r ? NULL : rec; |
| 297 | } |
| 298 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 299 | LIBYANG_API_DEF struct ly_err_item * |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 300 | ly_err_first(const struct ly_ctx *ctx) |
| 301 | { |
Michal Vasko | 88ccd58 | 2023-03-30 11:50:57 +0200 | [diff] [blame] | 302 | struct ly_ctx_err_rec *rec; |
| 303 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 304 | LY_CHECK_ARG_RET(NULL, ctx, NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 305 | |
Michal Vasko | 88ccd58 | 2023-03-30 11:50:57 +0200 | [diff] [blame] | 306 | /* get the pointer to the matching record */ |
| 307 | rec = ly_err_get_rec(ctx); |
| 308 | |
| 309 | return rec ? rec->err : NULL; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 310 | } |
| 311 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 312 | LIBYANG_API_DEF struct ly_err_item * |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 313 | ly_err_last(const struct ly_ctx *ctx) |
| 314 | { |
Michal Vasko | 88ccd58 | 2023-03-30 11:50:57 +0200 | [diff] [blame] | 315 | struct ly_ctx_err_rec *rec; |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 316 | |
| 317 | LY_CHECK_ARG_RET(NULL, ctx, NULL); |
| 318 | |
Michal Vasko | 88ccd58 | 2023-03-30 11:50:57 +0200 | [diff] [blame] | 319 | /* get the pointer to the matching record */ |
| 320 | if (!(rec = ly_err_get_rec(ctx))) { |
| 321 | return NULL; |
| 322 | } |
| 323 | |
| 324 | return rec->err ? rec->err->prev : NULL; |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 325 | } |
| 326 | |
Michal Vasko | 9dbb91d | 2023-01-30 13:59:22 +0100 | [diff] [blame] | 327 | void |
| 328 | ly_err_move(struct ly_ctx *src_ctx, struct ly_ctx *trg_ctx) |
| 329 | { |
Michal Vasko | 88ccd58 | 2023-03-30 11:50:57 +0200 | [diff] [blame] | 330 | struct ly_ctx_err_rec *rec; |
| 331 | struct ly_err_item *err = NULL; |
Michal Vasko | 9dbb91d | 2023-01-30 13:59:22 +0100 | [diff] [blame] | 332 | |
Michal Vasko | 88ccd58 | 2023-03-30 11:50:57 +0200 | [diff] [blame] | 333 | /* get and remove the errors from src */ |
| 334 | rec = ly_err_get_rec(src_ctx); |
| 335 | if (rec) { |
| 336 | err = rec->err; |
| 337 | rec->err = NULL; |
| 338 | } |
Michal Vasko | 9dbb91d | 2023-01-30 13:59:22 +0100 | [diff] [blame] | 339 | |
| 340 | /* set them for trg */ |
Michal Vasko | e0be745 | 2023-03-30 13:35:35 +0200 | [diff] [blame] | 341 | if (!(rec = ly_err_get_rec(trg_ctx))) { |
| 342 | if (!(rec = ly_err_new_rec(trg_ctx))) { |
| 343 | LOGINT(NULL); |
| 344 | ly_err_free(err); |
| 345 | return; |
| 346 | } |
| 347 | } |
Michal Vasko | 88ccd58 | 2023-03-30 11:50:57 +0200 | [diff] [blame] | 348 | ly_err_free(rec->err); |
| 349 | rec->err = err; |
Michal Vasko | 9dbb91d | 2023-01-30 13:59:22 +0100 | [diff] [blame] | 350 | } |
| 351 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 352 | LIBYANG_API_DEF void |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 353 | ly_err_free(void *ptr) |
| 354 | { |
Michal Vasko | 88ccd58 | 2023-03-30 11:50:57 +0200 | [diff] [blame] | 355 | struct ly_err_item *e, *next; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 356 | |
| 357 | /* clean the error list */ |
Michal Vasko | 88ccd58 | 2023-03-30 11:50:57 +0200 | [diff] [blame] | 358 | LY_LIST_FOR_SAFE(ptr, next, e) { |
| 359 | free(e->msg); |
| 360 | free(e->path); |
| 361 | free(e->apptag); |
| 362 | free(e); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 366 | LIBYANG_API_DEF void |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 367 | ly_err_clean(struct ly_ctx *ctx, struct ly_err_item *eitem) |
| 368 | { |
Michal Vasko | 88ccd58 | 2023-03-30 11:50:57 +0200 | [diff] [blame] | 369 | struct ly_ctx_err_rec *rec; |
| 370 | struct ly_err_item *e; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 371 | |
Michal Vasko | 88ccd58 | 2023-03-30 11:50:57 +0200 | [diff] [blame] | 372 | if (!(rec = ly_err_get_rec(ctx))) { |
| 373 | return; |
| 374 | } |
| 375 | if (rec->err == eitem) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 376 | eitem = NULL; |
| 377 | } |
Michal Vasko | 88ccd58 | 2023-03-30 11:50:57 +0200 | [diff] [blame] | 378 | |
| 379 | if (!eitem) { |
| 380 | /* free all err */ |
| 381 | ly_err_free(rec->err); |
| 382 | rec->err = NULL; |
| 383 | } else { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 384 | /* disconnect the error */ |
Michal Vasko | 88ccd58 | 2023-03-30 11:50:57 +0200 | [diff] [blame] | 385 | for (e = rec->err; e && (e->next != eitem); e = e->next) {} |
| 386 | assert(e); |
| 387 | e->next = NULL; |
| 388 | rec->err->prev = e; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 389 | /* free this err and newer */ |
| 390 | ly_err_free(eitem); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 394 | LIBYANG_API_DEF LY_LOG_LEVEL |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 395 | ly_log_level(LY_LOG_LEVEL level) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 396 | { |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 397 | LY_LOG_LEVEL prev = ATOMIC_LOAD_RELAXED(ly_ll); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 398 | |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 399 | ATOMIC_STORE_RELAXED(ly_ll, level); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 400 | return prev; |
| 401 | } |
| 402 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 403 | LIBYANG_API_DEF uint32_t |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 404 | ly_log_options(uint32_t opts) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 405 | { |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 406 | uint32_t prev = ATOMIC_LOAD_RELAXED(ly_log_opts); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 407 | |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 408 | ATOMIC_STORE_RELAXED(ly_log_opts, opts); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 409 | return prev; |
| 410 | } |
| 411 | |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 412 | LIBYANG_API_DEF void |
| 413 | ly_temp_log_options(uint32_t *opts) |
| 414 | { |
| 415 | temp_ly_log_opts = opts; |
| 416 | } |
| 417 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 418 | LIBYANG_API_DEF uint32_t |
Radek Krejci | 68433c9 | 2020-10-12 17:03:55 +0200 | [diff] [blame] | 419 | ly_log_dbg_groups(uint32_t dbg_groups) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 420 | { |
| 421 | #ifndef NDEBUG |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 422 | uint32_t prev = ATOMIC_LOAD_RELAXED(ly_ldbg_groups); |
Radek Krejci | ebdaed0 | 2020-11-09 13:05:06 +0100 | [diff] [blame] | 423 | |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 424 | ATOMIC_STORE_RELAXED(ly_ldbg_groups, dbg_groups); |
Radek Krejci | ebdaed0 | 2020-11-09 13:05:06 +0100 | [diff] [blame] | 425 | return prev; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 426 | #else |
| 427 | (void)dbg_groups; |
Radek Krejci | ebdaed0 | 2020-11-09 13:05:06 +0100 | [diff] [blame] | 428 | return 0; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 429 | #endif |
| 430 | } |
| 431 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 432 | LIBYANG_API_DEF void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 433 | ly_set_log_clb(ly_log_clb clb, ly_bool path) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 434 | { |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 435 | log_clb = clb; |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 436 | ATOMIC_STORE_RELAXED(path_flag, path); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 437 | } |
| 438 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 439 | LIBYANG_API_DEF ly_log_clb |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 440 | ly_get_log_clb(void) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 441 | { |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 442 | return log_clb; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 443 | } |
| 444 | |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 445 | void |
Michal Vasko | 59e69e7 | 2022-02-18 09:18:21 +0100 | [diff] [blame] | 446 | 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] | 447 | uint64_t line) |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 448 | { |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 449 | if (scnode) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 450 | ly_set_add(&log_location.scnodes, (void *)scnode, 1, NULL); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 451 | } |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 452 | if (dnode) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 453 | ly_set_add(&log_location.dnodes, (void *)dnode, 1, NULL); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 454 | } |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 455 | if (path) { |
| 456 | char *s = strdup(path); |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 457 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 458 | LY_CHECK_ERR_RET(!s, LOGMEM(NULL), ); |
| 459 | ly_set_add(&log_location.paths, s, 1, NULL); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 460 | } |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 461 | if (in) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 462 | ly_set_add(&log_location.inputs, (void *)in, 1, NULL); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 463 | } |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 464 | if (line) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 465 | log_location.line = line; |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
| 469 | void |
Michal Vasko | 59e69e7 | 2022-02-18 09:18:21 +0100 | [diff] [blame] | 470 | 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] | 471 | { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 472 | for (uint32_t i = scnode_steps; i && log_location.scnodes.count; i--) { |
| 473 | log_location.scnodes.count--; |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 474 | } |
| 475 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 476 | for (uint32_t i = dnode_steps; i && log_location.dnodes.count; i--) { |
| 477 | log_location.dnodes.count--; |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 478 | } |
| 479 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 480 | for (uint32_t i = path_steps; i && log_location.paths.count; i--) { |
| 481 | 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] | 482 | } |
| 483 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 484 | for (uint32_t i = in_steps; i && log_location.inputs.count; i--) { |
| 485 | log_location.inputs.count--; |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 486 | } |
| 487 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 488 | /* deallocate the empty sets */ |
| 489 | if (scnode_steps && !log_location.scnodes.count) { |
| 490 | ly_set_erase(&log_location.scnodes, NULL); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 491 | } |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 492 | if (dnode_steps && !log_location.dnodes.count) { |
| 493 | ly_set_erase(&log_location.dnodes, NULL); |
| 494 | } |
| 495 | if (path_steps && !log_location.paths.count) { |
| 496 | ly_set_erase(&log_location.paths, free); |
| 497 | } |
| 498 | if (in_steps && !log_location.inputs.count) { |
| 499 | ly_set_erase(&log_location.inputs, NULL); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
Michal Vasko | 88ccd58 | 2023-03-30 11:50:57 +0200 | [diff] [blame] | 503 | /** |
| 504 | * @brief Store generated error in a context. |
| 505 | * |
| 506 | * @param[in] ctx Context to use. |
| 507 | * @param[in] level Message log level. |
| 508 | * @param[in] no Error number. |
| 509 | * @param[in] vecode Error validation error code. |
| 510 | * @param[in] msg Error message, always spent. |
| 511 | * @param[in] path Error path, always spent. |
| 512 | * @param[in] apptag Error app tag, always spent. |
| 513 | * @return LY_ERR value. |
| 514 | */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 515 | static LY_ERR |
| 516 | log_store(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, LY_VECODE vecode, char *msg, char *path, char *apptag) |
| 517 | { |
Michal Vasko | e0be745 | 2023-03-30 13:35:35 +0200 | [diff] [blame] | 518 | struct ly_ctx_err_rec *rec; |
Michal Vasko | 88ccd58 | 2023-03-30 11:50:57 +0200 | [diff] [blame] | 519 | struct ly_err_item *e, *last; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 520 | |
| 521 | assert(ctx && (level < LY_LLVRB)); |
| 522 | |
Michal Vasko | 88ccd58 | 2023-03-30 11:50:57 +0200 | [diff] [blame] | 523 | if (!(rec = ly_err_get_rec(ctx))) { |
Michal Vasko | e0be745 | 2023-03-30 13:35:35 +0200 | [diff] [blame] | 524 | if (!(rec = ly_err_new_rec(ctx))) { |
| 525 | goto mem_fail; |
Michal Vasko | 88ccd58 | 2023-03-30 11:50:57 +0200 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
| 529 | e = rec->err; |
| 530 | if (!e) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 531 | /* if we are only to fill in path, there must have been an error stored */ |
| 532 | assert(msg); |
Michal Vasko | 88ccd58 | 2023-03-30 11:50:57 +0200 | [diff] [blame] | 533 | e = malloc(sizeof *e); |
| 534 | LY_CHECK_GOTO(!e, mem_fail); |
| 535 | e->prev = e; |
| 536 | e->next = NULL; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 537 | |
Michal Vasko | 88ccd58 | 2023-03-30 11:50:57 +0200 | [diff] [blame] | 538 | rec->err = e; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 539 | } else if (!msg) { |
| 540 | /* only filling the path */ |
| 541 | assert(path); |
| 542 | |
| 543 | /* find last error */ |
Michal Vasko | 88ccd58 | 2023-03-30 11:50:57 +0200 | [diff] [blame] | 544 | e = e->prev; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 545 | do { |
Michal Vasko | 88ccd58 | 2023-03-30 11:50:57 +0200 | [diff] [blame] | 546 | if (e->level == LY_LLERR) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 547 | /* fill the path */ |
Michal Vasko | 88ccd58 | 2023-03-30 11:50:57 +0200 | [diff] [blame] | 548 | free(e->path); |
| 549 | e->path = path; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 550 | return LY_SUCCESS; |
| 551 | } |
Michal Vasko | 88ccd58 | 2023-03-30 11:50:57 +0200 | [diff] [blame] | 552 | e = e->prev; |
| 553 | } while (e->prev->next); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 554 | /* last error was not found */ |
| 555 | assert(0); |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 556 | } else if ((temp_ly_log_opts && ((*temp_ly_log_opts & LY_LOSTORE_LAST) == LY_LOSTORE_LAST)) || |
| 557 | (!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] | 558 | /* overwrite last message */ |
Michal Vasko | 88ccd58 | 2023-03-30 11:50:57 +0200 | [diff] [blame] | 559 | free(e->msg); |
| 560 | free(e->path); |
| 561 | free(e->apptag); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 562 | } else { |
| 563 | /* store new message */ |
Michal Vasko | 88ccd58 | 2023-03-30 11:50:57 +0200 | [diff] [blame] | 564 | last = e->prev; |
| 565 | e->prev = malloc(sizeof *e); |
| 566 | LY_CHECK_GOTO(!e->prev, mem_fail); |
| 567 | e = e->prev; |
| 568 | e->prev = last; |
| 569 | e->next = NULL; |
| 570 | last->next = e; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | /* fill in the information */ |
Michal Vasko | 88ccd58 | 2023-03-30 11:50:57 +0200 | [diff] [blame] | 574 | e->level = level; |
| 575 | e->no = no; |
| 576 | e->vecode = vecode; |
| 577 | e->msg = msg; |
| 578 | e->path = path; |
| 579 | e->apptag = apptag; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 580 | return LY_SUCCESS; |
| 581 | |
| 582 | mem_fail: |
| 583 | LOGMEM(NULL); |
| 584 | free(msg); |
| 585 | free(path); |
| 586 | free(apptag); |
| 587 | return LY_EMEM; |
| 588 | } |
| 589 | |
| 590 | static void |
Michal Vasko | e9391c7 | 2021-10-05 10:04:56 +0200 | [diff] [blame] | 591 | 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] | 592 | const char *format, va_list args) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 593 | { |
| 594 | char *msg = NULL; |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 595 | ly_bool free_strs, lolog, lostore; |
| 596 | |
| 597 | /* learn effective logger options */ |
| 598 | if (temp_ly_log_opts) { |
| 599 | lolog = *temp_ly_log_opts & LY_LOLOG; |
| 600 | lostore = *temp_ly_log_opts & LY_LOSTORE; |
| 601 | } else { |
| 602 | lolog = ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOLOG; |
| 603 | lostore = ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOSTORE; |
| 604 | } |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 605 | |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 606 | if (level > ATOMIC_LOAD_RELAXED(ly_ll)) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 607 | /* do not print or store the message */ |
| 608 | free(path); |
| 609 | return; |
| 610 | } |
| 611 | |
Michal Vasko | 5a01692 | 2021-05-07 08:19:15 +0200 | [diff] [blame] | 612 | if (no == LY_EMEM) { |
| 613 | /* just print it, anything else would most likely fail anyway */ |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 614 | if (lolog) { |
Michal Vasko | 5a01692 | 2021-05-07 08:19:15 +0200 | [diff] [blame] | 615 | if (log_clb) { |
| 616 | log_clb(level, LY_EMEM_MSG, path); |
| 617 | } else { |
| 618 | fprintf(stderr, "libyang[%d]: ", level); |
| 619 | vfprintf(stderr, format, args); |
| 620 | if (path) { |
| 621 | fprintf(stderr, " (path: %s)\n", path); |
| 622 | } else { |
| 623 | fprintf(stderr, "\n"); |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | free(path); |
| 628 | return; |
| 629 | } |
| 630 | |
Michal Vasko | 236cbac | 2023-02-10 15:45:37 +0100 | [diff] [blame] | 631 | /* print into a single message */ |
| 632 | if (vasprintf(&msg, format, args) == -1) { |
| 633 | LOGMEM(ctx); |
| 634 | free(path); |
| 635 | return; |
| 636 | } |
| 637 | |
| 638 | /* store as the last message */ |
| 639 | strncpy(last_msg, msg, LY_LAST_MSG_SIZE - 1); |
| 640 | |
| 641 | /* store the error/warning in the context (if we need to store errors internally, it does not matter what are |
| 642 | * the user log options) */ |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 643 | if ((level < LY_LLVRB) && ctx && lostore) { |
Radek Krejci | c9e64a6 | 2020-09-18 20:08:12 +0200 | [diff] [blame] | 644 | if (((no & ~LY_EPLUGIN) == LY_EVALID) && (vecode == LYVE_SUCCESS)) { |
| 645 | /* assume we are inheriting the error, so inherit vecode as well */ |
| 646 | vecode = ly_vecode(ctx); |
| 647 | } |
Michal Vasko | e9391c7 | 2021-10-05 10:04:56 +0200 | [diff] [blame] | 648 | 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] | 649 | return; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 650 | } |
| 651 | free_strs = 0; |
| 652 | } else { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 653 | free_strs = 1; |
| 654 | } |
| 655 | |
| 656 | /* if we are only storing errors internally, never print the message (yet) */ |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 657 | if (lolog) { |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 658 | if (log_clb) { |
| 659 | log_clb(level, msg, path); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 660 | } else { |
| 661 | fprintf(stderr, "libyang[%d]: %s%s", level, msg, path ? " " : "\n"); |
| 662 | if (path) { |
| 663 | fprintf(stderr, "(path: %s)\n", path); |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | if (free_strs) { |
| 669 | free(path); |
| 670 | free(msg); |
| 671 | } |
| 672 | } |
| 673 | |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 674 | #ifndef NDEBUG |
| 675 | |
| 676 | void |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 677 | ly_log_dbg(uint32_t group, const char *format, ...) |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 678 | { |
| 679 | char *dbg_format; |
| 680 | const char *str_group; |
| 681 | va_list ap; |
| 682 | |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 683 | if (!(ATOMIC_LOAD_RELAXED(ly_ldbg_groups) & group)) { |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 684 | return; |
| 685 | } |
| 686 | |
| 687 | switch (group) { |
| 688 | case LY_LDGDICT: |
| 689 | str_group = "DICT"; |
| 690 | break; |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 691 | case LY_LDGXPATH: |
| 692 | str_group = "XPATH"; |
| 693 | break; |
Michal Vasko | e558f79 | 2021-07-28 08:20:15 +0200 | [diff] [blame] | 694 | case LY_LDGDEPSETS: |
| 695 | str_group = "DEPSETS"; |
| 696 | break; |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 697 | default: |
| 698 | LOGINT(NULL); |
| 699 | return; |
| 700 | } |
| 701 | |
| 702 | if (asprintf(&dbg_format, "%s: %s", str_group, format) == -1) { |
| 703 | LOGMEM(NULL); |
| 704 | return; |
| 705 | } |
| 706 | |
| 707 | va_start(ap, format); |
Michal Vasko | e9391c7 | 2021-10-05 10:04:56 +0200 | [diff] [blame] | 708 | log_vprintf(NULL, LY_LLDBG, 0, 0, NULL, NULL, dbg_format, ap); |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 709 | va_end(ap); |
aPiecek | d6d2c04 | 2023-06-02 08:31:43 +0200 | [diff] [blame] | 710 | |
| 711 | free(dbg_format); |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | #endif |
| 715 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 716 | void |
| 717 | ly_log(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, const char *format, ...) |
| 718 | { |
| 719 | va_list ap; |
| 720 | |
| 721 | va_start(ap, format); |
Michal Vasko | e9391c7 | 2021-10-05 10:04:56 +0200 | [diff] [blame] | 722 | log_vprintf(ctx, level, no, 0, NULL, NULL, format, ap); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 723 | va_end(ap); |
| 724 | } |
| 725 | |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 726 | /** |
| 727 | * @brief Append a schema node name to a generated data path, only if it fits. |
| 728 | * |
| 729 | * @param[in,out] str Generated path to update. |
| 730 | * @param[in] snode Schema node to append. |
| 731 | * @param[in] parent Last printed data node. |
| 732 | * @return LY_ERR value. |
| 733 | */ |
| 734 | static LY_ERR |
| 735 | ly_vlog_build_path_append(char **str, const struct lysc_node *snode, const struct lyd_node *parent) |
| 736 | { |
| 737 | const struct lys_module *mod, *prev_mod; |
| 738 | uint32_t len, new_len; |
| 739 | void *mem; |
| 740 | |
| 741 | if (snode->nodetype & (LYS_CHOICE | LYS_CASE)) { |
| 742 | /* schema-only node */ |
| 743 | return LY_SUCCESS; |
| 744 | } else if (lysc_data_parent(snode) != parent->schema) { |
| 745 | /* not a direct descendant node */ |
| 746 | return LY_SUCCESS; |
| 747 | } |
| 748 | |
| 749 | /* get module to print, if any */ |
| 750 | mod = snode->module; |
| 751 | prev_mod = (parent->schema) ? parent->schema->module : lyd_owner_module(parent); |
| 752 | if (prev_mod == mod) { |
| 753 | mod = NULL; |
| 754 | } |
| 755 | |
| 756 | /* realloc string */ |
| 757 | len = strlen(*str); |
| 758 | new_len = len + 1 + (mod ? strlen(mod->name) + 1 : 0) + strlen(snode->name); |
| 759 | mem = realloc(*str, new_len + 1); |
| 760 | LY_CHECK_ERR_RET(!mem, LOGMEM(LYD_CTX(parent)), LY_EMEM); |
| 761 | *str = mem; |
| 762 | |
| 763 | /* print the last schema node */ |
| 764 | sprintf(*str + len, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", snode->name); |
| 765 | return LY_SUCCESS; |
| 766 | } |
| 767 | |
| 768 | /** |
| 769 | * @brief Build log path from the stored log location information. |
| 770 | * |
| 771 | * @param[in] ctx Context to use. |
| 772 | * @param[out] path Generated log path. |
| 773 | * @return LY_ERR value. |
| 774 | */ |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 775 | static LY_ERR |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 776 | ly_vlog_build_path(const struct ly_ctx *ctx, char **path) |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 777 | { |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 778 | int r; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 779 | char *str = NULL, *prev = NULL; |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 780 | const struct lyd_node *dnode; |
Radek Krejci | cb3e647 | 2021-01-06 08:19:01 +0100 | [diff] [blame] | 781 | |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 782 | *path = NULL; |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 783 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 784 | 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] | 785 | /* simply get what is in the provided path string */ |
Michal Vasko | 6727c68 | 2023-02-17 10:40:26 +0100 | [diff] [blame] | 786 | r = asprintf(path, "Path \"%s\".", (const char *)log_location.paths.objs[log_location.paths.count - 1]); |
| 787 | LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 788 | } else { |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 789 | /* data/schema node */ |
| 790 | if (log_location.dnodes.count) { |
| 791 | dnode = log_location.dnodes.objs[log_location.dnodes.count - 1]; |
Michal Vasko | a4dfb3c | 2022-10-25 14:59:31 +0200 | [diff] [blame] | 792 | if (dnode->parent || !lysc_data_parent(dnode->schema)) { |
| 793 | /* data node with all of its parents */ |
| 794 | str = lyd_path(log_location.dnodes.objs[log_location.dnodes.count - 1], LYD_PATH_STD, NULL, 0); |
| 795 | LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM); |
| 796 | } else { |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 797 | /* data parsers put all the parent nodes in the set, but they are not connected */ |
| 798 | str = lyd_path_set(&log_location.dnodes, LYD_PATH_STD); |
Michal Vasko | 3e65ee3 | 2022-10-21 10:09:51 +0200 | [diff] [blame] | 799 | LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM); |
Michal Vasko | a4dfb3c | 2022-10-25 14:59:31 +0200 | [diff] [blame] | 800 | } |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 801 | |
Michal Vasko | a4dfb3c | 2022-10-25 14:59:31 +0200 | [diff] [blame] | 802 | /* sometimes the last node is not created yet and we only have the schema node */ |
| 803 | if (log_location.scnodes.count) { |
| 804 | 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] | 805 | } |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 806 | |
| 807 | r = asprintf(path, "Data location \"%s\"", str); |
| 808 | free(str); |
| 809 | LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM); |
| 810 | } else if (log_location.scnodes.count) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 811 | 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] | 812 | LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM); |
| 813 | |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 814 | r = asprintf(path, "Schema location \"%s\"", str); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 815 | free(str); |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 816 | LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 817 | } |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 818 | |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 819 | /* line */ |
| 820 | prev = *path; |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 821 | if (log_location.line) { |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 822 | 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] | 823 | free(prev); |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 824 | LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 825 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 826 | log_location.line = 0; |
| 827 | } else if (log_location.inputs.count) { |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 828 | r = asprintf(path, "%s%sine number %" PRIu64, prev ? prev : "", prev ? ", l" : "L", |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 829 | ((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] | 830 | free(prev); |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 831 | LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | if (*path) { |
| 835 | prev = *path; |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 836 | r = asprintf(path, "%s.", prev); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 837 | free(prev); |
Michal Vasko | dbf3e65 | 2022-10-21 08:46:25 +0200 | [diff] [blame] | 838 | LY_CHECK_ERR_RET(r == -1, LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 839 | } |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 840 | } |
| 841 | |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 842 | return LY_SUCCESS; |
| 843 | } |
| 844 | |
| 845 | void |
Michal Vasko | e9391c7 | 2021-10-05 10:04:56 +0200 | [diff] [blame] | 846 | 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] | 847 | { |
| 848 | va_list ap; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 849 | char *path = NULL; |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 850 | |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 851 | if (ATOMIC_LOAD_RELAXED(path_flag) && ctx) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 852 | ly_vlog_build_path(ctx, &path); |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | va_start(ap, format); |
Michal Vasko | e9391c7 | 2021-10-05 10:04:56 +0200 | [diff] [blame] | 856 | log_vprintf(ctx, LY_LLERR, LY_EVALID, code, path, apptag, format, ap); |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 857 | /* path is spent and should not be freed! */ |
| 858 | va_end(ap); |
| 859 | } |
| 860 | |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 861 | /** |
| 862 | * @brief Print a log message from an extension plugin callback. |
| 863 | * |
| 864 | * @param[in] ctx libyang context to store the error record. If not provided, the error is just printed. |
| 865 | * @param[in] plugin_name Name of the plugin generating the message. |
| 866 | * @param[in] level Log message level (error, warning, etc.) |
| 867 | * @param[in] err_no Error type code. |
Michal Vasko | 6727c68 | 2023-02-17 10:40:26 +0100 | [diff] [blame] | 868 | * @param[in] path Optional path of the error, used if set. |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 869 | * @param[in] format Format string to print. |
| 870 | * @param[in] ap Var arg list for @p format. |
| 871 | */ |
| 872 | static void |
Michal Vasko | 6727c68 | 2023-02-17 10:40:26 +0100 | [diff] [blame] | 873 | ly_ext_log(const struct ly_ctx *ctx, const char *plugin_name, LY_LOG_LEVEL level, LY_ERR err_no, char *path, |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 874 | const char *format, va_list ap) |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 875 | { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 876 | char *plugin_msg; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 877 | |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 878 | if (ATOMIC_LOAD_RELAXED(ly_ll) < level) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 879 | return; |
| 880 | } |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 881 | if (asprintf(&plugin_msg, "Ext plugin \"%s\": %s", plugin_name, format) == -1) { |
| 882 | LOGMEM(ctx); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 883 | return; |
| 884 | } |
| 885 | |
Michal Vasko | 6727c68 | 2023-02-17 10:40:26 +0100 | [diff] [blame] | 886 | log_vprintf(ctx, level, (level == LY_LLERR ? LY_EPLUGIN : 0) | err_no, LYVE_OTHER, path, NULL, plugin_msg, ap); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 887 | free(plugin_msg); |
| 888 | } |
| 889 | |
| 890 | LIBYANG_API_DEF void |
| 891 | lyplg_ext_parse_log(const struct lysp_ctx *pctx, const struct lysp_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no, |
| 892 | const char *format, ...) |
| 893 | { |
| 894 | va_list ap; |
| 895 | char *path = NULL; |
| 896 | |
| 897 | if (ATOMIC_LOAD_RELAXED(path_flag)) { |
| 898 | ly_vlog_build_path(PARSER_CTX(pctx), &path); |
| 899 | } |
| 900 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 901 | va_start(ap, format); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 902 | 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] | 903 | va_end(ap); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | LIBYANG_API_DEF void |
| 907 | lyplg_ext_compile_log(const struct lysc_ctx *cctx, const struct lysc_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no, |
| 908 | const char *format, ...) |
| 909 | { |
| 910 | va_list ap; |
Michal Vasko | 6727c68 | 2023-02-17 10:40:26 +0100 | [diff] [blame] | 911 | char *path = NULL; |
| 912 | |
| 913 | if (cctx && (asprintf(&path, "Path \"%s\".", cctx->path) == -1)) { |
| 914 | LOGMEM(cctx->ctx); |
| 915 | return; |
| 916 | } |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 917 | |
| 918 | va_start(ap, format); |
Michal Vasko | 6727c68 | 2023-02-17 10:40:26 +0100 | [diff] [blame] | 919 | ly_ext_log(ext->module->ctx, ext->def->plugin->id, level, err_no, path, format, ap); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 920 | va_end(ap); |
| 921 | } |
| 922 | |
| 923 | LIBYANG_API_DEF void |
| 924 | lyplg_ext_compile_log_path(const char *path, const struct lysc_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no, |
| 925 | const char *format, ...) |
| 926 | { |
| 927 | va_list ap; |
Michal Vasko | 6727c68 | 2023-02-17 10:40:26 +0100 | [diff] [blame] | 928 | char *log_path = NULL; |
| 929 | |
| 930 | if (path && (asprintf(&log_path, "Path \"%s\".", path) == -1)) { |
| 931 | LOGMEM(ext->module->ctx); |
| 932 | return; |
| 933 | } |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 934 | |
| 935 | va_start(ap, format); |
Michal Vasko | 6727c68 | 2023-02-17 10:40:26 +0100 | [diff] [blame] | 936 | ly_ext_log(ext->module->ctx, ext->def->plugin->id, level, err_no, log_path, format, ap); |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 937 | va_end(ap); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 938 | } |
| 939 | |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 940 | /** |
Michal Vasko | 6727c68 | 2023-02-17 10:40:26 +0100 | [diff] [blame] | 941 | * @brief Serves only for creating ap. |
| 942 | */ |
| 943 | static void |
| 944 | _lyplg_ext_compile_log_err(const struct ly_err_item *err, const struct lysc_ext_instance *ext, ...) |
| 945 | { |
| 946 | va_list ap; |
| 947 | |
| 948 | va_start(ap, ext); |
| 949 | ly_ext_log(ext->module->ctx, ext->def->plugin->id, err->level, err->no, err->path ? strdup(err->path) : NULL, "%s", ap); |
| 950 | va_end(ap); |
| 951 | } |
| 952 | |
| 953 | LIBYANG_API_DEF void |
| 954 | lyplg_ext_compile_log_err(const struct ly_err_item *err, const struct lysc_ext_instance *ext) |
| 955 | { |
| 956 | _lyplg_ext_compile_log_err(err, ext, err->msg); |
| 957 | } |
| 958 | |
| 959 | /** |
Michal Vasko | c78a609 | 2021-05-07 15:27:35 +0200 | [diff] [blame] | 960 | * @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] | 961 | */ |
| 962 | static void |
Michal Vasko | c78a609 | 2021-05-07 15:27:35 +0200 | [diff] [blame] | 963 | _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] | 964 | { |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 965 | va_list ap; |
| 966 | char *path_dup = NULL; |
| 967 | |
| 968 | LY_CHECK_ARG_RET(ctx, eitem, ); |
| 969 | |
| 970 | if (eitem->path) { |
| 971 | /* duplicate path because it will be freed */ |
| 972 | path_dup = strdup(eitem->path); |
| 973 | LY_CHECK_ERR_RET(!path_dup, LOGMEM(ctx), ); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 974 | } |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 975 | |
Michal Vasko | c78a609 | 2021-05-07 15:27:35 +0200 | [diff] [blame] | 976 | va_start(ap, format); |
Michal Vasko | e9391c7 | 2021-10-05 10:04:56 +0200 | [diff] [blame] | 977 | 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] | 978 | va_end(ap); |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 979 | } |
| 980 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 981 | LIBYANG_API_DEF void |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 982 | ly_err_print(const struct ly_ctx *ctx, struct ly_err_item *eitem) |
| 983 | { |
Michal Vasko | c78a609 | 2021-05-07 15:27:35 +0200 | [diff] [blame] | 984 | /* String ::ly_err_item.msg cannot be used directly because it may contain the % character */ |
| 985 | _ly_err_print(ctx, eitem, "%s", eitem->msg); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 986 | } |