Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file log.h |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Logger manipulation routines and error definitions. |
| 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 | |
| 15 | #ifndef LY_LOG_H_ |
| 16 | #define LY_LOG_H_ |
| 17 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 18 | #include <stdint.h> |
| 19 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 20 | #ifdef __cplusplus |
| 21 | extern "C" { |
| 22 | #endif |
| 23 | |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 24 | /* dummy context structure */ |
| 25 | struct ly_ctx; |
| 26 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 27 | /** |
| 28 | * @defgroup log Logger |
| 29 | * @{ |
| 30 | * |
| 31 | * Publicly visible functions and values of the libyang logger. For more |
| 32 | * information, see \ref howtologger. |
| 33 | */ |
| 34 | |
| 35 | /** |
| 36 | * @typedef LY_LOG_LEVEL |
| 37 | * @brief Verbosity levels of the libyang logger. |
| 38 | */ |
| 39 | typedef enum |
| 40 | { |
| 41 | LY_LLERR = 0, /**< Print only error messages, default value. */ |
| 42 | LY_LLWRN = 1, /**< Print error and warning messages. */ |
| 43 | LY_LLVRB = 2, /**< Besides errors and warnings, print some other verbose messages. */ |
| 44 | LY_LLDBG = 3 /**< Print all messages including some development debug messages (be careful, |
| 45 | without subsequently calling ly_verb_dbg() no debug messages will be printed!). */ |
| 46 | } LY_LOG_LEVEL; |
| 47 | |
| 48 | /** |
| 49 | * @brief Set logger verbosity level. |
| 50 | * @param[in] level Verbosity level. |
| 51 | * @return Previous verbosity level. |
| 52 | */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 53 | LY_LOG_LEVEL ly_verb(LY_LOG_LEVEL level); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * @defgroup logopts Logging options |
| 57 | * @ingroup logger |
| 58 | * |
| 59 | * Logging option bits of libyang. |
| 60 | * |
| 61 | * @{ |
| 62 | */ |
| 63 | #define LY_LOLOG 0x01 /**< Log messages normally, using callback if set. If not set, messages will |
| 64 | not be printed by libyang. */ |
| 65 | #define LY_LOSTORE 0x02 /**< Store any generated errors or warnings, never verbose or debug messages. |
| 66 | Note that if #LY_LOLOG is not set then verbose and debug messages are always lost. */ |
| 67 | #define LY_LOSTORE_LAST 0x06 /**< Store any generated errors or warnings but only the last message, always overwrite |
| 68 | the previous one. */ |
| 69 | |
| 70 | /** |
| 71 | * @} |
| 72 | */ |
| 73 | |
| 74 | /** |
| 75 | * @brief Set additional logger options. Default is #LY_LOLOG | #LY_LOSTORE_LAST. |
| 76 | * |
| 77 | * @param[in] opts Bitfield of @ref logopts. |
| 78 | * @return Previous logger options. |
| 79 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 80 | uint32_t ly_log_options(uint32_t opts); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 81 | |
| 82 | #ifndef NDEBUG |
| 83 | |
| 84 | /** |
| 85 | * @defgroup dbggroup Debug message groups |
| 86 | * @ingroup log |
| 87 | * |
| 88 | * Selected displayed debug message groups. |
| 89 | * |
| 90 | * @{ |
| 91 | */ |
| 92 | |
| 93 | #define LY_LDGDICT 0x01 /**< Dictionary additions and deletions. */ |
| 94 | #define LY_LDGYANG 0x02 /**< YANG parser messages. */ |
| 95 | #define LY_LDGYIN 0x04 /**< YIN parser messages. */ |
| 96 | #define LY_LDGXPATH 0x08 /**< XPath parsing end evaluation. */ |
| 97 | #define LY_LDGDIFF 0x10 /**< Diff processing and creation. */ |
| 98 | |
| 99 | /** |
| 100 | * @} |
| 101 | */ |
| 102 | |
| 103 | /** |
| 104 | * @brief Enable specific debugging messages (independent of log level). |
| 105 | * @param[in] dbg_groups Bitfield of enabled debug message groups (see @ref dbggroup). |
| 106 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 107 | void ly_verb_dbg(uint32_t dbg_groups); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 108 | |
| 109 | #endif |
| 110 | |
| 111 | /** |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 112 | * @brief Logger callback. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 113 | * |
| 114 | * !IMPORTANT! If an error has a specific error-app-tag defined in the model, it will NOT be set |
| 115 | * at the time of calling this callback. It will be set right after, so to retrieve it |
| 116 | * it must be checked afterwards with ly_errapptag(). |
| 117 | * |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 118 | * @param[in] level Log level of the message. |
| 119 | * @param[in] msg Message. |
| 120 | * @param[in] path Optional path of the concerned node. |
| 121 | */ |
| 122 | typedef void (*ly_log_clb)(LY_LOG_LEVEL level, const char *msg, const char *path); |
| 123 | |
| 124 | /** |
| 125 | * @brief Set logger callback. |
| 126 | * |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 127 | * @param[in] clb Logging callback. |
| 128 | * @param[in] path flag to resolve and provide path as the third parameter of the callback function. In case of |
| 129 | * validation and some other errors, it can be useful to get the path to the problematic element. Note, |
| 130 | * that according to the tree type and the specific situation, the path can slightly differs (keys |
| 131 | * presence) or it can be NULL, so consider it as an optional parameter. If the flag is 0, libyang will |
| 132 | * not bother with resolving the path. |
| 133 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 134 | void ly_set_log_clb(ly_log_clb clb, uint8_t path); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 135 | |
| 136 | /** |
| 137 | * @brief Get logger callback. |
| 138 | * @return Logger callback (can be NULL). |
| 139 | */ |
Michal Vasko | d808561 | 2020-08-21 12:55:23 +0200 | [diff] [blame] | 140 | ly_log_clb ly_get_log_clb(void); |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 141 | |
| 142 | /** @} log */ |
| 143 | |
| 144 | /** |
| 145 | * @defgroup errors Error information |
| 146 | * @{ |
| 147 | */ |
| 148 | |
| 149 | /** |
| 150 | * @typedef LY_ERR |
| 151 | * @brief libyang's error codes returned by the libyang functions. |
| 152 | */ |
| 153 | typedef enum |
| 154 | { |
| 155 | LY_SUCCESS = 0, /**< no error, not set by functions, included just to complete #LY_ERR enumeration */ |
| 156 | LY_EMEM, /**< Memory allocation failure */ |
Radek Krejci | dc1c7e7 | 2018-09-07 14:58:20 +0200 | [diff] [blame] | 157 | LY_ESYS, /**< System call failure */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 158 | LY_EINVAL, /**< Invalid value */ |
| 159 | LY_EEXIST, /**< Item already exists */ |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 160 | LY_ENOTFOUND, /**< Item does not exists */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 161 | LY_EINT, /**< Internal error */ |
| 162 | LY_EVALID, /**< Validation failure */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 163 | LY_EDENIED, /**< Operation is not allowed */ |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 164 | LY_EINCOMPLETE, /**< The operation did not failed, but for some reason it was not possible to finish it completely. |
| 165 | According to the specific use case, the caller is usually supposed to perform the operation again. */ |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 166 | LY_ENOT, /**< Negative result */ |
Radek Krejci | a4614e6 | 2020-05-15 14:19:28 +0200 | [diff] [blame] | 167 | LY_EOTHER, /**< Unknown error */ |
| 168 | |
| 169 | LY_EPLUGIN = 128/**< Error reported by a plugin - the highest bit in the first byte is set. |
| 170 | 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] | 171 | } LY_ERR; |
| 172 | |
| 173 | /** |
| 174 | * @typedef LY_VECODE |
| 175 | * @brief libyang's codes of validation error. Whenever ly_errno is set to LY_EVALID, the ly_vecode is also set |
| 176 | * to the appropriate LY_VECODE value. |
| 177 | * @ingroup logger |
| 178 | */ |
| 179 | typedef enum { |
| 180 | LYVE_SUCCESS = 0, /**< no error */ |
Radek Krejci | 94aa994 | 2018-09-07 17:12:17 +0200 | [diff] [blame] | 181 | LYVE_SYNTAX, /**< generic syntax error */ |
| 182 | LYVE_SYNTAX_YANG, /**< YANG-related syntax error */ |
David Sedlák | 0a875b4 | 2019-03-07 22:24:05 +0100 | [diff] [blame] | 183 | LYVE_SYNTAX_YIN, /**< YIN-related syntax error */ |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 184 | LYVE_REFERENCE, /**< invalid referencing or using an item */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 185 | LYVE_XPATH, /**< invalid XPath expression */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 186 | LYVE_SEMANTICS, /**< generic semantic error */ |
| 187 | LYVE_SYNTAX_XML, /**< XML-related syntax error */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 188 | LYVE_SYNTAX_JSON, /**< JSON-related syntax error */ |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 189 | LYVE_DATA, /**< YANG data does not reflect some of the module restrictions */ |
Radek Krejci | a4614e6 | 2020-05-15 14:19:28 +0200 | [diff] [blame] | 190 | |
| 191 | LYVE_OTHER /**< Unknown error */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 192 | } LY_VECODE; |
| 193 | |
| 194 | /** |
| 195 | * @brief Libyang full error structure. |
| 196 | */ |
| 197 | struct ly_err_item { |
| 198 | LY_LOG_LEVEL level; |
| 199 | LY_ERR no; |
| 200 | LY_VECODE vecode; |
| 201 | char *msg; |
| 202 | char *path; |
| 203 | char *apptag; |
| 204 | struct ly_err_item *next; |
| 205 | struct ly_err_item *prev; /* first item's prev points to the last item */ |
| 206 | }; |
| 207 | |
| 208 | /** |
| 209 | * @brief Get the last (thread, context-specific) validation error code. |
| 210 | * |
| 211 | * This value is set only if ly_errno is #LY_EVALID. |
| 212 | * |
| 213 | * @param[in] ctx Relative context. |
| 214 | * @return Validation error code. |
| 215 | */ |
| 216 | LY_VECODE ly_vecode(const struct ly_ctx *ctx); |
| 217 | |
| 218 | /** |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 219 | * @brief Get the last (thread, context-specific) error code. |
| 220 | * |
| 221 | * @param[in] ctx Relative context. |
| 222 | * @return LY_ERR value of the last error code. |
| 223 | */ |
| 224 | LY_ERR ly_errcode(const struct ly_ctx *ctx); |
| 225 | |
| 226 | /** |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 227 | * @brief Get the last (thread, context-specific) error message. If the coresponding module defined |
| 228 | * a specific error message, it will be used instead the default one. |
| 229 | * |
| 230 | * Sometimes, the error message is extended with path of the element where the problem is. |
| 231 | * The path is available via ly_errpath(). |
| 232 | * |
| 233 | * @param[in] ctx Relative context. |
| 234 | * @return Text of the last error message, empty string if there is no error. |
| 235 | */ |
| 236 | const char *ly_errmsg(const struct ly_ctx *ctx); |
| 237 | |
| 238 | /** |
| 239 | * @brief Get the last (thread, context-specific) path of the element where was an error. |
| 240 | * |
| 241 | * The path always corresponds to the error message available via ly_errmsg(), so |
| 242 | * whenever a subsequent error message is printed, the path is erased or rewritten. |
| 243 | * The path reflects the type of the processed tree - data path for data tree functions |
| 244 | * and schema path in case of schema tree functions. In case of processing YIN schema |
| 245 | * or XML data, the path can be just XML path. In such a case, the corresponding |
| 246 | * ly_vecode (value 1-3) is set. |
| 247 | * |
| 248 | * @param[in] ctx Relative context. |
| 249 | * @return Path of the error element, empty string if error path does not apply to the last error. |
| 250 | */ |
| 251 | const char *ly_errpath(const struct ly_ctx *ctx); |
| 252 | |
| 253 | /** |
| 254 | * @brief Get the last (thread, context-specific) error-app-tag if there was a specific one defined |
| 255 | * in the module for the last error. |
| 256 | * |
| 257 | * The app-tag always corresponds to the error message available via ly_errmsg(), so |
| 258 | * whenever a subsequent error message is printed, the app-tag is erased or rewritten. |
| 259 | * |
| 260 | * @param[in] ctx Relative context. |
| 261 | * @return Error-app-tag of the last error, empty string if the error-app-tag does not apply to the last error. |
| 262 | */ |
| 263 | const char *ly_errapptag(const struct ly_ctx *ctx); |
| 264 | |
| 265 | /** |
| 266 | * @brief Get the first (thread, context-specific) generated error structure. |
| 267 | * |
| 268 | * @param[in] ctx Relative context. |
| 269 | * @return First error structure (can be NULL), do not modify! |
| 270 | */ |
| 271 | struct ly_err_item *ly_err_first(const struct ly_ctx *ctx); |
| 272 | |
| 273 | /** |
| 274 | * @brief Print the error structure as if just generated. |
| 275 | * |
| 276 | * @param[in] eitem Error item structure to print. |
| 277 | */ |
| 278 | void ly_err_print(struct ly_err_item *eitem); |
| 279 | |
| 280 | /** |
| 281 | * @brief Free error structures from a context. |
| 282 | * |
| 283 | * If \p eitem is not set, free all the error structures. |
| 284 | * |
| 285 | * @param[in] ctx Relative context. |
| 286 | * @param[in] eitem Oldest error structure to remove, optional. |
| 287 | */ |
| 288 | void ly_err_clean(struct ly_ctx *ctx, struct ly_err_item *eitem); |
| 289 | |
| 290 | /** @} errors */ |
| 291 | |
| 292 | #ifdef __cplusplus |
| 293 | } |
| 294 | #endif |
| 295 | |
| 296 | #endif /* LY_LOG_H_ */ |