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 |
| 16 | |
| 17 | #include "log.h" |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 18 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 19 | #include <assert.h> |
Radek Krejci | c04f0a2 | 2018-09-21 15:49:45 +0200 | [diff] [blame] | 20 | #include <inttypes.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 21 | #include <pthread.h> |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 22 | #include <stdarg.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 23 | #include <stdint.h> |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 24 | #include <stdio.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 27 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 28 | #include "common.h" |
Radek Krejci | aa45bda | 2020-07-20 07:43:38 +0200 | [diff] [blame] | 29 | #include "compat.h" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 30 | #include "plugins_exts.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 31 | #include "tree_data.h" |
| 32 | #include "tree_schema.h" |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 33 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 34 | volatile LY_LOG_LEVEL ly_log_level = LY_LLWRN; |
| 35 | volatile uint32_t ly_log_opts = LY_LOLOG | LY_LOSTORE_LAST; |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 36 | static ly_log_clb log_clb; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 37 | static volatile ly_bool path_flag = 1; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 38 | #ifndef NDEBUG |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 39 | volatile uint32_t ly_log_dbg_groups = 0; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 40 | #endif |
| 41 | |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 42 | /* how many bytes add when enlarging buffers */ |
| 43 | #define LY_BUF_STEP 128 |
| 44 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 45 | API LY_ERR |
| 46 | ly_errcode(const struct ly_ctx *ctx) |
| 47 | { |
| 48 | struct ly_err_item *i; |
| 49 | |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 50 | i = ly_err_last(ctx); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 51 | if (i) { |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 52 | return i->no; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | return LY_SUCCESS; |
| 56 | } |
| 57 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 58 | API LY_VECODE |
| 59 | ly_vecode(const struct ly_ctx *ctx) |
| 60 | { |
| 61 | struct ly_err_item *i; |
| 62 | |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 63 | i = ly_err_last(ctx); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 64 | if (i) { |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 65 | return i->vecode; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | return LYVE_SUCCESS; |
| 69 | } |
| 70 | |
| 71 | API const char * |
| 72 | ly_errmsg(const struct ly_ctx *ctx) |
| 73 | { |
| 74 | struct ly_err_item *i; |
| 75 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 76 | LY_CHECK_ARG_RET(NULL, ctx, NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 77 | |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 78 | i = ly_err_last(ctx); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 79 | if (i) { |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 80 | return i->msg; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | return NULL; |
| 84 | } |
| 85 | |
| 86 | API const char * |
| 87 | ly_errpath(const struct ly_ctx *ctx) |
| 88 | { |
| 89 | struct ly_err_item *i; |
| 90 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 91 | LY_CHECK_ARG_RET(NULL, ctx, NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 92 | |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 93 | i = ly_err_last(ctx); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 94 | if (i) { |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 95 | return i->path; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | return NULL; |
| 99 | } |
| 100 | |
| 101 | API const char * |
| 102 | ly_errapptag(const struct ly_ctx *ctx) |
| 103 | { |
| 104 | struct ly_err_item *i; |
| 105 | |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 106 | LY_CHECK_ARG_RET(NULL, ctx, NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 107 | |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 108 | i = ly_err_last(ctx); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 109 | if (i) { |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 110 | return i->apptag; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | return NULL; |
| 114 | } |
| 115 | |
| 116 | API struct ly_err_item * |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 117 | ly_err_new(LY_LOG_LEVEL level, LY_ERR no, LY_VECODE vecode, char *msg, char *path, char *apptag) |
| 118 | { |
| 119 | struct ly_err_item *eitem; |
| 120 | |
| 121 | eitem = malloc(sizeof *eitem); |
| 122 | LY_CHECK_ERR_RET(!eitem, LOGMEM(NULL), NULL); |
| 123 | eitem->prev = eitem; |
| 124 | eitem->next = NULL; |
| 125 | |
| 126 | /* fill in the information */ |
| 127 | eitem->level = level; |
| 128 | eitem->no = no; |
| 129 | eitem->vecode = vecode; |
| 130 | eitem->msg = msg; |
| 131 | eitem->path = path; |
| 132 | eitem->apptag = apptag; |
| 133 | |
| 134 | return eitem; |
| 135 | } |
| 136 | |
| 137 | API struct ly_err_item * |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 138 | ly_err_first(const struct ly_ctx *ctx) |
| 139 | { |
Michal Vasko | b3d0d6b | 2018-09-07 10:17:33 +0200 | [diff] [blame] | 140 | LY_CHECK_ARG_RET(NULL, ctx, NULL); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 141 | |
| 142 | return pthread_getspecific(ctx->errlist_key); |
| 143 | } |
| 144 | |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 145 | API struct ly_err_item * |
| 146 | ly_err_last(const struct ly_ctx *ctx) |
| 147 | { |
| 148 | const struct ly_err_item *e; |
| 149 | |
| 150 | LY_CHECK_ARG_RET(NULL, ctx, NULL); |
| 151 | |
| 152 | e = pthread_getspecific(ctx->errlist_key); |
| 153 | return e ? e->prev : NULL; |
| 154 | } |
| 155 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 156 | API void |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 157 | ly_err_free(void *ptr) |
| 158 | { |
| 159 | struct ly_err_item *i, *next; |
| 160 | |
| 161 | /* clean the error list */ |
| 162 | for (i = (struct ly_err_item *)ptr; i; i = next) { |
| 163 | next = i->next; |
| 164 | free(i->msg); |
| 165 | free(i->path); |
| 166 | free(i->apptag); |
| 167 | free(i); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | API void |
| 172 | ly_err_clean(struct ly_ctx *ctx, struct ly_err_item *eitem) |
| 173 | { |
| 174 | struct ly_err_item *i, *first; |
| 175 | |
| 176 | first = ly_err_first(ctx); |
| 177 | if (first == eitem) { |
| 178 | eitem = NULL; |
| 179 | } |
| 180 | if (eitem) { |
| 181 | /* disconnect the error */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 182 | for (i = first; i && (i->next != eitem); i = i->next) {} |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 183 | assert(i); |
| 184 | i->next = NULL; |
| 185 | first->prev = i; |
| 186 | /* free this err and newer */ |
| 187 | ly_err_free(eitem); |
| 188 | } else { |
| 189 | /* free all err */ |
| 190 | ly_err_free(first); |
| 191 | pthread_setspecific(ctx->errlist_key, NULL); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | API LY_LOG_LEVEL |
| 196 | ly_verb(LY_LOG_LEVEL level) |
| 197 | { |
| 198 | LY_LOG_LEVEL prev = ly_log_level; |
| 199 | |
| 200 | ly_log_level = level; |
| 201 | return prev; |
| 202 | } |
| 203 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 204 | API uint32_t |
| 205 | ly_log_options(uint32_t opts) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 206 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 207 | uint32_t prev = ly_log_opts; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 208 | |
| 209 | ly_log_opts = opts; |
| 210 | return prev; |
| 211 | } |
| 212 | |
| 213 | API void |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 214 | ly_verb_dbg(uint32_t dbg_groups) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 215 | { |
| 216 | #ifndef NDEBUG |
| 217 | ly_log_dbg_groups = dbg_groups; |
| 218 | #else |
| 219 | (void)dbg_groups; |
| 220 | #endif |
| 221 | } |
| 222 | |
| 223 | API void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 224 | ly_set_log_clb(ly_log_clb clb, ly_bool path) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 225 | { |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 226 | log_clb = clb; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 227 | path_flag = path; |
| 228 | } |
| 229 | |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 230 | API ly_log_clb |
| 231 | ly_get_log_clb(void) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 232 | { |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 233 | return log_clb; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | static LY_ERR |
| 237 | log_store(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, LY_VECODE vecode, char *msg, char *path, char *apptag) |
| 238 | { |
| 239 | struct ly_err_item *eitem, *last; |
| 240 | |
| 241 | assert(ctx && (level < LY_LLVRB)); |
| 242 | |
| 243 | eitem = pthread_getspecific(ctx->errlist_key); |
| 244 | if (!eitem) { |
| 245 | /* if we are only to fill in path, there must have been an error stored */ |
| 246 | assert(msg); |
| 247 | eitem = malloc(sizeof *eitem); |
| 248 | LY_CHECK_GOTO(!eitem, mem_fail); |
| 249 | eitem->prev = eitem; |
| 250 | eitem->next = NULL; |
| 251 | |
| 252 | pthread_setspecific(ctx->errlist_key, eitem); |
| 253 | } else if (!msg) { |
| 254 | /* only filling the path */ |
| 255 | assert(path); |
| 256 | |
| 257 | /* find last error */ |
| 258 | eitem = eitem->prev; |
| 259 | do { |
| 260 | if (eitem->level == LY_LLERR) { |
| 261 | /* fill the path */ |
| 262 | free(eitem->path); |
| 263 | eitem->path = path; |
| 264 | return LY_SUCCESS; |
| 265 | } |
| 266 | eitem = eitem->prev; |
| 267 | } while (eitem->prev->next); |
| 268 | /* last error was not found */ |
| 269 | assert(0); |
Michal Vasko | ed94a29 | 2019-11-06 15:43:41 +0100 | [diff] [blame] | 270 | } else if ((ly_log_opts & LY_LOSTORE_LAST) == LY_LOSTORE_LAST) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 271 | /* overwrite last message */ |
| 272 | free(eitem->msg); |
| 273 | free(eitem->path); |
| 274 | free(eitem->apptag); |
| 275 | } else { |
| 276 | /* store new message */ |
| 277 | last = eitem->prev; |
| 278 | eitem->prev = malloc(sizeof *eitem); |
| 279 | LY_CHECK_GOTO(!eitem->prev, mem_fail); |
| 280 | eitem = eitem->prev; |
| 281 | eitem->prev = last; |
| 282 | eitem->next = NULL; |
| 283 | last->next = eitem; |
| 284 | } |
| 285 | |
| 286 | /* fill in the information */ |
| 287 | eitem->level = level; |
| 288 | eitem->no = no; |
| 289 | eitem->vecode = vecode; |
| 290 | eitem->msg = msg; |
| 291 | eitem->path = path; |
| 292 | eitem->apptag = apptag; |
| 293 | return LY_SUCCESS; |
| 294 | |
| 295 | mem_fail: |
| 296 | LOGMEM(NULL); |
| 297 | free(msg); |
| 298 | free(path); |
| 299 | free(apptag); |
| 300 | return LY_EMEM; |
| 301 | } |
| 302 | |
| 303 | static void |
| 304 | 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] | 305 | const char *format, va_list args) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 306 | { |
| 307 | char *msg = NULL; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 308 | ly_bool free_strs; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 309 | |
Michal Vasko | ed94a29 | 2019-11-06 15:43:41 +0100 | [diff] [blame] | 310 | if (level > ly_log_level) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 311 | /* do not print or store the message */ |
| 312 | free(path); |
| 313 | return; |
| 314 | } |
| 315 | |
Radek Krejci | a4614e6 | 2020-05-15 14:19:28 +0200 | [diff] [blame] | 316 | if (((no & ~LY_EPLUGIN) == LY_EVALID) && (vecode == LYVE_SUCCESS)) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 317 | /* assume we are inheriting the error, so inherit vecode as well */ |
| 318 | vecode = ly_vecode(ctx); |
| 319 | } |
| 320 | |
| 321 | /* 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] | 322 | if ((level < LY_LLVRB) && ctx && (ly_log_opts & LY_LOSTORE)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 323 | assert(format); |
| 324 | if (vasprintf(&msg, format, args) == -1) { |
| 325 | LOGMEM(ctx); |
| 326 | free(path); |
| 327 | return; |
| 328 | } |
| 329 | if (log_store(ctx, level, no, vecode, msg, path, NULL)) { |
| 330 | return; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 331 | } |
| 332 | free_strs = 0; |
| 333 | } else { |
| 334 | if (vasprintf(&msg, format, args) == -1) { |
| 335 | LOGMEM(ctx); |
| 336 | free(path); |
| 337 | return; |
| 338 | } |
| 339 | free_strs = 1; |
| 340 | } |
| 341 | |
| 342 | /* if we are only storing errors internally, never print the message (yet) */ |
Michal Vasko | ed94a29 | 2019-11-06 15:43:41 +0100 | [diff] [blame] | 343 | if (ly_log_opts & LY_LOLOG) { |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 344 | if (log_clb) { |
| 345 | log_clb(level, msg, path); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 346 | } else { |
| 347 | fprintf(stderr, "libyang[%d]: %s%s", level, msg, path ? " " : "\n"); |
| 348 | if (path) { |
| 349 | fprintf(stderr, "(path: %s)\n", path); |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | if (free_strs) { |
| 355 | free(path); |
| 356 | free(msg); |
| 357 | } |
| 358 | } |
| 359 | |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 360 | #ifndef NDEBUG |
| 361 | |
| 362 | void |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 363 | ly_log_dbg(uint32_t group, const char *format, ...) |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 364 | { |
| 365 | char *dbg_format; |
| 366 | const char *str_group; |
| 367 | va_list ap; |
| 368 | |
| 369 | if (!(ly_log_dbg_groups & group)) { |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | switch (group) { |
| 374 | case LY_LDGDICT: |
| 375 | str_group = "DICT"; |
| 376 | break; |
| 377 | case LY_LDGYANG: |
| 378 | str_group = "YANG"; |
| 379 | break; |
| 380 | case LY_LDGYIN: |
| 381 | str_group = "YIN"; |
| 382 | break; |
| 383 | case LY_LDGXPATH: |
| 384 | str_group = "XPATH"; |
| 385 | break; |
| 386 | case LY_LDGDIFF: |
| 387 | str_group = "DIFF"; |
| 388 | break; |
| 389 | default: |
| 390 | LOGINT(NULL); |
| 391 | return; |
| 392 | } |
| 393 | |
| 394 | if (asprintf(&dbg_format, "%s: %s", str_group, format) == -1) { |
| 395 | LOGMEM(NULL); |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | va_start(ap, format); |
| 400 | log_vprintf(NULL, LY_LLDBG, 0, 0, NULL, dbg_format, ap); |
| 401 | va_end(ap); |
| 402 | } |
| 403 | |
| 404 | #endif |
| 405 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 406 | void |
| 407 | ly_log(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, const char *format, ...) |
| 408 | { |
| 409 | va_list ap; |
| 410 | |
| 411 | va_start(ap, format); |
| 412 | log_vprintf(ctx, level, no, 0, NULL, format, ap); |
| 413 | va_end(ap); |
| 414 | } |
| 415 | |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 416 | static LY_ERR |
Radek Krejci | c04f0a2 | 2018-09-21 15:49:45 +0200 | [diff] [blame] | 417 | ly_vlog_build_path(const struct ly_ctx *ctx, enum LY_VLOG_ELEM elem_type, const void *elem, char **path) |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 418 | { |
Radek Krejci | c04f0a2 | 2018-09-21 15:49:45 +0200 | [diff] [blame] | 419 | int rc; |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 420 | |
Radek Krejci | c04f0a2 | 2018-09-21 15:49:45 +0200 | [diff] [blame] | 421 | switch (elem_type) { |
| 422 | case LY_VLOG_STR: |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 423 | *path = strdup(elem); |
Radek Krejci | c04f0a2 | 2018-09-21 15:49:45 +0200 | [diff] [blame] | 424 | LY_CHECK_ERR_RET(!(*path), LOGMEM(ctx), LY_EMEM); |
| 425 | break; |
| 426 | case LY_VLOG_LINE: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 427 | rc = asprintf(path, "Line number %" PRIu64 ".", *((uint64_t *)elem)); |
Radek Krejci | c04f0a2 | 2018-09-21 15:49:45 +0200 | [diff] [blame] | 428 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(ctx), LY_EMEM); |
| 429 | break; |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 430 | case LY_VLOG_LYSC: |
| 431 | *path = lysc_path(elem, LYSC_PATH_LOG, NULL, 0); |
| 432 | LY_CHECK_ERR_RET(!(*path), LOGMEM(ctx), LY_EMEM); |
| 433 | break; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 434 | case LY_VLOG_LYD: |
| 435 | *path = lyd_path(elem, LYD_PATH_LOG, NULL, 0); |
| 436 | LY_CHECK_ERR_RET(!(*path), LOGMEM(ctx), LY_EMEM); |
| 437 | break; |
Radek Krejci | c04f0a2 | 2018-09-21 15:49:45 +0200 | [diff] [blame] | 438 | default: |
| 439 | /* shouldn't be here */ |
| 440 | LOGINT_RET(ctx); |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 441 | } |
| 442 | |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 443 | return LY_SUCCESS; |
| 444 | } |
| 445 | |
| 446 | void |
| 447 | ly_vlog(const struct ly_ctx *ctx, enum LY_VLOG_ELEM elem_type, const void *elem, LY_VECODE code, const char *format, ...) |
| 448 | { |
| 449 | va_list ap; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 450 | char *path = NULL; |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 451 | const struct ly_err_item *first; |
| 452 | |
| 453 | if (path_flag && (elem_type != LY_VLOG_NONE)) { |
| 454 | if (elem_type == LY_VLOG_PREV) { |
| 455 | /* use previous path */ |
| 456 | first = ly_err_first(ctx); |
| 457 | if (first && first->prev->path) { |
| 458 | path = strdup(first->prev->path); |
| 459 | } |
| 460 | } else { |
| 461 | /* print path */ |
| 462 | if (!elem) { |
| 463 | /* top-level */ |
| 464 | path = strdup("/"); |
| 465 | } else { |
Radek Krejci | c04f0a2 | 2018-09-21 15:49:45 +0200 | [diff] [blame] | 466 | ly_vlog_build_path(ctx, elem_type, elem, &path); |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | va_start(ap, format); |
| 472 | log_vprintf(ctx, LY_LLERR, LY_EVALID, code, path, format, ap); |
| 473 | /* path is spent and should not be freed! */ |
| 474 | va_end(ap); |
| 475 | } |
| 476 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 477 | API void |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 478 | lyext_log(const struct lysc_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no, const char *path, const char *format, ...) |
| 479 | { |
| 480 | va_list ap; |
| 481 | char *plugin_msg; |
| 482 | int ret; |
| 483 | |
| 484 | if (ly_log_level < level) { |
| 485 | return; |
| 486 | } |
| 487 | ret = asprintf(&plugin_msg, "Extension plugin \"%s\": %s)", ext->def->plugin->id, format); |
| 488 | if (ret == -1) { |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 489 | LOGMEM(ext->module->ctx); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 490 | return; |
| 491 | } |
| 492 | |
| 493 | va_start(ap, format); |
Radek Krejci | a4614e6 | 2020-05-15 14:19:28 +0200 | [diff] [blame] | 494 | 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] | 495 | va_end(ap); |
| 496 | |
| 497 | free(plugin_msg); |
| 498 | } |
| 499 | |
| 500 | API void |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 501 | ly_err_print(struct ly_err_item *eitem) |
| 502 | { |
| 503 | if (ly_log_opts & LY_LOLOG) { |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 504 | if (log_clb) { |
| 505 | log_clb(eitem->level, eitem->msg, eitem->path); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 506 | } else { |
| 507 | fprintf(stderr, "libyang[%d]: %s%s", eitem->level, eitem->msg, eitem->path ? " " : "\n"); |
| 508 | if (eitem->path) { |
| 509 | fprintf(stderr, "(path: %s)\n", eitem->path); |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | } |