blob: 88a4f50bc350cb8a8020dc89082d2d8dff48748b [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 */
145 LY_EINVAL, /**< Invalid value */
146 LY_EEXIST, /**< Item already exists */
147 LY_EINT, /**< Internal error */
148 LY_EVALID, /**< Validation failure */
149 LY_EPLUGIN /**< Error reported by a plugin */
150} LY_ERR;
151
152/**
153 * @typedef LY_VECODE
154 * @brief libyang's codes of validation error. Whenever ly_errno is set to LY_EVALID, the ly_vecode is also set
155 * to the appropriate LY_VECODE value.
156 * @ingroup logger
157 */
158typedef enum {
159 LYVE_SUCCESS = 0, /**< no error */
160} LY_VECODE;
161
162/**
163 * @brief Libyang full error structure.
164 */
165struct ly_err_item {
166 LY_LOG_LEVEL level;
167 LY_ERR no;
168 LY_VECODE vecode;
169 char *msg;
170 char *path;
171 char *apptag;
172 struct ly_err_item *next;
173 struct ly_err_item *prev; /* first item's prev points to the last item */
174};
175
176/**
177 * @brief Get the last (thread, context-specific) validation error code.
178 *
179 * This value is set only if ly_errno is #LY_EVALID.
180 *
181 * @param[in] ctx Relative context.
182 * @return Validation error code.
183 */
184LY_VECODE ly_vecode(const struct ly_ctx *ctx);
185
186/**
187 * @brief Get the last (thread, context-specific) error message. If the coresponding module defined
188 * a specific error message, it will be used instead the default one.
189 *
190 * Sometimes, the error message is extended with path of the element where the problem is.
191 * The path is available via ly_errpath().
192 *
193 * @param[in] ctx Relative context.
194 * @return Text of the last error message, empty string if there is no error.
195 */
196const char *ly_errmsg(const struct ly_ctx *ctx);
197
198/**
199 * @brief Get the last (thread, context-specific) path of the element where was an error.
200 *
201 * The path always corresponds to the error message available via ly_errmsg(), so
202 * whenever a subsequent error message is printed, the path is erased or rewritten.
203 * The path reflects the type of the processed tree - data path for data tree functions
204 * and schema path in case of schema tree functions. In case of processing YIN schema
205 * or XML data, the path can be just XML path. In such a case, the corresponding
206 * ly_vecode (value 1-3) is set.
207 *
208 * @param[in] ctx Relative context.
209 * @return Path of the error element, empty string if error path does not apply to the last error.
210 */
211const char *ly_errpath(const struct ly_ctx *ctx);
212
213/**
214 * @brief Get the last (thread, context-specific) error-app-tag if there was a specific one defined
215 * in the module for the last error.
216 *
217 * The app-tag always corresponds to the error message available via ly_errmsg(), so
218 * whenever a subsequent error message is printed, the app-tag is erased or rewritten.
219 *
220 * @param[in] ctx Relative context.
221 * @return Error-app-tag of the last error, empty string if the error-app-tag does not apply to the last error.
222 */
223const char *ly_errapptag(const struct ly_ctx *ctx);
224
225/**
226 * @brief Get the first (thread, context-specific) generated error structure.
227 *
228 * @param[in] ctx Relative context.
229 * @return First error structure (can be NULL), do not modify!
230 */
231struct ly_err_item *ly_err_first(const struct ly_ctx *ctx);
232
233/**
234 * @brief Print the error structure as if just generated.
235 *
236 * @param[in] eitem Error item structure to print.
237 */
238void ly_err_print(struct ly_err_item *eitem);
239
240/**
241 * @brief Free error structures from a context.
242 *
243 * If \p eitem is not set, free all the error structures.
244 *
245 * @param[in] ctx Relative context.
246 * @param[in] eitem Oldest error structure to remove, optional.
247 */
248void ly_err_clean(struct ly_ctx *ctx, struct ly_err_item *eitem);
249
250/** @} errors */
251
252#ifdef __cplusplus
253}
254#endif
255
256#endif /* LY_LOG_H_ */