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