Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file log.h |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 4 | * @author Michal Vasko <mvasko@cesnet.cz> |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 5 | * @brief Logger manipulation routines and error definitions. |
| 6 | * |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 7 | * Copyright (c) 2015 - 2022 CESNET, z.s.p.o. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 8 | * |
| 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * https://opensource.org/licenses/BSD-3-Clause |
| 14 | */ |
| 15 | |
| 16 | #ifndef LY_LOG_H_ |
| 17 | #define LY_LOG_H_ |
| 18 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 21 | #include "config.h" |
| 22 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 23 | #ifdef __cplusplus |
| 24 | extern "C" { |
| 25 | #endif |
| 26 | |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 27 | /* dummy context structure */ |
| 28 | struct ly_ctx; |
| 29 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 30 | /** |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 31 | * @brief Type to indicate boolean value. |
| 32 | * |
| 33 | * Do not test for actual value. Instead, handle it as true/false value in condition. |
| 34 | */ |
| 35 | typedef uint8_t ly_bool; |
| 36 | |
| 37 | /** |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 38 | * @page howtoLogger Information Logging |
| 39 | * |
| 40 | * The libyang logger is supposed to process all the messages (and some other accompanied information) generated by the performed |
| 41 | * functions. According to the logger settings, the information can be printed, stored or further processed by a callback |
| 42 | * functions. |
| 43 | * |
| 44 | * The logger is tightly connected with [errors handling](@ref howtoErrors), because when an error appears, the logger (according |
| 45 | * to [logger options](@ref logopts)) generates error records available via libyang context. |
| 46 | * |
| 47 | * There are 4 verbosity levels defined as ::LY_LOG_LEVEL. The level can be changed by the ::ly_log_level() function. |
| 48 | * By default, the verbosity level is set to #LY_LLERR value, so only the errors are processed. |
| 49 | * |
| 50 | * By default, all libyang messages are printed to `stderr`. However, the callers are able to set their own logging callback |
| 51 | * function (::ly_log_clb). In that case, instead of printing messages, libyang passes error level, message and path (if any) to |
| 52 | * the caller's callback function set via ::ly_set_log_clb(). In case of error level, the error information is still |
| 53 | * automatically stored and available via the [error handling functions](@ref howtoErrors). |
| 54 | * |
| 55 | * With [logging options](@ref logopts) set via ::ly_log_options(), the caller can modify what is done with all the messages. |
| 56 | * Default flags are ::LY_LOLOG and ::LY_LOSTORE_LAST so that messages are logged and the last one is stored. If you set the flag |
| 57 | * ::LY_LOSTORE, all the messages will be stored. Be careful because unless you regularly clean them, the error list in context |
| 58 | * will grow indefinitely. |
| 59 | * |
| 60 | * As a separate group, there are @ref dbggroup to select group of debugging messages to print. The options can be set via |
| 61 | * ::ly_log_dbg_groups() function, but note that the options take effect only in case the libyang is compiled in |
Radek Krejci | 7132fc5 | 2020-10-26 14:34:06 +0100 | [diff] [blame] | 62 | * [Debug build mode](@ref build). |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 63 | * |
| 64 | * \note API for this group of functions is described in the [logger module](@ref log). |
| 65 | * |
| 66 | * Functions List |
| 67 | * -------------- |
| 68 | * - ::ly_log_level() |
| 69 | * - ::ly_log_dbg_groups() |
| 70 | * - ::ly_log_options() |
| 71 | * - ::ly_set_log_clb() |
| 72 | * - ::ly_get_log_clb() |
| 73 | * |
| 74 | */ |
| 75 | |
| 76 | /** |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 77 | * @defgroup log Logger |
| 78 | * @{ |
| 79 | * |
| 80 | * Publicly visible functions and values of the libyang logger. For more |
Michal Vasko | 8906ac1 | 2021-11-25 11:14:59 +0100 | [diff] [blame] | 81 | * information, see @ref howtoLogger. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 82 | */ |
| 83 | |
| 84 | /** |
| 85 | * @typedef LY_LOG_LEVEL |
| 86 | * @brief Verbosity levels of the libyang logger. |
| 87 | */ |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 88 | typedef enum { |
Michal Vasko | 8906ac1 | 2021-11-25 11:14:59 +0100 | [diff] [blame] | 89 | LY_LLERR = 0, /**< Print only error messages. */ |
| 90 | LY_LLWRN = 1, /**< Print error and warning messages, default value. */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 91 | LY_LLVRB = 2, /**< Besides errors and warnings, print some other verbose messages. */ |
Michal Vasko | 8906ac1 | 2021-11-25 11:14:59 +0100 | [diff] [blame] | 92 | LY_LLDBG = 3 /**< Print all messages including some development debug messages (be careful, |
| 93 | without subsequently calling ::ly_log_dbg_groups() no debug messages will be printed!). */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 94 | } LY_LOG_LEVEL; |
| 95 | |
| 96 | /** |
| 97 | * @brief Set logger verbosity level. |
Radek Krejci | ebdaed0 | 2020-11-09 13:05:06 +0100 | [diff] [blame] | 98 | * |
aPiecek | b0445f2 | 2021-06-24 11:34:07 +0200 | [diff] [blame] | 99 | * To get the current value, the function must be called twice resetting the level by the received value. |
Radek Krejci | ebdaed0 | 2020-11-09 13:05:06 +0100 | [diff] [blame] | 100 | * |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 101 | * @param[in] level Verbosity level. |
| 102 | * @return Previous verbosity level. |
| 103 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 104 | LIBYANG_API_DECL LY_LOG_LEVEL ly_log_level(LY_LOG_LEVEL level); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 105 | |
| 106 | /** |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 107 | * @ingroup logger |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 108 | * @defgroup logopts Logging options |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 109 | * |
| 110 | * Logging option bits of libyang. |
| 111 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 112 | * Can be set via ::ly_log_options(). |
| 113 | * |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 114 | * @{ |
| 115 | */ |
| 116 | #define LY_LOLOG 0x01 /**< Log messages normally, using callback if set. If not set, messages will |
| 117 | not be printed by libyang. */ |
| 118 | #define LY_LOSTORE 0x02 /**< Store any generated errors or warnings, never verbose or debug messages. |
| 119 | Note that if #LY_LOLOG is not set then verbose and debug messages are always lost. */ |
| 120 | #define LY_LOSTORE_LAST 0x06 /**< Store any generated errors or warnings but only the last message, always overwrite |
| 121 | the previous one. */ |
| 122 | |
| 123 | /** |
| 124 | * @} |
| 125 | */ |
| 126 | |
| 127 | /** |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 128 | * @brief Set logger options. Default is #LY_LOLOG | #LY_LOSTORE_LAST. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 129 | * |
aPiecek | b0445f2 | 2021-06-24 11:34:07 +0200 | [diff] [blame] | 130 | * To get the current value, the function must be called twice resetting the level by the received value. |
Radek Krejci | ebdaed0 | 2020-11-09 13:05:06 +0100 | [diff] [blame] | 131 | * |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 132 | * @param[in] opts Bitfield of @ref logopts. |
| 133 | * @return Previous logger options. |
| 134 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 135 | LIBYANG_API_DECL uint32_t ly_log_options(uint32_t opts); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 136 | |
Michal Vasko | d4a6d04 | 2022-12-08 08:34:29 +0100 | [diff] [blame] | 137 | /** |
| 138 | * @brief Set temporary thread-safe logger options overwriting those set by ::ly_log_options(). |
| 139 | * |
| 140 | * @param[in] opts Pointer to the temporary @ref logopts. If NULL, restores the effect of global logger options. |
| 141 | */ |
| 142 | LIBYANG_API_DECL void ly_temp_log_options(uint32_t *opts); |
| 143 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 144 | /** |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 145 | * @ingroup log |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 146 | * @defgroup dbggroup Debug messages groups |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 147 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 148 | * Categories of the debug messages. |
| 149 | * |
| 150 | * Allows to show only the selected group(s) of the debug messages. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 151 | * |
| 152 | * @{ |
| 153 | */ |
| 154 | |
Michal Vasko | e558f79 | 2021-07-28 08:20:15 +0200 | [diff] [blame] | 155 | #define LY_LDGDICT 0x01 /**< Dictionary additions and deletions. */ |
| 156 | #define LY_LDGXPATH 0x02 /**< XPath parsing end evaluation. */ |
| 157 | #define LY_LDGDEPSETS 0x04 /**< Dependency module sets for schema compilation. */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 158 | |
| 159 | /** |
| 160 | * @} |
| 161 | */ |
| 162 | |
| 163 | /** |
| 164 | * @brief Enable specific debugging messages (independent of log level). |
Radek Krejci | ebdaed0 | 2020-11-09 13:05:06 +0100 | [diff] [blame] | 165 | * |
aPiecek | b0445f2 | 2021-06-24 11:34:07 +0200 | [diff] [blame] | 166 | * To get the current value, the function must be called twice resetting the level by the received value. |
Thomas Egerer | dc0c720 | 2023-10-23 15:47:15 +0200 | [diff] [blame] | 167 | * Note: does not have any effect on non-debug (Release) builds |
Radek Krejci | ebdaed0 | 2020-11-09 13:05:06 +0100 | [diff] [blame] | 168 | * |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 169 | * @param[in] dbg_groups Bitfield of enabled debug message groups (see @ref dbggroup). |
Radek Krejci | ebdaed0 | 2020-11-09 13:05:06 +0100 | [diff] [blame] | 170 | * @return Previous options bitfield. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 171 | */ |
aPiecek | 3af0d45 | 2023-05-30 11:40:35 +0200 | [diff] [blame] | 172 | LIBYANG_API_DECL uint32_t ly_log_dbg_groups(uint32_t dbg_groups); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 173 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 174 | /** |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 175 | * @brief Logger callback. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 176 | * |
| 177 | * !IMPORTANT! If an error has a specific error-app-tag defined in the model, it will NOT be set |
| 178 | * at the time of calling this callback. It will be set right after, so to retrieve it |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 179 | * it must be checked afterwards with ::ly_errapptag(). |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 180 | * |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 181 | * @param[in] level Log level of the message. |
| 182 | * @param[in] msg Message. |
| 183 | * @param[in] path Optional path of the concerned node. |
| 184 | */ |
| 185 | typedef void (*ly_log_clb)(LY_LOG_LEVEL level, const char *msg, const char *path); |
| 186 | |
| 187 | /** |
| 188 | * @brief Set logger callback. |
| 189 | * |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 190 | * @param[in] clb Logging callback. |
| 191 | * @param[in] path flag to resolve and provide path as the third parameter of the callback function. In case of |
| 192 | * validation and some other errors, it can be useful to get the path to the problematic element. Note, |
| 193 | * that according to the tree type and the specific situation, the path can slightly differs (keys |
| 194 | * presence) or it can be NULL, so consider it as an optional parameter. If the flag is 0, libyang will |
| 195 | * not bother with resolving the path. |
| 196 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 197 | LIBYANG_API_DECL void ly_set_log_clb(ly_log_clb clb, ly_bool path); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 198 | |
| 199 | /** |
| 200 | * @brief Get logger callback. |
| 201 | * @return Logger callback (can be NULL). |
| 202 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 203 | LIBYANG_API_DECL ly_log_clb ly_get_log_clb(void); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 204 | |
| 205 | /** @} log */ |
| 206 | |
| 207 | /** |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 208 | * @page howtoErrors Errors Handling |
| 209 | * |
| 210 | * The most of the API functions directly returns error code in the form of ::LY_ERR value. In addition, if the ::LY_EVALID error |
| 211 | * arises, additional [validation error code](@ref ::LY_VECODE) is provided to categorize validation failures into several groups. |
| 212 | * |
| 213 | * All the errors arisen in connection with manipulation with the [context](@ref howtoContext), [YANG modules](@ref howtoSchema) |
| 214 | * or [YANG data](@ref howtoData), are recorded into the context and can be examined for the more detailed information. These |
| 215 | * records are stored as ::ly_err_item structures and they are not only context-specific, but also thread-specific. |
| 216 | * |
| 217 | * Storing error information is tightly connected with |
| 218 | * [logging](@ref howtoLogger). So the @ref logopts control if and which errors are stored in the context. By default, only the |
| 219 | * last error is recorded, so a new error replaces the previous one. This can be changed with ::LY_LOSTORE option set via |
| 220 | * ::ly_log_options(). Then, the errors are stored as a list preserving the previous error records. The stored records can be |
| 221 | * accessed using ::ly_err_last() or ::ly_err_first() functions. The ::ly_err_clean() is used to remove error records from the |
| 222 | * context. |
| 223 | * |
| 224 | * To print a specific error information via libyang logger, there is ::ly_err_print(). |
| 225 | * |
| 226 | * To simplify access to the last error record in the context, there is a set of functions returning important error information. |
| 227 | * - ::ly_errapptag() |
| 228 | * - ::ly_errcode() |
| 229 | * - ::ly_vecode() |
| 230 | * - ::ly_errmsg() |
| 231 | * - ::ly_errpath() |
| 232 | * |
| 233 | * \note API for this group of functions is described in the [error information module](@ref errors). |
| 234 | */ |
| 235 | |
| 236 | /** |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 237 | * @defgroup errors Error information |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 238 | * |
| 239 | * Structures and functions to allow error information processing. |
| 240 | * |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 241 | * @{ |
| 242 | */ |
| 243 | |
| 244 | /** |
| 245 | * @typedef LY_ERR |
| 246 | * @brief libyang's error codes returned by the libyang functions. |
| 247 | */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 248 | typedef enum { |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 249 | LY_SUCCESS = 0, /**< no error, not set by functions, included just to complete #LY_ERR enumeration */ |
| 250 | LY_EMEM, /**< Memory allocation failure */ |
Radek Krejci | dc1c7e7 | 2018-09-07 14:58:20 +0200 | [diff] [blame] | 251 | LY_ESYS, /**< System call failure */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 252 | LY_EINVAL, /**< Invalid value */ |
| 253 | LY_EEXIST, /**< Item already exists */ |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 254 | LY_ENOTFOUND, /**< Item does not exists */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 255 | LY_EINT, /**< Internal error */ |
| 256 | LY_EVALID, /**< Validation failure */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 257 | LY_EDENIED, /**< Operation is not allowed */ |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 258 | LY_EINCOMPLETE, /**< The operation did not fail, but for some reason it was not possible to finish it completely. |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 259 | According to the specific use case, the caller is usually supposed to perform the operation again. */ |
Michal Vasko | 1ccbf54 | 2021-04-19 11:35:00 +0200 | [diff] [blame] | 260 | LY_ERECOMPILE, /**< The operation did not fail, but requires context recompilation before it can be completed. |
| 261 | According to the specific use case, the caller should react appropriately. */ |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 262 | LY_ENOT, /**< Negative result */ |
Radek Krejci | a4614e6 | 2020-05-15 14:19:28 +0200 | [diff] [blame] | 263 | LY_EOTHER, /**< Unknown error */ |
| 264 | |
| 265 | LY_EPLUGIN = 128/**< Error reported by a plugin - the highest bit in the first byte is set. |
| 266 | This value is used ORed with one of the other LY_ERR value and can be simply masked. */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 267 | } LY_ERR; |
| 268 | |
| 269 | /** |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 270 | * @ingroup logger |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 271 | * @typedef LY_VECODE |
| 272 | * @brief libyang's codes of validation error. Whenever ly_errno is set to LY_EVALID, the ly_vecode is also set |
| 273 | * to the appropriate LY_VECODE value. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 274 | */ |
| 275 | typedef enum { |
| 276 | LYVE_SUCCESS = 0, /**< no error */ |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 277 | LYVE_SYNTAX, /**< generic syntax error */ |
| 278 | LYVE_SYNTAX_YANG, /**< YANG-related syntax error */ |
David Sedlák | 0a875b4 | 2019-03-07 22:24:05 +0100 | [diff] [blame] | 279 | LYVE_SYNTAX_YIN, /**< YIN-related syntax error */ |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 280 | LYVE_REFERENCE, /**< invalid referencing or using an item */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 281 | LYVE_XPATH, /**< invalid XPath expression */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 282 | LYVE_SEMANTICS, /**< generic semantic error */ |
| 283 | LYVE_SYNTAX_XML, /**< XML-related syntax error */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 284 | LYVE_SYNTAX_JSON, /**< JSON-related syntax error */ |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 285 | LYVE_DATA, /**< YANG data does not reflect some of the module restrictions */ |
Radek Krejci | a4614e6 | 2020-05-15 14:19:28 +0200 | [diff] [blame] | 286 | |
| 287 | LYVE_OTHER /**< Unknown error */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 288 | } LY_VECODE; |
| 289 | |
| 290 | /** |
| 291 | * @brief Libyang full error structure. |
| 292 | */ |
| 293 | struct ly_err_item { |
Michal Vasko | dab9cc9 | 2023-07-10 10:35:42 +0200 | [diff] [blame] | 294 | LY_LOG_LEVEL level; /**< error (message) log level */ |
| 295 | LY_ERR no; /**< error code */ |
| 296 | LY_VECODE vecode; /**< validation error code, if any */ |
| 297 | char *msg; /**< error message */ |
| 298 | char *path; /**< error path that caused the error, if any */ |
| 299 | char *apptag; /**< error-app-tag, if any */ |
| 300 | struct ly_err_item *next; /**< next error item */ |
| 301 | struct ly_err_item *prev; /**< previous error item, points to the last item for the ifrst item */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 302 | }; |
| 303 | |
| 304 | /** |
Michal Vasko | b5b883c | 2023-07-10 10:36:18 +0200 | [diff] [blame] | 305 | * @brief Get the last (thread, context-specific) error code. |
Michal Vasko | 236cbac | 2023-02-10 15:45:37 +0100 | [diff] [blame] | 306 | * |
Michal Vasko | b5b883c | 2023-07-10 10:36:18 +0200 | [diff] [blame] | 307 | * @param[in] ctx Relative context. |
| 308 | * @return LY_ERR value of the last error code. |
Michal Vasko | 236cbac | 2023-02-10 15:45:37 +0100 | [diff] [blame] | 309 | */ |
Michal Vasko | b5b883c | 2023-07-10 10:36:18 +0200 | [diff] [blame] | 310 | LIBYANG_API_DECL LY_ERR ly_errcode(const struct ly_ctx *ctx); |
| 311 | |
| 312 | /** |
| 313 | * @brief Get human-readable error message for an error code. |
| 314 | * |
| 315 | * @param[in] err Error code. |
Michal Vasko | e420765 | 2023-07-10 14:57:32 +0200 | [diff] [blame] | 316 | * @return String error message. |
Michal Vasko | b5b883c | 2023-07-10 10:36:18 +0200 | [diff] [blame] | 317 | */ |
| 318 | LIBYANG_API_DECL const char *ly_strerrcode(LY_ERR err); |
Michal Vasko | 236cbac | 2023-02-10 15:45:37 +0100 | [diff] [blame] | 319 | |
| 320 | /** |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 321 | * @brief Get the last (thread, context-specific) validation error code. |
| 322 | * |
| 323 | * This value is set only if ly_errno is #LY_EVALID. |
| 324 | * |
| 325 | * @param[in] ctx Relative context. |
| 326 | * @return Validation error code. |
| 327 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 328 | LIBYANG_API_DECL LY_VECODE ly_vecode(const struct ly_ctx *ctx); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 329 | |
| 330 | /** |
Michal Vasko | b5b883c | 2023-07-10 10:36:18 +0200 | [diff] [blame] | 331 | * @brief Get human-readable error message for a validation error code. |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 332 | * |
Michal Vasko | b5b883c | 2023-07-10 10:36:18 +0200 | [diff] [blame] | 333 | * @param[in] vecode Validation error code. |
Michal Vasko | e420765 | 2023-07-10 14:57:32 +0200 | [diff] [blame] | 334 | * @return String error message. |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 335 | */ |
Michal Vasko | b5b883c | 2023-07-10 10:36:18 +0200 | [diff] [blame] | 336 | LIBYANG_API_DECL const char *ly_strvecode(LY_VECODE vecode); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 337 | |
| 338 | /** |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 339 | * @brief Get the last (thread, context-specific) error message. If the coresponding module defined |
| 340 | * a specific error message, it will be used instead the default one. |
| 341 | * |
| 342 | * Sometimes, the error message is extended with path of the element where the problem is. |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 343 | * The path is available via ::ly_errpath(). |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 344 | * |
| 345 | * @param[in] ctx Relative context. |
| 346 | * @return Text of the last error message, empty string if there is no error. |
| 347 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 348 | LIBYANG_API_DECL const char *ly_errmsg(const struct ly_ctx *ctx); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 349 | |
| 350 | /** |
Michal Vasko | b5b883c | 2023-07-10 10:36:18 +0200 | [diff] [blame] | 351 | * @brief Get the last (thread-specific) error message. |
| 352 | * |
| 353 | * ::ly_errmsg() should be used instead of this function but this one is useful for getting |
| 354 | * errors from functions that do not have any context accessible. Or as a simple unified logging API. |
| 355 | * |
| 356 | * @return Last generated error message. |
| 357 | */ |
| 358 | LIBYANG_API_DECL const char *ly_last_errmsg(void); |
| 359 | |
| 360 | /** |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 361 | * @brief Get the last (thread, context-specific) path of the element where was an error. |
| 362 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 363 | * The path always corresponds to the error message available via ::ly_errmsg(), so |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 364 | * whenever a subsequent error message is printed, the path is erased or rewritten. |
| 365 | * The path reflects the type of the processed tree - data path for data tree functions |
| 366 | * and schema path in case of schema tree functions. In case of processing YIN schema |
| 367 | * or XML data, the path can be just XML path. In such a case, the corresponding |
| 368 | * ly_vecode (value 1-3) is set. |
| 369 | * |
| 370 | * @param[in] ctx Relative context. |
| 371 | * @return Path of the error element, empty string if error path does not apply to the last error. |
| 372 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 373 | LIBYANG_API_DECL const char *ly_errpath(const struct ly_ctx *ctx); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 374 | |
| 375 | /** |
| 376 | * @brief Get the last (thread, context-specific) error-app-tag if there was a specific one defined |
| 377 | * in the module for the last error. |
| 378 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 379 | * The app-tag always corresponds to the error message available via ::ly_errmsg(), so |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 380 | * whenever a subsequent error message is printed, the app-tag is erased or rewritten. |
| 381 | * |
| 382 | * @param[in] ctx Relative context. |
| 383 | * @return Error-app-tag of the last error, empty string if the error-app-tag does not apply to the last error. |
| 384 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 385 | LIBYANG_API_DECL const char *ly_errapptag(const struct ly_ctx *ctx); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 386 | |
| 387 | /** |
| 388 | * @brief Get the first (thread, context-specific) generated error structure. |
| 389 | * |
| 390 | * @param[in] ctx Relative context. |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 391 | * @return The first error structure (can be NULL), do not modify! |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 392 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 393 | LIBYANG_API_DECL struct ly_err_item *ly_err_first(const struct ly_ctx *ctx); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 394 | |
| 395 | /** |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 396 | * @brief Get the latest (thread, context-specific) generated error structure. |
| 397 | * |
| 398 | * @param[in] ctx Relative context. |
| 399 | * @return The last error structure (can be NULL), do not modify! |
| 400 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 401 | LIBYANG_API_DECL struct ly_err_item *ly_err_last(const struct ly_ctx *ctx); |
Radek Krejci | 572ee60 | 2020-09-16 14:35:08 +0200 | [diff] [blame] | 402 | |
| 403 | /** |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 404 | * @brief Print the error structure as if just generated. |
| 405 | * |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 406 | * @param[in] ctx Optional context to store the message in. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 407 | * @param[in] eitem Error item structure to print. |
| 408 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 409 | LIBYANG_API_DECL void 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] | 410 | |
| 411 | /** |
| 412 | * @brief Free error structures from a context. |
| 413 | * |
| 414 | * If \p eitem is not set, free all the error structures. |
| 415 | * |
| 416 | * @param[in] ctx Relative context. |
| 417 | * @param[in] eitem Oldest error structure to remove, optional. |
| 418 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 419 | LIBYANG_API_DECL void ly_err_clean(struct ly_ctx *ctx, struct ly_err_item *eitem); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 420 | |
| 421 | /** @} errors */ |
| 422 | |
| 423 | #ifdef __cplusplus |
| 424 | } |
| 425 | #endif |
| 426 | |
| 427 | #endif /* LY_LOG_H_ */ |