Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file log.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Logger routines implementations |
| 5 | * |
| 6 | * Copyright (c) 2015 - 2018 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
Christian Hopps | 32874e1 | 2021-05-01 09:43:54 -0400 | [diff] [blame] | 15 | #define _GNU_SOURCE /* asprintf, strdup */ |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 16 | |
| 17 | #include "log.h" |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 18 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 19 | #include <assert.h> |
Radek Krejci | c04f0a2 | 2018-09-21 15:49:45 +0200 | [diff] [blame] | 20 | #include <inttypes.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 21 | #include <pthread.h> |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 22 | #include <stdarg.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 23 | #include <stdint.h> |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 24 | #include <stdio.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 27 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 28 | #include "common.h" |
Radek Krejci | aa45bda | 2020-07-20 07:43:38 +0200 | [diff] [blame] | 29 | #include "compat.h" |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 30 | #include "in_internal.h" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 31 | #include "plugins_exts.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 32 | #include "set.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 33 | #include "tree_data.h" |
| 34 | #include "tree_schema.h" |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 35 | |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 36 | ATOMIC_T ly_ll = (uint_fast32_t)LY_LLWRN; |
| 37 | ATOMIC_T ly_log_opts = (uint_fast32_t)(LY_LOLOG | LY_LOSTORE_LAST); |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 38 | static ly_log_clb log_clb; |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 39 | static ATOMIC_T path_flag = 1; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 40 | #ifndef NDEBUG |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 41 | ATOMIC_T ly_ldbg_groups = 0; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 42 | #endif |
| 43 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 44 | THREAD_LOCAL struct ly_log_location_s log_location = {0}; |
| 45 | |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 46 | /* how many bytes add when enlarging buffers */ |
| 47 | #define LY_BUF_STEP 128 |
| 48 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 49 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 50 | ly_errcode(const struct ly_ctx *ctx) |
| 51 | { |
| 52 | struct ly_err_item *i; |
| 53 | |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 54 | i = ly_err_last(ctx); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 55 | if (i) { |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 56 | return i->no; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | return LY_SUCCESS; |
| 60 | } |
| 61 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 62 | LIBYANG_API_DEF LY_VECODE |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 63 | ly_vecode(const struct ly_ctx *ctx) |
| 64 | { |
| 65 | struct ly_err_item *i; |
| 66 | |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 67 | i = ly_err_last(ctx); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 68 | if (i) { |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 69 | return i->vecode; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | return LYVE_SUCCESS; |
| 73 | } |
| 74 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 75 | LIBYANG_API_DEF const char * |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 76 | ly_errmsg(const struct ly_ctx *ctx) |
| 77 | { |
| 78 | struct ly_err_item *i; |
| 79 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 80 | LY_CHECK_ARG_RET(NULL, ctx, NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 81 | |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 82 | i = ly_err_last(ctx); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 83 | if (i) { |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 84 | return i->msg; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | return NULL; |
| 88 | } |
| 89 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 90 | LIBYANG_API_DEF const char * |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 91 | ly_errpath(const struct ly_ctx *ctx) |
| 92 | { |
| 93 | struct ly_err_item *i; |
| 94 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 95 | LY_CHECK_ARG_RET(NULL, ctx, NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 96 | |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 97 | i = ly_err_last(ctx); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 98 | if (i) { |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 99 | return i->path; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | return NULL; |
| 103 | } |
| 104 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 105 | LIBYANG_API_DEF const char * |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 106 | ly_errapptag(const struct ly_ctx *ctx) |
| 107 | { |
| 108 | struct ly_err_item *i; |
| 109 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 110 | LY_CHECK_ARG_RET(NULL, ctx, NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 111 | |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 112 | i = ly_err_last(ctx); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 113 | if (i) { |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 114 | return i->apptag; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | return NULL; |
| 118 | } |
| 119 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 120 | LIBYANG_API_DEF LY_ERR |
aPiecek | 6d61855 | 2021-06-18 10:02:59 +0200 | [diff] [blame] | 121 | 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] | 122 | { |
Radek Krejci | db0ee02 | 2021-03-15 16:53:44 +0100 | [diff] [blame] | 123 | char *msg = NULL; |
| 124 | struct ly_err_item *e; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 125 | |
Radek Krejci | d43298b | 2021-03-25 16:17:15 +0100 | [diff] [blame] | 126 | if (!err || (ecode == LY_SUCCESS)) { |
Radek Krejci | db0ee02 | 2021-03-15 16:53:44 +0100 | [diff] [blame] | 127 | /* nothing to do */ |
| 128 | return ecode; |
| 129 | } |
| 130 | |
| 131 | e = malloc(sizeof *e); |
| 132 | LY_CHECK_ERR_RET(!e, LOGMEM(NULL), LY_EMEM); |
| 133 | e->prev = (*err) ? (*err)->prev : e; |
| 134 | e->next = NULL; |
| 135 | if (*err) { |
| 136 | (*err)->prev->next = e; |
| 137 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 138 | |
| 139 | /* fill in the information */ |
Radek Krejci | db0ee02 | 2021-03-15 16:53:44 +0100 | [diff] [blame] | 140 | e->level = LY_LLERR; |
| 141 | e->no = ecode; |
| 142 | e->vecode = vecode; |
| 143 | e->path = path; |
| 144 | e->apptag = apptag; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 145 | |
aPiecek | 6d61855 | 2021-06-18 10:02:59 +0200 | [diff] [blame] | 146 | if (err_format) { |
Radek Krejci | db0ee02 | 2021-03-15 16:53:44 +0100 | [diff] [blame] | 147 | va_list print_args; |
| 148 | |
aPiecek | 6d61855 | 2021-06-18 10:02:59 +0200 | [diff] [blame] | 149 | va_start(print_args, err_format); |
Radek Krejci | db0ee02 | 2021-03-15 16:53:44 +0100 | [diff] [blame] | 150 | |
aPiecek | 6d61855 | 2021-06-18 10:02:59 +0200 | [diff] [blame] | 151 | if (vasprintf(&msg, err_format, print_args) == -1) { |
| 152 | /* 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] | 153 | * still keep the information about the original error instead of LY_EMEM or other printf's error */ |
| 154 | msg = NULL; |
| 155 | } |
| 156 | |
| 157 | va_end(print_args); |
| 158 | } |
| 159 | e->msg = msg; |
| 160 | |
| 161 | if (!(*err)) { |
| 162 | *err = e; |
| 163 | } |
| 164 | |
| 165 | return e->no; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 166 | } |
| 167 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 168 | LIBYANG_API_DEF struct ly_err_item * |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 169 | ly_err_first(const struct ly_ctx *ctx) |
| 170 | { |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 171 | LY_CHECK_ARG_RET(NULL, ctx, NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 172 | |
| 173 | return pthread_getspecific(ctx->errlist_key); |
| 174 | } |
| 175 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 176 | LIBYANG_API_DEF struct ly_err_item * |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 177 | ly_err_last(const struct ly_ctx *ctx) |
| 178 | { |
| 179 | const struct ly_err_item *e; |
| 180 | |
| 181 | LY_CHECK_ARG_RET(NULL, ctx, NULL); |
| 182 | |
| 183 | e = pthread_getspecific(ctx->errlist_key); |
| 184 | return e ? e->prev : NULL; |
| 185 | } |
| 186 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 187 | LIBYANG_API_DEF void |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 188 | ly_err_free(void *ptr) |
| 189 | { |
| 190 | struct ly_err_item *i, *next; |
| 191 | |
| 192 | /* clean the error list */ |
| 193 | for (i = (struct ly_err_item *)ptr; i; i = next) { |
| 194 | next = i->next; |
Radek Krejci | db0ee02 | 2021-03-15 16:53:44 +0100 | [diff] [blame] | 195 | free(i->msg); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 196 | free(i->path); |
| 197 | free(i->apptag); |
| 198 | free(i); |
| 199 | } |
| 200 | } |
| 201 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 202 | LIBYANG_API_DEF void |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 203 | ly_err_clean(struct ly_ctx *ctx, struct ly_err_item *eitem) |
| 204 | { |
| 205 | struct ly_err_item *i, *first; |
| 206 | |
| 207 | first = ly_err_first(ctx); |
| 208 | if (first == eitem) { |
| 209 | eitem = NULL; |
| 210 | } |
| 211 | if (eitem) { |
| 212 | /* disconnect the error */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 213 | for (i = first; i && (i->next != eitem); i = i->next) {} |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 214 | assert(i); |
| 215 | i->next = NULL; |
| 216 | first->prev = i; |
| 217 | /* free this err and newer */ |
| 218 | ly_err_free(eitem); |
| 219 | } else { |
| 220 | /* free all err */ |
| 221 | ly_err_free(first); |
| 222 | pthread_setspecific(ctx->errlist_key, NULL); |
| 223 | } |
| 224 | } |
| 225 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 226 | LIBYANG_API_DEF LY_LOG_LEVEL |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 227 | ly_log_level(LY_LOG_LEVEL level) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 228 | { |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 229 | LY_LOG_LEVEL prev = ATOMIC_LOAD_RELAXED(ly_ll); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 230 | |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 231 | ATOMIC_STORE_RELAXED(ly_ll, level); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 232 | return prev; |
| 233 | } |
| 234 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 235 | LIBYANG_API_DEF uint32_t |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 236 | ly_log_options(uint32_t opts) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 237 | { |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 238 | uint32_t prev = ATOMIC_LOAD_RELAXED(ly_log_opts); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 239 | |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 240 | ATOMIC_STORE_RELAXED(ly_log_opts, opts); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 241 | return prev; |
| 242 | } |
| 243 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 244 | LIBYANG_API_DEF uint32_t |
Radek Krejci | 68433c9 | 2020-10-12 17:03:55 +0200 | [diff] [blame] | 245 | ly_log_dbg_groups(uint32_t dbg_groups) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 246 | { |
| 247 | #ifndef NDEBUG |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 248 | uint32_t prev = ATOMIC_LOAD_RELAXED(ly_ldbg_groups); |
Radek Krejci | ebdaed0 | 2020-11-09 13:05:06 +0100 | [diff] [blame] | 249 | |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 250 | ATOMIC_STORE_RELAXED(ly_ldbg_groups, dbg_groups); |
Radek Krejci | ebdaed0 | 2020-11-09 13:05:06 +0100 | [diff] [blame] | 251 | return prev; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 252 | #else |
| 253 | (void)dbg_groups; |
Radek Krejci | ebdaed0 | 2020-11-09 13:05:06 +0100 | [diff] [blame] | 254 | return 0; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 255 | #endif |
| 256 | } |
| 257 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 258 | LIBYANG_API_DEF void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 259 | ly_set_log_clb(ly_log_clb clb, ly_bool path) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 260 | { |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 261 | log_clb = clb; |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 262 | ATOMIC_STORE_RELAXED(path_flag, path); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 263 | } |
| 264 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 265 | LIBYANG_API_DEF ly_log_clb |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 266 | ly_get_log_clb(void) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 267 | { |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 268 | return log_clb; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 269 | } |
| 270 | |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 271 | void |
Michal Vasko | 59e69e7 | 2022-02-18 09:18:21 +0100 | [diff] [blame] | 272 | ly_log_location(const struct lysc_node *scnode, const struct lyd_node *dnode, const char *path, const struct ly_in *in, |
| 273 | uint64_t line, ly_bool reset) |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 274 | { |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 275 | if (scnode) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 276 | ly_set_add(&log_location.scnodes, (void *)scnode, 1, NULL); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 277 | } else if (reset) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 278 | ly_set_erase(&log_location.scnodes, NULL); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | if (dnode) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 282 | ly_set_add(&log_location.dnodes, (void *)dnode, 1, NULL); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 283 | } else if (reset) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 284 | ly_set_erase(&log_location.dnodes, NULL); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | if (path) { |
| 288 | char *s = strdup(path); |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 289 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 290 | LY_CHECK_ERR_RET(!s, LOGMEM(NULL), ); |
| 291 | ly_set_add(&log_location.paths, s, 1, NULL); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 292 | } else if (reset) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 293 | ly_set_erase(&log_location.paths, free); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | if (in) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 297 | ly_set_add(&log_location.inputs, (void *)in, 1, NULL); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 298 | } else if (reset) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 299 | ly_set_erase(&log_location.inputs, NULL); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | if (line) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 303 | log_location.line = line; |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
| 307 | void |
Michal Vasko | 59e69e7 | 2022-02-18 09:18:21 +0100 | [diff] [blame] | 308 | 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] | 309 | { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 310 | for (uint32_t i = scnode_steps; i && log_location.scnodes.count; i--) { |
| 311 | log_location.scnodes.count--; |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 312 | } |
| 313 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 314 | for (uint32_t i = dnode_steps; i && log_location.dnodes.count; i--) { |
| 315 | log_location.dnodes.count--; |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 316 | } |
| 317 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 318 | for (uint32_t i = path_steps; i && log_location.paths.count; i--) { |
| 319 | 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] | 320 | } |
| 321 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 322 | for (uint32_t i = in_steps; i && log_location.inputs.count; i--) { |
| 323 | log_location.inputs.count--; |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 324 | } |
| 325 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 326 | /* deallocate the empty sets */ |
| 327 | if (scnode_steps && !log_location.scnodes.count) { |
| 328 | ly_set_erase(&log_location.scnodes, NULL); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 329 | } |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 330 | if (dnode_steps && !log_location.dnodes.count) { |
| 331 | ly_set_erase(&log_location.dnodes, NULL); |
| 332 | } |
| 333 | if (path_steps && !log_location.paths.count) { |
| 334 | ly_set_erase(&log_location.paths, free); |
| 335 | } |
| 336 | if (in_steps && !log_location.inputs.count) { |
| 337 | ly_set_erase(&log_location.inputs, NULL); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 341 | static LY_ERR |
| 342 | log_store(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, LY_VECODE vecode, char *msg, char *path, char *apptag) |
| 343 | { |
| 344 | struct ly_err_item *eitem, *last; |
| 345 | |
| 346 | assert(ctx && (level < LY_LLVRB)); |
| 347 | |
| 348 | eitem = pthread_getspecific(ctx->errlist_key); |
| 349 | if (!eitem) { |
| 350 | /* if we are only to fill in path, there must have been an error stored */ |
| 351 | assert(msg); |
| 352 | eitem = malloc(sizeof *eitem); |
| 353 | LY_CHECK_GOTO(!eitem, mem_fail); |
| 354 | eitem->prev = eitem; |
| 355 | eitem->next = NULL; |
| 356 | |
| 357 | pthread_setspecific(ctx->errlist_key, eitem); |
| 358 | } else if (!msg) { |
| 359 | /* only filling the path */ |
| 360 | assert(path); |
| 361 | |
| 362 | /* find last error */ |
| 363 | eitem = eitem->prev; |
| 364 | do { |
| 365 | if (eitem->level == LY_LLERR) { |
| 366 | /* fill the path */ |
| 367 | free(eitem->path); |
| 368 | eitem->path = path; |
| 369 | return LY_SUCCESS; |
| 370 | } |
| 371 | eitem = eitem->prev; |
| 372 | } while (eitem->prev->next); |
| 373 | /* last error was not found */ |
| 374 | assert(0); |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 375 | } else if ((ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOSTORE_LAST) == LY_LOSTORE_LAST) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 376 | /* overwrite last message */ |
| 377 | free(eitem->msg); |
| 378 | free(eitem->path); |
| 379 | free(eitem->apptag); |
| 380 | } else { |
| 381 | /* store new message */ |
| 382 | last = eitem->prev; |
| 383 | eitem->prev = malloc(sizeof *eitem); |
| 384 | LY_CHECK_GOTO(!eitem->prev, mem_fail); |
| 385 | eitem = eitem->prev; |
| 386 | eitem->prev = last; |
| 387 | eitem->next = NULL; |
| 388 | last->next = eitem; |
| 389 | } |
| 390 | |
| 391 | /* fill in the information */ |
| 392 | eitem->level = level; |
| 393 | eitem->no = no; |
| 394 | eitem->vecode = vecode; |
| 395 | eitem->msg = msg; |
| 396 | eitem->path = path; |
| 397 | eitem->apptag = apptag; |
| 398 | return LY_SUCCESS; |
| 399 | |
| 400 | mem_fail: |
| 401 | LOGMEM(NULL); |
| 402 | free(msg); |
| 403 | free(path); |
| 404 | free(apptag); |
| 405 | return LY_EMEM; |
| 406 | } |
| 407 | |
| 408 | static void |
Michal Vasko | e9391c7 | 2021-10-05 10:04:56 +0200 | [diff] [blame] | 409 | 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] | 410 | const char *format, va_list args) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 411 | { |
| 412 | char *msg = NULL; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 413 | ly_bool free_strs; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 414 | |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 415 | if (level > ATOMIC_LOAD_RELAXED(ly_ll)) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 416 | /* do not print or store the message */ |
| 417 | free(path); |
| 418 | return; |
| 419 | } |
| 420 | |
Michal Vasko | 5a01692 | 2021-05-07 08:19:15 +0200 | [diff] [blame] | 421 | if (no == LY_EMEM) { |
| 422 | /* just print it, anything else would most likely fail anyway */ |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 423 | if (ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOLOG) { |
Michal Vasko | 5a01692 | 2021-05-07 08:19:15 +0200 | [diff] [blame] | 424 | if (log_clb) { |
| 425 | log_clb(level, LY_EMEM_MSG, path); |
| 426 | } else { |
| 427 | fprintf(stderr, "libyang[%d]: ", level); |
| 428 | vfprintf(stderr, format, args); |
| 429 | if (path) { |
| 430 | fprintf(stderr, " (path: %s)\n", path); |
| 431 | } else { |
| 432 | fprintf(stderr, "\n"); |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | free(path); |
| 437 | return; |
| 438 | } |
| 439 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 440 | /* store the error/warning (if we need to store errors internally, it does not matter what are the user log options) */ |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 441 | if ((level < LY_LLVRB) && ctx && (ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOSTORE)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 442 | assert(format); |
| 443 | if (vasprintf(&msg, format, args) == -1) { |
| 444 | LOGMEM(ctx); |
| 445 | free(path); |
| 446 | return; |
| 447 | } |
Radek Krejci | c9e64a6 | 2020-09-18 20:08:12 +0200 | [diff] [blame] | 448 | if (((no & ~LY_EPLUGIN) == LY_EVALID) && (vecode == LYVE_SUCCESS)) { |
| 449 | /* assume we are inheriting the error, so inherit vecode as well */ |
| 450 | vecode = ly_vecode(ctx); |
| 451 | } |
Michal Vasko | e9391c7 | 2021-10-05 10:04:56 +0200 | [diff] [blame] | 452 | 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] | 453 | return; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 454 | } |
| 455 | free_strs = 0; |
| 456 | } else { |
| 457 | if (vasprintf(&msg, format, args) == -1) { |
| 458 | LOGMEM(ctx); |
| 459 | free(path); |
| 460 | return; |
| 461 | } |
| 462 | free_strs = 1; |
| 463 | } |
| 464 | |
| 465 | /* if we are only storing errors internally, never print the message (yet) */ |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 466 | if (ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOLOG) { |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 467 | if (log_clb) { |
| 468 | log_clb(level, msg, path); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 469 | } else { |
| 470 | fprintf(stderr, "libyang[%d]: %s%s", level, msg, path ? " " : "\n"); |
| 471 | if (path) { |
| 472 | fprintf(stderr, "(path: %s)\n", path); |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | if (free_strs) { |
| 478 | free(path); |
| 479 | free(msg); |
| 480 | } |
| 481 | } |
| 482 | |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 483 | #ifndef NDEBUG |
| 484 | |
| 485 | void |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 486 | ly_log_dbg(uint32_t group, const char *format, ...) |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 487 | { |
| 488 | char *dbg_format; |
| 489 | const char *str_group; |
| 490 | va_list ap; |
| 491 | |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 492 | if (!(ATOMIC_LOAD_RELAXED(ly_ldbg_groups) & group)) { |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 493 | return; |
| 494 | } |
| 495 | |
| 496 | switch (group) { |
| 497 | case LY_LDGDICT: |
| 498 | str_group = "DICT"; |
| 499 | break; |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 500 | case LY_LDGXPATH: |
| 501 | str_group = "XPATH"; |
| 502 | break; |
Michal Vasko | e558f79 | 2021-07-28 08:20:15 +0200 | [diff] [blame] | 503 | case LY_LDGDEPSETS: |
| 504 | str_group = "DEPSETS"; |
| 505 | break; |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 506 | default: |
| 507 | LOGINT(NULL); |
| 508 | return; |
| 509 | } |
| 510 | |
| 511 | if (asprintf(&dbg_format, "%s: %s", str_group, format) == -1) { |
| 512 | LOGMEM(NULL); |
| 513 | return; |
| 514 | } |
| 515 | |
| 516 | va_start(ap, format); |
Michal Vasko | e9391c7 | 2021-10-05 10:04:56 +0200 | [diff] [blame] | 517 | log_vprintf(NULL, LY_LLDBG, 0, 0, NULL, NULL, dbg_format, ap); |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 518 | va_end(ap); |
| 519 | } |
| 520 | |
| 521 | #endif |
| 522 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 523 | void |
| 524 | ly_log(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, const char *format, ...) |
| 525 | { |
| 526 | va_list ap; |
| 527 | |
| 528 | va_start(ap, format); |
Michal Vasko | e9391c7 | 2021-10-05 10:04:56 +0200 | [diff] [blame] | 529 | log_vprintf(ctx, level, no, 0, NULL, NULL, format, ap); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 530 | va_end(ap); |
| 531 | } |
| 532 | |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 533 | static LY_ERR |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 534 | ly_vlog_build_path(const struct ly_ctx *ctx, char **path) |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 535 | { |
Radek Krejci | c04f0a2 | 2018-09-21 15:49:45 +0200 | [diff] [blame] | 536 | int rc; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 537 | char *str = NULL, *prev = NULL; |
Radek Krejci | cb3e647 | 2021-01-06 08:19:01 +0100 | [diff] [blame] | 538 | |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 539 | *path = NULL; |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 540 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 541 | 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] | 542 | /* simply get what is in the provided path string */ |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 543 | *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] | 544 | LY_CHECK_ERR_RET(!(*path), LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 545 | } else { |
| 546 | /* generate location string */ |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 547 | if (log_location.scnodes.count) { |
| 548 | 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] | 549 | LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM); |
| 550 | |
Michal Vasko | 959f8d8 | 2022-06-16 07:51:50 +0200 | [diff] [blame] | 551 | rc = asprintf(path, "Schema location \"%s\"", str); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 552 | free(str); |
| 553 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(ctx), LY_EMEM); |
| 554 | } |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 555 | if (log_location.dnodes.count) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 556 | prev = *path; |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 557 | str = lyd_path(log_location.dnodes.objs[log_location.dnodes.count - 1], LYD_PATH_STD, NULL, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 558 | LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM); |
| 559 | |
Michal Vasko | 959f8d8 | 2022-06-16 07:51:50 +0200 | [diff] [blame] | 560 | rc = asprintf(path, "%s%sata location \"%s\"", prev ? prev : "", prev ? ", d" : "D", str); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 561 | free(str); |
| 562 | free(prev); |
| 563 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(ctx), LY_EMEM); |
| 564 | } |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 565 | if (log_location.line) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 566 | prev = *path; |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 567 | rc = 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] | 568 | free(prev); |
| 569 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(ctx), LY_EMEM); |
| 570 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 571 | log_location.line = 0; |
| 572 | } else if (log_location.inputs.count) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 573 | prev = *path; |
| 574 | rc = asprintf(path, "%s%sine number %" PRIu64, prev ? prev : "", prev ? ", l" : "L", |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 575 | ((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] | 576 | free(prev); |
| 577 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(ctx), LY_EMEM); |
| 578 | } |
| 579 | |
| 580 | if (*path) { |
| 581 | prev = *path; |
| 582 | rc = asprintf(path, "%s.", prev); |
| 583 | free(prev); |
| 584 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(ctx), LY_EMEM); |
| 585 | } |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 586 | } |
| 587 | |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 588 | return LY_SUCCESS; |
| 589 | } |
| 590 | |
| 591 | void |
Michal Vasko | e9391c7 | 2021-10-05 10:04:56 +0200 | [diff] [blame] | 592 | 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] | 593 | { |
| 594 | va_list ap; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 595 | char *path = NULL; |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 596 | |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 597 | if (ATOMIC_LOAD_RELAXED(path_flag) && ctx) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 598 | ly_vlog_build_path(ctx, &path); |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | va_start(ap, format); |
Michal Vasko | e9391c7 | 2021-10-05 10:04:56 +0200 | [diff] [blame] | 602 | log_vprintf(ctx, LY_LLERR, LY_EVALID, code, path, apptag, format, ap); |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 603 | /* path is spent and should not be freed! */ |
| 604 | va_end(ap); |
| 605 | } |
| 606 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 607 | LIBYANG_API_DEF void |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 608 | lyplg_ext_log(const struct lysc_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no, const char *path, const char *format, ...) |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 609 | { |
| 610 | va_list ap; |
| 611 | char *plugin_msg; |
| 612 | int ret; |
| 613 | |
Václav Kubernát | d367ad9 | 2021-11-29 09:28:56 +0100 | [diff] [blame] | 614 | if (ATOMIC_LOAD_RELAXED(ly_ll) < level) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 615 | return; |
| 616 | } |
Radek Krejci | 932d3bb | 2021-02-09 16:29:38 +0100 | [diff] [blame] | 617 | ret = asprintf(&plugin_msg, "Extension plugin \"%s\": %s", ext->def->plugin->id, format); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 618 | if (ret == -1) { |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 619 | LOGMEM(ext->module->ctx); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 620 | return; |
| 621 | } |
| 622 | |
| 623 | va_start(ap, format); |
Michal Vasko | e9391c7 | 2021-10-05 10:04:56 +0200 | [diff] [blame] | 624 | log_vprintf(ext->module->ctx, level, (level == LY_LLERR ? LY_EPLUGIN : 0) | err_no, LYVE_OTHER, |
| 625 | path ? strdup(path) : NULL, NULL, plugin_msg, ap); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 626 | va_end(ap); |
| 627 | |
| 628 | free(plugin_msg); |
| 629 | } |
| 630 | |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 631 | /** |
Michal Vasko | c78a609 | 2021-05-07 15:27:35 +0200 | [diff] [blame] | 632 | * @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] | 633 | */ |
| 634 | static void |
Michal Vasko | c78a609 | 2021-05-07 15:27:35 +0200 | [diff] [blame] | 635 | _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] | 636 | { |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 637 | va_list ap; |
| 638 | char *path_dup = NULL; |
| 639 | |
| 640 | LY_CHECK_ARG_RET(ctx, eitem, ); |
| 641 | |
| 642 | if (eitem->path) { |
| 643 | /* duplicate path because it will be freed */ |
| 644 | path_dup = strdup(eitem->path); |
| 645 | LY_CHECK_ERR_RET(!path_dup, LOGMEM(ctx), ); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 646 | } |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 647 | |
Michal Vasko | c78a609 | 2021-05-07 15:27:35 +0200 | [diff] [blame] | 648 | va_start(ap, format); |
Michal Vasko | e9391c7 | 2021-10-05 10:04:56 +0200 | [diff] [blame] | 649 | 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] | 650 | va_end(ap); |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 651 | } |
| 652 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 653 | LIBYANG_API_DEF void |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 654 | ly_err_print(const struct ly_ctx *ctx, struct ly_err_item *eitem) |
| 655 | { |
Michal Vasko | c78a609 | 2021-05-07 15:27:35 +0200 | [diff] [blame] | 656 | /* String ::ly_err_item.msg cannot be used directly because it may contain the % character */ |
| 657 | _ly_err_print(ctx, eitem, "%s", eitem->msg); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 658 | } |