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