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 | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 34 | volatile LY_LOG_LEVEL ly_ll = LY_LLWRN; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 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 | 68433c9 | 2020-10-12 17:03:55 +0200 | [diff] [blame] | 39 | volatile uint32_t ly_ldbg_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 |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 196 | ly_log_level(LY_LOG_LEVEL level) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 197 | { |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 198 | LY_LOG_LEVEL prev = ly_ll; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 199 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 200 | ly_ll = level; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 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 | |
Radek Krejci | ebdaed0 | 2020-11-09 13:05:06 +0100 | [diff] [blame^] | 213 | API uint32_t |
Radek Krejci | 68433c9 | 2020-10-12 17:03:55 +0200 | [diff] [blame] | 214 | ly_log_dbg_groups(uint32_t dbg_groups) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 215 | { |
| 216 | #ifndef NDEBUG |
Radek Krejci | ebdaed0 | 2020-11-09 13:05:06 +0100 | [diff] [blame^] | 217 | uint32_t prev = ly_ldbg_groups; |
| 218 | |
Radek Krejci | 68433c9 | 2020-10-12 17:03:55 +0200 | [diff] [blame] | 219 | ly_ldbg_groups = dbg_groups; |
Radek Krejci | ebdaed0 | 2020-11-09 13:05:06 +0100 | [diff] [blame^] | 220 | return prev; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 221 | #else |
| 222 | (void)dbg_groups; |
Radek Krejci | ebdaed0 | 2020-11-09 13:05:06 +0100 | [diff] [blame^] | 223 | return 0; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 224 | #endif |
| 225 | } |
| 226 | |
| 227 | API void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 228 | ly_set_log_clb(ly_log_clb clb, ly_bool path) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 229 | { |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 230 | log_clb = clb; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 231 | path_flag = path; |
| 232 | } |
| 233 | |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 234 | API ly_log_clb |
| 235 | ly_get_log_clb(void) |
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 | return log_clb; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | static LY_ERR |
| 241 | log_store(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, LY_VECODE vecode, char *msg, char *path, char *apptag) |
| 242 | { |
| 243 | struct ly_err_item *eitem, *last; |
| 244 | |
| 245 | assert(ctx && (level < LY_LLVRB)); |
| 246 | |
| 247 | eitem = pthread_getspecific(ctx->errlist_key); |
| 248 | if (!eitem) { |
| 249 | /* if we are only to fill in path, there must have been an error stored */ |
| 250 | assert(msg); |
| 251 | eitem = malloc(sizeof *eitem); |
| 252 | LY_CHECK_GOTO(!eitem, mem_fail); |
| 253 | eitem->prev = eitem; |
| 254 | eitem->next = NULL; |
| 255 | |
| 256 | pthread_setspecific(ctx->errlist_key, eitem); |
| 257 | } else if (!msg) { |
| 258 | /* only filling the path */ |
| 259 | assert(path); |
| 260 | |
| 261 | /* find last error */ |
| 262 | eitem = eitem->prev; |
| 263 | do { |
| 264 | if (eitem->level == LY_LLERR) { |
| 265 | /* fill the path */ |
| 266 | free(eitem->path); |
| 267 | eitem->path = path; |
| 268 | return LY_SUCCESS; |
| 269 | } |
| 270 | eitem = eitem->prev; |
| 271 | } while (eitem->prev->next); |
| 272 | /* last error was not found */ |
| 273 | assert(0); |
Michal Vasko | ed94a29 | 2019-11-06 15:43:41 +0100 | [diff] [blame] | 274 | } else if ((ly_log_opts & LY_LOSTORE_LAST) == LY_LOSTORE_LAST) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 275 | /* overwrite last message */ |
| 276 | free(eitem->msg); |
| 277 | free(eitem->path); |
| 278 | free(eitem->apptag); |
| 279 | } else { |
| 280 | /* store new message */ |
| 281 | last = eitem->prev; |
| 282 | eitem->prev = malloc(sizeof *eitem); |
| 283 | LY_CHECK_GOTO(!eitem->prev, mem_fail); |
| 284 | eitem = eitem->prev; |
| 285 | eitem->prev = last; |
| 286 | eitem->next = NULL; |
| 287 | last->next = eitem; |
| 288 | } |
| 289 | |
| 290 | /* fill in the information */ |
| 291 | eitem->level = level; |
| 292 | eitem->no = no; |
| 293 | eitem->vecode = vecode; |
| 294 | eitem->msg = msg; |
| 295 | eitem->path = path; |
| 296 | eitem->apptag = apptag; |
| 297 | return LY_SUCCESS; |
| 298 | |
| 299 | mem_fail: |
| 300 | LOGMEM(NULL); |
| 301 | free(msg); |
| 302 | free(path); |
| 303 | free(apptag); |
| 304 | return LY_EMEM; |
| 305 | } |
| 306 | |
| 307 | static void |
| 308 | 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] | 309 | const char *format, va_list args) |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 310 | { |
| 311 | char *msg = NULL; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 312 | ly_bool free_strs; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 313 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 314 | if (level > ly_ll) { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 315 | /* do not print or store the message */ |
| 316 | free(path); |
| 317 | return; |
| 318 | } |
| 319 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 320 | /* 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] | 321 | if ((level < LY_LLVRB) && ctx && (ly_log_opts & LY_LOSTORE)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 322 | assert(format); |
| 323 | if (vasprintf(&msg, format, args) == -1) { |
| 324 | LOGMEM(ctx); |
| 325 | free(path); |
| 326 | return; |
| 327 | } |
Radek Krejci | c9e64a6 | 2020-09-18 20:08:12 +0200 | [diff] [blame] | 328 | if (((no & ~LY_EPLUGIN) == LY_EVALID) && (vecode == LYVE_SUCCESS)) { |
| 329 | /* assume we are inheriting the error, so inherit vecode as well */ |
| 330 | vecode = ly_vecode(ctx); |
| 331 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 332 | if (log_store(ctx, level, no, vecode, msg, path, NULL)) { |
| 333 | return; |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 334 | } |
| 335 | free_strs = 0; |
| 336 | } else { |
| 337 | if (vasprintf(&msg, format, args) == -1) { |
| 338 | LOGMEM(ctx); |
| 339 | free(path); |
| 340 | return; |
| 341 | } |
| 342 | free_strs = 1; |
| 343 | } |
| 344 | |
| 345 | /* if we are only storing errors internally, never print the message (yet) */ |
Michal Vasko | ed94a29 | 2019-11-06 15:43:41 +0100 | [diff] [blame] | 346 | if (ly_log_opts & LY_LOLOG) { |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 347 | if (log_clb) { |
| 348 | log_clb(level, msg, path); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 349 | } else { |
| 350 | fprintf(stderr, "libyang[%d]: %s%s", level, msg, path ? " " : "\n"); |
| 351 | if (path) { |
| 352 | fprintf(stderr, "(path: %s)\n", path); |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | if (free_strs) { |
| 358 | free(path); |
| 359 | free(msg); |
| 360 | } |
| 361 | } |
| 362 | |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 363 | #ifndef NDEBUG |
| 364 | |
| 365 | void |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 366 | ly_log_dbg(uint32_t group, const char *format, ...) |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 367 | { |
| 368 | char *dbg_format; |
| 369 | const char *str_group; |
| 370 | va_list ap; |
| 371 | |
Radek Krejci | 68433c9 | 2020-10-12 17:03:55 +0200 | [diff] [blame] | 372 | if (!(ly_ldbg_groups & group)) { |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 373 | return; |
| 374 | } |
| 375 | |
| 376 | switch (group) { |
| 377 | case LY_LDGDICT: |
| 378 | str_group = "DICT"; |
| 379 | break; |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 380 | case LY_LDGXPATH: |
| 381 | str_group = "XPATH"; |
| 382 | break; |
Radek Krejci | 4ab6156 | 2018-09-05 15:00:37 +0200 | [diff] [blame] | 383 | default: |
| 384 | LOGINT(NULL); |
| 385 | return; |
| 386 | } |
| 387 | |
| 388 | if (asprintf(&dbg_format, "%s: %s", str_group, format) == -1) { |
| 389 | LOGMEM(NULL); |
| 390 | return; |
| 391 | } |
| 392 | |
| 393 | va_start(ap, format); |
| 394 | log_vprintf(NULL, LY_LLDBG, 0, 0, NULL, dbg_format, ap); |
| 395 | va_end(ap); |
| 396 | } |
| 397 | |
| 398 | #endif |
| 399 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 400 | void |
| 401 | ly_log(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR no, const char *format, ...) |
| 402 | { |
| 403 | va_list ap; |
| 404 | |
| 405 | va_start(ap, format); |
| 406 | log_vprintf(ctx, level, no, 0, NULL, format, ap); |
| 407 | va_end(ap); |
| 408 | } |
| 409 | |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 410 | static LY_ERR |
Radek Krejci | c04f0a2 | 2018-09-21 15:49:45 +0200 | [diff] [blame] | 411 | 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] | 412 | { |
Radek Krejci | c04f0a2 | 2018-09-21 15:49:45 +0200 | [diff] [blame] | 413 | int rc; |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 414 | |
Radek Krejci | c04f0a2 | 2018-09-21 15:49:45 +0200 | [diff] [blame] | 415 | switch (elem_type) { |
| 416 | case LY_VLOG_STR: |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 417 | *path = strdup(elem); |
Radek Krejci | c04f0a2 | 2018-09-21 15:49:45 +0200 | [diff] [blame] | 418 | LY_CHECK_ERR_RET(!(*path), LOGMEM(ctx), LY_EMEM); |
| 419 | break; |
| 420 | case LY_VLOG_LINE: |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 421 | rc = asprintf(path, "Line number %" PRIu64 ".", *((uint64_t *)elem)); |
Radek Krejci | c04f0a2 | 2018-09-21 15:49:45 +0200 | [diff] [blame] | 422 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(ctx), LY_EMEM); |
| 423 | break; |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 424 | case LY_VLOG_LYSC: |
| 425 | *path = lysc_path(elem, LYSC_PATH_LOG, NULL, 0); |
| 426 | LY_CHECK_ERR_RET(!(*path), LOGMEM(ctx), LY_EMEM); |
| 427 | break; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 428 | case LY_VLOG_LYD: |
| 429 | *path = lyd_path(elem, LYD_PATH_LOG, NULL, 0); |
| 430 | LY_CHECK_ERR_RET(!(*path), LOGMEM(ctx), LY_EMEM); |
| 431 | break; |
Radek Krejci | c04f0a2 | 2018-09-21 15:49:45 +0200 | [diff] [blame] | 432 | default: |
| 433 | /* shouldn't be here */ |
| 434 | LOGINT_RET(ctx); |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 435 | } |
| 436 | |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 437 | return LY_SUCCESS; |
| 438 | } |
| 439 | |
| 440 | void |
| 441 | ly_vlog(const struct ly_ctx *ctx, enum LY_VLOG_ELEM elem_type, const void *elem, LY_VECODE code, const char *format, ...) |
| 442 | { |
| 443 | va_list ap; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 444 | char *path = NULL; |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 445 | const struct ly_err_item *first; |
| 446 | |
| 447 | if (path_flag && (elem_type != LY_VLOG_NONE)) { |
| 448 | if (elem_type == LY_VLOG_PREV) { |
| 449 | /* use previous path */ |
| 450 | first = ly_err_first(ctx); |
| 451 | if (first && first->prev->path) { |
| 452 | path = strdup(first->prev->path); |
| 453 | } |
| 454 | } else { |
| 455 | /* print path */ |
| 456 | if (!elem) { |
| 457 | /* top-level */ |
| 458 | path = strdup("/"); |
| 459 | } else { |
Radek Krejci | c04f0a2 | 2018-09-21 15:49:45 +0200 | [diff] [blame] | 460 | ly_vlog_build_path(ctx, elem_type, elem, &path); |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | va_start(ap, format); |
| 466 | log_vprintf(ctx, LY_LLERR, LY_EVALID, code, path, format, ap); |
| 467 | /* path is spent and should not be freed! */ |
| 468 | va_end(ap); |
| 469 | } |
| 470 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 471 | API void |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 472 | lyext_log(const struct lysc_ext_instance *ext, LY_LOG_LEVEL level, LY_ERR err_no, const char *path, const char *format, ...) |
| 473 | { |
| 474 | va_list ap; |
| 475 | char *plugin_msg; |
| 476 | int ret; |
| 477 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 478 | if (ly_ll < level) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 479 | return; |
| 480 | } |
| 481 | ret = asprintf(&plugin_msg, "Extension plugin \"%s\": %s)", ext->def->plugin->id, format); |
| 482 | if (ret == -1) { |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame] | 483 | LOGMEM(ext->module->ctx); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 484 | return; |
| 485 | } |
| 486 | |
| 487 | va_start(ap, format); |
Radek Krejci | a4614e6 | 2020-05-15 14:19:28 +0200 | [diff] [blame] | 488 | 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] | 489 | va_end(ap); |
| 490 | |
| 491 | free(plugin_msg); |
| 492 | } |
| 493 | |
| 494 | API void |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 495 | ly_err_print(struct ly_err_item *eitem) |
| 496 | { |
| 497 | if (ly_log_opts & LY_LOLOG) { |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 498 | if (log_clb) { |
| 499 | log_clb(eitem->level, eitem->msg, eitem->path); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 500 | } else { |
| 501 | fprintf(stderr, "libyang[%d]: %s%s", eitem->level, eitem->msg, eitem->path ? " " : "\n"); |
| 502 | if (eitem->path) { |
| 503 | fprintf(stderr, "(path: %s)\n", eitem->path); |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | } |