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