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 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
Radek Krejci | f8dc59a | 2020-11-25 13:47:44 +0100 | [diff] [blame] | 16 | #define _POSIX_C_SOURCE 200809L /* 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" |
| 35 | #include "tree_schema.h" |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 36 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 37 | volatile LY_LOG_LEVEL ly_ll = LY_LLWRN; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 38 | volatile uint32_t ly_log_opts = LY_LOLOG | LY_LOSTORE_LAST; |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 39 | static ly_log_clb log_clb; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 40 | static volatile ly_bool path_flag = 1; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 41 | #ifndef NDEBUG |
Radek Krejci | 68433c9 | 2020-10-12 17:03:55 +0200 | [diff] [blame] | 42 | volatile uint32_t ly_ldbg_groups = 0; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 43 | #endif |
| 44 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 45 | THREAD_LOCAL struct ly_log_location_s log_location = {0}; |
| 46 | |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 47 | /* how many bytes add when enlarging buffers */ |
| 48 | #define LY_BUF_STEP 128 |
| 49 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 50 | API LY_ERR |
| 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 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 63 | API LY_VECODE |
| 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 | |
| 76 | API const char * |
| 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 | |
| 91 | API const char * |
| 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 | |
| 106 | API const char * |
| 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 | |
| 121 | API struct ly_err_item * |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 122 | ly_err_new(LY_LOG_LEVEL level, LY_ERR no, LY_VECODE vecode, char *msg, char *path, char *apptag) |
| 123 | { |
| 124 | struct ly_err_item *eitem; |
| 125 | |
| 126 | eitem = malloc(sizeof *eitem); |
| 127 | LY_CHECK_ERR_RET(!eitem, LOGMEM(NULL), NULL); |
| 128 | eitem->prev = eitem; |
| 129 | eitem->next = NULL; |
| 130 | |
| 131 | /* fill in the information */ |
| 132 | eitem->level = level; |
| 133 | eitem->no = no; |
| 134 | eitem->vecode = vecode; |
| 135 | eitem->msg = msg; |
| 136 | eitem->path = path; |
| 137 | eitem->apptag = apptag; |
| 138 | |
| 139 | return eitem; |
| 140 | } |
| 141 | |
| 142 | API struct ly_err_item * |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 143 | ly_err_first(const struct ly_ctx *ctx) |
| 144 | { |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 145 | LY_CHECK_ARG_RET(NULL, ctx, NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 146 | |
| 147 | return pthread_getspecific(ctx->errlist_key); |
| 148 | } |
| 149 | |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 150 | API struct ly_err_item * |
| 151 | ly_err_last(const struct ly_ctx *ctx) |
| 152 | { |
| 153 | const struct ly_err_item *e; |
| 154 | |
| 155 | LY_CHECK_ARG_RET(NULL, ctx, NULL); |
| 156 | |
| 157 | e = pthread_getspecific(ctx->errlist_key); |
| 158 | return e ? e->prev : NULL; |
| 159 | } |
| 160 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 161 | API void |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 162 | ly_err_free(void *ptr) |
| 163 | { |
| 164 | struct ly_err_item *i, *next; |
| 165 | |
| 166 | /* clean the error list */ |
| 167 | for (i = (struct ly_err_item *)ptr; i; i = next) { |
| 168 | next = i->next; |
Radek Krejci | e269220 | 2020-12-01 14:21:12 +0100 | [diff] [blame] | 169 | if (i->msg && strcmp(i->msg, LY_EMEM_MSG)) { |
| 170 | free(i->msg); |
| 171 | } |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 172 | free(i->path); |
| 173 | free(i->apptag); |
| 174 | free(i); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | API void |
| 179 | ly_err_clean(struct ly_ctx *ctx, struct ly_err_item *eitem) |
| 180 | { |
| 181 | struct ly_err_item *i, *first; |
| 182 | |
| 183 | first = ly_err_first(ctx); |
| 184 | if (first == eitem) { |
| 185 | eitem = NULL; |
| 186 | } |
| 187 | if (eitem) { |
| 188 | /* disconnect the error */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 189 | for (i = first; i && (i->next != eitem); i = i->next) {} |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 190 | assert(i); |
| 191 | i->next = NULL; |
| 192 | first->prev = i; |
| 193 | /* free this err and newer */ |
| 194 | ly_err_free(eitem); |
| 195 | } else { |
| 196 | /* free all err */ |
| 197 | ly_err_free(first); |
| 198 | pthread_setspecific(ctx->errlist_key, NULL); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | API LY_LOG_LEVEL |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 203 | ly_log_level(LY_LOG_LEVEL level) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 204 | { |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 205 | LY_LOG_LEVEL prev = ly_ll; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 206 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 207 | ly_ll = level; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 208 | return prev; |
| 209 | } |
| 210 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 211 | API uint32_t |
| 212 | ly_log_options(uint32_t opts) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 213 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 214 | uint32_t prev = ly_log_opts; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 215 | |
| 216 | ly_log_opts = opts; |
| 217 | return prev; |
| 218 | } |
| 219 | |
Radek Krejci | ebdaed0 | 2020-11-09 13:05:06 +0100 | [diff] [blame] | 220 | API uint32_t |
Radek Krejci | 68433c9 | 2020-10-12 17:03:55 +0200 | [diff] [blame] | 221 | ly_log_dbg_groups(uint32_t dbg_groups) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 222 | { |
| 223 | #ifndef NDEBUG |
Radek Krejci | ebdaed0 | 2020-11-09 13:05:06 +0100 | [diff] [blame] | 224 | uint32_t prev = ly_ldbg_groups; |
| 225 | |
Radek Krejci | 68433c9 | 2020-10-12 17:03:55 +0200 | [diff] [blame] | 226 | ly_ldbg_groups = dbg_groups; |
Radek Krejci | ebdaed0 | 2020-11-09 13:05:06 +0100 | [diff] [blame] | 227 | return prev; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 228 | #else |
| 229 | (void)dbg_groups; |
Radek Krejci | ebdaed0 | 2020-11-09 13:05:06 +0100 | [diff] [blame] | 230 | return 0; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 231 | #endif |
| 232 | } |
| 233 | |
| 234 | API void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 235 | ly_set_log_clb(ly_log_clb clb, ly_bool path) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 236 | { |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 237 | log_clb = clb; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 238 | path_flag = path; |
| 239 | } |
| 240 | |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 241 | API ly_log_clb |
| 242 | ly_get_log_clb(void) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 243 | { |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 244 | return log_clb; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 245 | } |
| 246 | |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 247 | void |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 248 | ly_log_location(const struct lysc_node *scnode, const struct lyd_node *dnode, |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 249 | const char *path, const struct ly_in *in, uint64_t line, ly_bool reset) |
| 250 | { |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 251 | if (scnode) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 252 | ly_set_add(&log_location.scnodes, (void *)scnode, 1, NULL); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 253 | } else if (reset) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 254 | ly_set_erase(&log_location.scnodes, NULL); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | if (dnode) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 258 | ly_set_add(&log_location.dnodes, (void *)dnode, 1, NULL); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 259 | } else if (reset) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 260 | ly_set_erase(&log_location.dnodes, NULL); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | if (path) { |
| 264 | char *s = strdup(path); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 265 | LY_CHECK_ERR_RET(!s, LOGMEM(NULL), ); |
| 266 | ly_set_add(&log_location.paths, s, 1, NULL); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 267 | } else if (reset) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 268 | ly_set_erase(&log_location.paths, free); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | if (in) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 272 | ly_set_add(&log_location.inputs, (void *)in, 1, NULL); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 273 | } else if (reset) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 274 | ly_set_erase(&log_location.inputs, NULL); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | if (line) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 278 | log_location.line = line; |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
| 282 | void |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 283 | ly_log_location_revert(uint32_t scnode_steps, uint32_t dnode_steps, |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 284 | uint32_t path_steps, uint32_t in_steps) |
| 285 | { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 286 | for (uint32_t i = scnode_steps; i && log_location.scnodes.count; i--) { |
| 287 | log_location.scnodes.count--; |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 288 | } |
| 289 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 290 | for (uint32_t i = dnode_steps; i && log_location.dnodes.count; i--) { |
| 291 | log_location.dnodes.count--; |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 292 | } |
| 293 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 294 | for (uint32_t i = path_steps; i && log_location.paths.count; i--) { |
| 295 | 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] | 296 | } |
| 297 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 298 | for (uint32_t i = in_steps; i && log_location.inputs.count; i--) { |
| 299 | log_location.inputs.count--; |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 300 | } |
| 301 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 302 | /* deallocate the empty sets */ |
| 303 | if (scnode_steps && !log_location.scnodes.count) { |
| 304 | ly_set_erase(&log_location.scnodes, NULL); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 305 | } |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 306 | if (dnode_steps && !log_location.dnodes.count) { |
| 307 | ly_set_erase(&log_location.dnodes, NULL); |
| 308 | } |
| 309 | if (path_steps && !log_location.paths.count) { |
| 310 | ly_set_erase(&log_location.paths, free); |
| 311 | } |
| 312 | if (in_steps && !log_location.inputs.count) { |
| 313 | ly_set_erase(&log_location.inputs, NULL); |
Radek Krejci | addfc9a | 2020-12-17 20:46:35 +0100 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 317 | static LY_ERR |
| 318 | log_store(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, LY_VECODE vecode, char *msg, char *path, char *apptag) |
| 319 | { |
| 320 | struct ly_err_item *eitem, *last; |
| 321 | |
| 322 | assert(ctx && (level < LY_LLVRB)); |
| 323 | |
| 324 | eitem = pthread_getspecific(ctx->errlist_key); |
| 325 | if (!eitem) { |
| 326 | /* if we are only to fill in path, there must have been an error stored */ |
| 327 | assert(msg); |
| 328 | eitem = malloc(sizeof *eitem); |
| 329 | LY_CHECK_GOTO(!eitem, mem_fail); |
| 330 | eitem->prev = eitem; |
| 331 | eitem->next = NULL; |
| 332 | |
| 333 | pthread_setspecific(ctx->errlist_key, eitem); |
| 334 | } else if (!msg) { |
| 335 | /* only filling the path */ |
| 336 | assert(path); |
| 337 | |
| 338 | /* find last error */ |
| 339 | eitem = eitem->prev; |
| 340 | do { |
| 341 | if (eitem->level == LY_LLERR) { |
| 342 | /* fill the path */ |
| 343 | free(eitem->path); |
| 344 | eitem->path = path; |
| 345 | return LY_SUCCESS; |
| 346 | } |
| 347 | eitem = eitem->prev; |
| 348 | } while (eitem->prev->next); |
| 349 | /* last error was not found */ |
| 350 | assert(0); |
Michal Vasko | ed94a29 | 2019-11-06 15:43:41 +0100 | [diff] [blame] | 351 | } else if ((ly_log_opts & LY_LOSTORE_LAST) == LY_LOSTORE_LAST) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 352 | /* overwrite last message */ |
| 353 | free(eitem->msg); |
| 354 | free(eitem->path); |
| 355 | free(eitem->apptag); |
| 356 | } else { |
| 357 | /* store new message */ |
| 358 | last = eitem->prev; |
| 359 | eitem->prev = malloc(sizeof *eitem); |
| 360 | LY_CHECK_GOTO(!eitem->prev, mem_fail); |
| 361 | eitem = eitem->prev; |
| 362 | eitem->prev = last; |
| 363 | eitem->next = NULL; |
| 364 | last->next = eitem; |
| 365 | } |
| 366 | |
| 367 | /* fill in the information */ |
| 368 | eitem->level = level; |
| 369 | eitem->no = no; |
| 370 | eitem->vecode = vecode; |
| 371 | eitem->msg = msg; |
| 372 | eitem->path = path; |
| 373 | eitem->apptag = apptag; |
| 374 | return LY_SUCCESS; |
| 375 | |
| 376 | mem_fail: |
| 377 | LOGMEM(NULL); |
| 378 | free(msg); |
| 379 | free(path); |
| 380 | free(apptag); |
| 381 | return LY_EMEM; |
| 382 | } |
| 383 | |
| 384 | static void |
| 385 | log_vprintf(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, LY_VECODE vecode, char *path, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 386 | const char *format, va_list args) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 387 | { |
| 388 | char *msg = NULL; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 389 | ly_bool free_strs; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 390 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 391 | if (level > ly_ll) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 392 | /* do not print or store the message */ |
| 393 | free(path); |
| 394 | return; |
| 395 | } |
| 396 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 397 | /* store the error/warning (if we need to store errors internally, it does not matter what are the user log options) */ |
Michal Vasko | ed94a29 | 2019-11-06 15:43:41 +0100 | [diff] [blame] | 398 | if ((level < LY_LLVRB) && ctx && (ly_log_opts & LY_LOSTORE)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 399 | assert(format); |
| 400 | if (vasprintf(&msg, format, args) == -1) { |
| 401 | LOGMEM(ctx); |
| 402 | free(path); |
| 403 | return; |
| 404 | } |
Radek Krejci | c9e64a6 | 2020-09-18 20:08:12 +0200 | [diff] [blame] | 405 | if (((no & ~LY_EPLUGIN) == LY_EVALID) && (vecode == LYVE_SUCCESS)) { |
| 406 | /* assume we are inheriting the error, so inherit vecode as well */ |
| 407 | vecode = ly_vecode(ctx); |
| 408 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 409 | if (log_store(ctx, level, no, vecode, msg, path, NULL)) { |
| 410 | return; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 411 | } |
| 412 | free_strs = 0; |
| 413 | } else { |
| 414 | if (vasprintf(&msg, format, args) == -1) { |
| 415 | LOGMEM(ctx); |
| 416 | free(path); |
| 417 | return; |
| 418 | } |
| 419 | free_strs = 1; |
| 420 | } |
| 421 | |
| 422 | /* if we are only storing errors internally, never print the message (yet) */ |
Michal Vasko | ed94a29 | 2019-11-06 15:43:41 +0100 | [diff] [blame] | 423 | if (ly_log_opts & LY_LOLOG) { |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 424 | if (log_clb) { |
| 425 | log_clb(level, msg, path); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 426 | } else { |
| 427 | fprintf(stderr, "libyang[%d]: %s%s", level, msg, path ? " " : "\n"); |
| 428 | if (path) { |
| 429 | fprintf(stderr, "(path: %s)\n", path); |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | if (free_strs) { |
| 435 | free(path); |
| 436 | free(msg); |
| 437 | } |
| 438 | } |
| 439 | |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 440 | #ifndef NDEBUG |
| 441 | |
| 442 | void |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 443 | ly_log_dbg(uint32_t group, const char *format, ...) |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 444 | { |
| 445 | char *dbg_format; |
| 446 | const char *str_group; |
| 447 | va_list ap; |
| 448 | |
Radek Krejci | 68433c9 | 2020-10-12 17:03:55 +0200 | [diff] [blame] | 449 | if (!(ly_ldbg_groups & group)) { |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 450 | return; |
| 451 | } |
| 452 | |
| 453 | switch (group) { |
| 454 | case LY_LDGDICT: |
| 455 | str_group = "DICT"; |
| 456 | break; |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 457 | case LY_LDGXPATH: |
| 458 | str_group = "XPATH"; |
| 459 | break; |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 460 | default: |
| 461 | LOGINT(NULL); |
| 462 | return; |
| 463 | } |
| 464 | |
| 465 | if (asprintf(&dbg_format, "%s: %s", str_group, format) == -1) { |
| 466 | LOGMEM(NULL); |
| 467 | return; |
| 468 | } |
| 469 | |
| 470 | va_start(ap, format); |
| 471 | log_vprintf(NULL, LY_LLDBG, 0, 0, NULL, dbg_format, ap); |
| 472 | va_end(ap); |
| 473 | } |
| 474 | |
| 475 | #endif |
| 476 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 477 | void |
| 478 | ly_log(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, const char *format, ...) |
| 479 | { |
| 480 | va_list ap; |
| 481 | |
| 482 | va_start(ap, format); |
| 483 | log_vprintf(ctx, level, no, 0, NULL, format, ap); |
| 484 | va_end(ap); |
| 485 | } |
| 486 | |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 487 | static LY_ERR |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 488 | ly_vlog_build_path(const struct ly_ctx *ctx, char **path) |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 489 | { |
Radek Krejci | c04f0a2 | 2018-09-21 15:49:45 +0200 | [diff] [blame] | 490 | int rc; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 491 | char *str = NULL, *prev = NULL; |
Radek Krejci | cb3e647 | 2021-01-06 08:19:01 +0100 | [diff] [blame] | 492 | |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 493 | *path = NULL; |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 494 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 495 | 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] | 496 | /* simply get what is in the provided path string */ |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 497 | *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] | 498 | LY_CHECK_ERR_RET(!(*path), LOGMEM(ctx), LY_EMEM); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 499 | } else { |
| 500 | /* generate location string */ |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 501 | if (log_location.scnodes.count) { |
| 502 | 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] | 503 | LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM); |
| 504 | |
| 505 | rc = asprintf(path, "Schema location %s", str); |
| 506 | free(str); |
| 507 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(ctx), LY_EMEM); |
| 508 | } |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 509 | if (log_location.dnodes.count) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 510 | prev = *path; |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 511 | 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] | 512 | LY_CHECK_ERR_RET(!str, LOGMEM(ctx), LY_EMEM); |
| 513 | |
| 514 | rc = asprintf(path, "%s%sata location %s", prev ? prev : "", prev ? ", d" : "D", str); |
| 515 | free(str); |
| 516 | free(prev); |
| 517 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(ctx), LY_EMEM); |
| 518 | } |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 519 | if (log_location.line) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 520 | prev = *path; |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 521 | 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] | 522 | free(prev); |
| 523 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(ctx), LY_EMEM); |
| 524 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 525 | log_location.line = 0; |
| 526 | } else if (log_location.inputs.count) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 527 | prev = *path; |
| 528 | rc = asprintf(path, "%s%sine number %" PRIu64, prev ? prev : "", prev ? ", l" : "L", |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 529 | ((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] | 530 | free(prev); |
| 531 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(ctx), LY_EMEM); |
| 532 | } |
| 533 | |
| 534 | if (*path) { |
| 535 | prev = *path; |
| 536 | rc = asprintf(path, "%s.", prev); |
| 537 | free(prev); |
| 538 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(ctx), LY_EMEM); |
| 539 | } |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 540 | } |
| 541 | |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 542 | return LY_SUCCESS; |
| 543 | } |
| 544 | |
| 545 | void |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 546 | ly_vlog(const struct ly_ctx *ctx, LY_VECODE code, const char *format, ...) |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 547 | { |
| 548 | va_list ap; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 549 | char *path = NULL; |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 550 | |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 551 | if (path_flag && ctx) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 552 | ly_vlog_build_path(ctx, &path); |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | va_start(ap, format); |
| 556 | log_vprintf(ctx, LY_LLERR, LY_EVALID, code, path, format, ap); |
| 557 | /* path is spent and should not be freed! */ |
| 558 | va_end(ap); |
| 559 | } |
| 560 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 561 | API void |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 562 | lyext_log(const struct lysc_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no, const char *path, const char *format, ...) |
| 563 | { |
| 564 | va_list ap; |
| 565 | char *plugin_msg; |
| 566 | int ret; |
| 567 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 568 | if (ly_ll < level) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 569 | return; |
| 570 | } |
Radek Krejci | 932d3bb | 2021-02-09 16:29:38 +0100 | [diff] [blame] | 571 | 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] | 572 | if (ret == -1) { |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 573 | LOGMEM(ext->module->ctx); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 574 | return; |
| 575 | } |
| 576 | |
| 577 | va_start(ap, format); |
Radek Krejci | a4614e6 | 2020-05-15 14:19:28 +0200 | [diff] [blame] | 578 | log_vprintf(ext->module->ctx, level, (level == LY_LLERR ? LY_EPLUGIN : 0) | err_no, LYVE_OTHER, path ? strdup(path) : NULL, plugin_msg, ap); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 579 | va_end(ap); |
| 580 | |
| 581 | free(plugin_msg); |
| 582 | } |
| 583 | |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 584 | /** |
| 585 | * @brief Exact same functionality as ::ly_err_print() but has variable arguments so va_start() can |
| 586 | * be used and an empty va_list created. |
| 587 | */ |
| 588 | static void |
| 589 | _ly_err_print(const struct ly_ctx *ctx, struct ly_err_item *eitem, ...) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 590 | { |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 591 | va_list ap; |
| 592 | char *path_dup = NULL; |
| 593 | |
| 594 | LY_CHECK_ARG_RET(ctx, eitem, ); |
| 595 | |
| 596 | if (eitem->path) { |
| 597 | /* duplicate path because it will be freed */ |
| 598 | path_dup = strdup(eitem->path); |
| 599 | LY_CHECK_ERR_RET(!path_dup, LOGMEM(ctx), ); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 600 | } |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 601 | |
| 602 | va_start(ap, eitem); |
| 603 | log_vprintf(ctx, eitem->level, eitem->no, eitem->vecode, eitem->path, eitem->msg, ap); |
| 604 | va_end(ap); |
| 605 | |
| 606 | if (path_dup) { |
| 607 | eitem->path = path_dup; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | API void |
| 612 | ly_err_print(const struct ly_ctx *ctx, struct ly_err_item *eitem) |
| 613 | { |
| 614 | _ly_err_print(ctx, eitem); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 615 | } |