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