blob: 0d679188783dee26fb67e94725a8a00401257b7b [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
18#include "libyang.h"
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
24/**
25 * @defgroup log Logger
26 * @{
27 *
28 * Publicly visible functions and values of the libyang logger. For more
29 * information, see \ref howtologger.
30 */
31
32/**
33 * @typedef LY_LOG_LEVEL
34 * @brief Verbosity levels of the libyang logger.
35 */
36typedef enum
37{
38 LY_LLERR = 0, /**< Print only error messages, default value. */
39 LY_LLWRN = 1, /**< Print error and warning messages. */
40 LY_LLVRB = 2, /**< Besides errors and warnings, print some other verbose messages. */
41 LY_LLDBG = 3 /**< Print all messages including some development debug messages (be careful,
42 without subsequently calling ly_verb_dbg() no debug messages will be printed!). */
43} LY_LOG_LEVEL;
44
45/**
46 * @brief Set logger verbosity level.
47 * @param[in] level Verbosity level.
48 * @return Previous verbosity level.
49 */
50LY_LOG_LEVEL ly_verb (LY_LOG_LEVEL level);
51
52/**
53 * @defgroup logopts Logging options
54 * @ingroup logger
55 *
56 * Logging option bits of libyang.
57 *
58 * @{
59 */
60#define LY_LOLOG 0x01 /**< Log messages normally, using callback if set. If not set, messages will
61 not be printed by libyang. */
62#define LY_LOSTORE 0x02 /**< Store any generated errors or warnings, never verbose or debug messages.
63 Note that if #LY_LOLOG is not set then verbose and debug messages are always lost. */
64#define LY_LOSTORE_LAST 0x06 /**< Store any generated errors or warnings but only the last message, always overwrite
65 the previous one. */
66
67/**
68 * @}
69 */
70
71/**
72 * @brief Set additional logger options. Default is #LY_LOLOG | #LY_LOSTORE_LAST.
73 *
74 * @param[in] opts Bitfield of @ref logopts.
75 * @return Previous logger options.
76 */
77int ly_log_options (int opts);
78
79#ifndef NDEBUG
80
81/**
82 * @defgroup dbggroup Debug message groups
83 * @ingroup log
84 *
85 * Selected displayed debug message groups.
86 *
87 * @{
88 */
89
90#define LY_LDGDICT 0x01 /**< Dictionary additions and deletions. */
91#define LY_LDGYANG 0x02 /**< YANG parser messages. */
92#define LY_LDGYIN 0x04 /**< YIN parser messages. */
93#define LY_LDGXPATH 0x08 /**< XPath parsing end evaluation. */
94#define LY_LDGDIFF 0x10 /**< Diff processing and creation. */
95
96/**
97 * @}
98 */
99
100/**
101 * @brief Enable specific debugging messages (independent of log level).
102 * @param[in] dbg_groups Bitfield of enabled debug message groups (see @ref dbggroup).
103 */
104void ly_verb_dbg (int dbg_groups);
105
106#endif
107
108/**
109 * @brief Set logger callback.
110 *
111 * !IMPORTANT! If an error has a specific error-app-tag defined in the model, it will NOT be set
112 * at the time of calling this callback. It will be set right after, so to retrieve it
113 * it must be checked afterwards with ly_errapptag().
114 *
115 * @param[in] clb Logging callback.
116 * @param[in] path flag to resolve and provide path as the third parameter of the callback function. In case of
117 * validation and some other errors, it can be useful to get the path to the problematic element. Note,
118 * that according to the tree type and the specific situation, the path can slightly differs (keys
119 * presence) or it can be NULL, so consider it as an optional parameter. If the flag is 0, libyang will
120 * not bother with resolving the path.
121 */
122void ly_set_log_clb (void(*clb) (LY_LOG_LEVEL level, const char *msg, const char *path), int path);
123
124/**
125 * @brief Get logger callback.
126 * @return Logger callback (can be NULL).
127 */
128void (*ly_get_log_clb (void)) (LY_LOG_LEVEL, const char *, const char *);
129
130/** @} log */
131
132/**
133 * @defgroup errors Error information
134 * @{
135 */
136
137/**
138 * @typedef LY_ERR
139 * @brief libyang's error codes returned by the libyang functions.
140 */
141typedef enum
142{
143 LY_SUCCESS = 0, /**< no error, not set by functions, included just to complete #LY_ERR enumeration */
144 LY_EMEM, /**< Memory allocation failure */
Radek Krejcidc1c7e72018-09-07 14:58:20 +0200145 LY_ESYS, /**< System call failure */
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200146 LY_EINVAL, /**< Invalid value */
147 LY_EEXIST, /**< Item already exists */
148 LY_EINT, /**< Internal error */
149 LY_EVALID, /**< Validation failure */
150 LY_EPLUGIN /**< Error reported by a plugin */
151} LY_ERR;
152
153/**
154 * @typedef LY_VECODE
155 * @brief libyang's codes of validation error. Whenever ly_errno is set to LY_EVALID, the ly_vecode is also set
156 * to the appropriate LY_VECODE value.
157 * @ingroup logger
158 */
159typedef enum {
160 LYVE_SUCCESS = 0, /**< no error */
161} LY_VECODE;
162
163/**
164 * @brief Libyang full error structure.
165 */
166struct ly_err_item {
167 LY_LOG_LEVEL level;
168 LY_ERR no;
169 LY_VECODE vecode;
170 char *msg;
171 char *path;
172 char *apptag;
173 struct ly_err_item *next;
174 struct ly_err_item *prev; /* first item's prev points to the last item */
175};
176
177/**
178 * @brief Get the last (thread, context-specific) validation error code.
179 *
180 * This value is set only if ly_errno is #LY_EVALID.
181 *
182 * @param[in] ctx Relative context.
183 * @return Validation error code.
184 */
185LY_VECODE ly_vecode(const struct ly_ctx *ctx);
186
187/**
188 * @brief Get the last (thread, context-specific) error message. If the coresponding module defined
189 * a specific error message, it will be used instead the default one.
190 *
191 * Sometimes, the error message is extended with path of the element where the problem is.
192 * The path is available via ly_errpath().
193 *
194 * @param[in] ctx Relative context.
195 * @return Text of the last error message, empty string if there is no error.
196 */
197const char *ly_errmsg(const struct ly_ctx *ctx);
198
199/**
200 * @brief Get the last (thread, context-specific) path of the element where was an error.
201 *
202 * The path always corresponds to the error message available via ly_errmsg(), so
203 * whenever a subsequent error message is printed, the path is erased or rewritten.
204 * The path reflects the type of the processed tree - data path for data tree functions
205 * and schema path in case of schema tree functions. In case of processing YIN schema
206 * or XML data, the path can be just XML path. In such a case, the corresponding
207 * ly_vecode (value 1-3) is set.
208 *
209 * @param[in] ctx Relative context.
210 * @return Path of the error element, empty string if error path does not apply to the last error.
211 */
212const char *ly_errpath(const struct ly_ctx *ctx);
213
214/**
215 * @brief Get the last (thread, context-specific) error-app-tag if there was a specific one defined
216 * in the module for the last error.
217 *
218 * The app-tag always corresponds to the error message available via ly_errmsg(), so
219 * whenever a subsequent error message is printed, the app-tag is erased or rewritten.
220 *
221 * @param[in] ctx Relative context.
222 * @return Error-app-tag of the last error, empty string if the error-app-tag does not apply to the last error.
223 */
224const char *ly_errapptag(const struct ly_ctx *ctx);
225
226/**
227 * @brief Get the first (thread, context-specific) generated error structure.
228 *
229 * @param[in] ctx Relative context.
230 * @return First error structure (can be NULL), do not modify!
231 */
232struct ly_err_item *ly_err_first(const struct ly_ctx *ctx);
233
234/**
235 * @brief Print the error structure as if just generated.
236 *
237 * @param[in] eitem Error item structure to print.
238 */
239void ly_err_print(struct ly_err_item *eitem);
240
241/**
242 * @brief Free error structures from a context.
243 *
244 * If \p eitem is not set, free all the error structures.
245 *
246 * @param[in] ctx Relative context.
247 * @param[in] eitem Oldest error structure to remove, optional.
248 */
249void ly_err_clean(struct ly_ctx *ctx, struct ly_err_item *eitem);
250
251/** @} errors */
252
253#ifdef __cplusplus
254}
255#endif
256
257#endif /* LY_LOG_H_ */