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