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