]>
Commit | Line | Data |
---|---|---|
b08024a8 | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
b08024a8 | 3 | * |
0ae67ff7 | 4 | * Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de> |
b08024a8 UH |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
21 | #include <stdarg.h> | |
22 | #include <stdio.h> | |
45c59c8b | 23 | #include "libsigrok.h" |
bdf6c50c | 24 | /** @cond PRIVATE */ |
3544f848 | 25 | #define NO_LOG_WRAPPERS |
bdf6c50c | 26 | /** @endcond */ |
45c59c8b | 27 | #include "libsigrok-internal.h" |
b08024a8 | 28 | |
b4bd7088 UH |
29 | /** |
30 | * @file | |
31 | * | |
393fb9cb | 32 | * Controlling the libsigrok message logging functionality. |
b4bd7088 UH |
33 | */ |
34 | ||
7b870c38 UH |
35 | /** |
36 | * @defgroup grp_logging Logging | |
37 | * | |
38 | * Controlling the libsigrok message logging functionality. | |
39 | * | |
40 | * @{ | |
41 | */ | |
42 | ||
0ae67ff7 | 43 | /* Currently selected libsigrok loglevel. Default: SR_LOG_WARN. */ |
f897acae | 44 | static int cur_loglevel = SR_LOG_WARN; /* Show errors+warnings per default. */ |
1352eedd | 45 | |
0ae67ff7 | 46 | /* Function prototype. */ |
1f9813eb UH |
47 | static int sr_logv(void *cb_data, int loglevel, const char *format, |
48 | va_list args); | |
0ae67ff7 | 49 | |
b5118d6c | 50 | /* Pointer to the currently selected log callback. Default: sr_logv(). */ |
144f6660 | 51 | static sr_log_callback sr_log_cb = sr_logv; |
0ae67ff7 UH |
52 | |
53 | /* | |
b5118d6c | 54 | * Pointer to private data that can be passed to the log callback. |
0ae67ff7 UH |
55 | * This can be used (for example) by C++ GUIs to pass a "this" pointer. |
56 | */ | |
144f6660 | 57 | static void *sr_log_cb_data = NULL; |
0ae67ff7 UH |
58 | |
59 | /* Log domain (a short string that is used as prefix for all messages). */ | |
b4bd7088 | 60 | /** @cond PRIVATE */ |
0ae67ff7 UH |
61 | #define LOGDOMAIN_MAXLEN 30 |
62 | #define LOGDOMAIN_DEFAULT "sr: " | |
b4bd7088 | 63 | /** @endcond */ |
0ae67ff7 UH |
64 | static char sr_log_domain[LOGDOMAIN_MAXLEN + 1] = LOGDOMAIN_DEFAULT; |
65 | ||
1352eedd UH |
66 | /** |
67 | * Set the libsigrok loglevel. | |
68 | * | |
69 | * This influences the amount of log messages (debug messages, error messages, | |
70 | * and so on) libsigrok will output. Using SR_LOG_NONE disables all messages. | |
71 | * | |
0ae67ff7 UH |
72 | * Note that this function itself will also output log messages. After the |
73 | * loglevel has changed, it will output a debug message with SR_LOG_DBG for | |
74 | * example. Whether this message is shown depends on the (new) loglevel. | |
75 | * | |
1352eedd | 76 | * @param loglevel The loglevel to set (SR_LOG_NONE, SR_LOG_ERR, SR_LOG_WARN, |
06dd80d4 | 77 | * SR_LOG_INFO, SR_LOG_DBG, or SR_LOG_SPEW). |
44dae539 | 78 | * |
1352eedd | 79 | * @return SR_OK upon success, SR_ERR_ARG upon invalid loglevel. |
9fb5f2df UH |
80 | * |
81 | * @since 0.1.0 | |
1352eedd | 82 | */ |
0ae67ff7 | 83 | SR_API int sr_log_loglevel_set(int loglevel) |
1352eedd | 84 | { |
06dd80d4 | 85 | if (loglevel < SR_LOG_NONE || loglevel > SR_LOG_SPEW) { |
0ae67ff7 | 86 | sr_err("Invalid loglevel %d.", loglevel); |
1352eedd UH |
87 | return SR_ERR_ARG; |
88 | } | |
89 | ||
f897acae | 90 | cur_loglevel = loglevel; |
1352eedd | 91 | |
0ae67ff7 | 92 | sr_dbg("libsigrok loglevel set to %d.", loglevel); |
1352eedd UH |
93 | |
94 | return SR_OK; | |
95 | } | |
96 | ||
97 | /** | |
98 | * Get the libsigrok loglevel. | |
99 | * | |
100 | * @return The currently configured libsigrok loglevel. | |
9fb5f2df UH |
101 | * |
102 | * @since 0.1.0 | |
1352eedd | 103 | */ |
0ae67ff7 | 104 | SR_API int sr_log_loglevel_get(void) |
1352eedd | 105 | { |
f897acae | 106 | return cur_loglevel; |
1352eedd UH |
107 | } |
108 | ||
0ae67ff7 UH |
109 | /** |
110 | * Set the libsigrok logdomain string. | |
111 | * | |
112 | * @param logdomain The string to use as logdomain for libsigrok log | |
113 | * messages from now on. Must not be NULL. The maximum | |
114 | * length of the string is 30 characters (this does not | |
115 | * include the trailing NUL-byte). Longer strings are | |
116 | * silently truncated. | |
117 | * In order to not use a logdomain, pass an empty string. | |
118 | * The function makes its own copy of the input string, i.e. | |
119 | * the caller does not need to keep it around. | |
44dae539 | 120 | * |
0ae67ff7 | 121 | * @return SR_OK upon success, SR_ERR_ARG upon invalid logdomain. |
9fb5f2df UH |
122 | * |
123 | * @since 0.1.0 | |
0ae67ff7 UH |
124 | */ |
125 | SR_API int sr_log_logdomain_set(const char *logdomain) | |
126 | { | |
127 | if (!logdomain) { | |
128 | sr_err("log: %s: logdomain was NULL", __func__); | |
129 | return SR_ERR_ARG; | |
130 | } | |
131 | ||
132 | /* TODO: Error handling. */ | |
133 | snprintf((char *)&sr_log_domain, LOGDOMAIN_MAXLEN, "%s", logdomain); | |
134 | ||
135 | sr_dbg("Log domain set to '%s'.", (const char *)&sr_log_domain); | |
136 | ||
137 | return SR_OK; | |
138 | } | |
139 | ||
140 | /** | |
141 | * Get the currently configured libsigrok logdomain. | |
142 | * | |
143 | * @return A copy of the currently configured libsigrok logdomain | |
144 | * string. The caller is responsible for g_free()ing the string when | |
145 | * it is no longer needed. | |
9fb5f2df UH |
146 | * |
147 | * @since 0.1.0 | |
0ae67ff7 UH |
148 | */ |
149 | SR_API char *sr_log_logdomain_get(void) | |
150 | { | |
151 | return g_strdup((const char *)&sr_log_domain); | |
152 | } | |
153 | ||
154 | /** | |
b5118d6c | 155 | * Set the libsigrok log callback to the specified function. |
0ae67ff7 | 156 | * |
b5118d6c UH |
157 | * @param cb Function pointer to the log callback function to use. |
158 | * Must not be NULL. | |
1f9813eb UH |
159 | * @param cb_data Pointer to private data to be passed on. This can be used by |
160 | * the caller to pass arbitrary data to the log functions. This | |
161 | * pointer is only stored or passed on by libsigrok, and is | |
162 | * never used or interpreted in any way. The pointer is allowed | |
163 | * to be NULL if the caller doesn't need/want to pass any data. | |
44dae539 | 164 | * |
0ae67ff7 | 165 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments. |
9fb5f2df | 166 | * |
47117241 | 167 | * @since 0.3.0 |
0ae67ff7 | 168 | */ |
144f6660 | 169 | SR_API int sr_log_callback_set(sr_log_callback cb, void *cb_data) |
0ae67ff7 | 170 | { |
b5118d6c UH |
171 | if (!cb) { |
172 | sr_err("log: %s: cb was NULL", __func__); | |
0ae67ff7 UH |
173 | return SR_ERR_ARG; |
174 | } | |
175 | ||
1f9813eb | 176 | /* Note: 'cb_data' is allowed to be NULL. */ |
0ae67ff7 | 177 | |
144f6660 UH |
178 | sr_log_cb = cb; |
179 | sr_log_cb_data = cb_data; | |
0ae67ff7 UH |
180 | |
181 | return SR_OK; | |
182 | } | |
183 | ||
184 | /** | |
b5118d6c | 185 | * Set the libsigrok log callback to the default built-in one. |
0ae67ff7 | 186 | * |
144f6660 | 187 | * Additionally, the internal 'sr_log_cb_data' pointer is set to NULL. |
0ae67ff7 UH |
188 | * |
189 | * @return SR_OK upon success, a negative error code otherwise. | |
9fb5f2df UH |
190 | * |
191 | * @since 0.1.0 | |
0ae67ff7 | 192 | */ |
b5118d6c | 193 | SR_API int sr_log_callback_set_default(void) |
0ae67ff7 UH |
194 | { |
195 | /* | |
196 | * Note: No log output in this function, as it should safely work | |
b5118d6c | 197 | * even if the currently set log callback is buggy/broken. |
0ae67ff7 | 198 | */ |
144f6660 UH |
199 | sr_log_cb = sr_logv; |
200 | sr_log_cb_data = NULL; | |
0ae67ff7 UH |
201 | |
202 | return SR_OK; | |
203 | } | |
204 | ||
1f9813eb | 205 | static int sr_logv(void *cb_data, int loglevel, const char *format, va_list args) |
b08024a8 UH |
206 | { |
207 | int ret; | |
208 | ||
b5118d6c | 209 | /* This specific log callback doesn't need the void pointer data. */ |
1f9813eb | 210 | (void)cb_data; |
0ae67ff7 | 211 | |
1352eedd | 212 | /* Only output messages of at least the selected loglevel(s). */ |
f897acae | 213 | if (loglevel > cur_loglevel) |
1352eedd | 214 | return SR_OK; /* TODO? */ |
b08024a8 | 215 | |
0ae67ff7 UH |
216 | if (sr_log_domain[0] != '\0') |
217 | fprintf(stderr, "%s", sr_log_domain); | |
b08024a8 UH |
218 | ret = vfprintf(stderr, format, args); |
219 | fprintf(stderr, "\n"); | |
220 | ||
221 | return ret; | |
222 | } | |
223 | ||
b4bd7088 | 224 | /** @private */ |
1a081ca6 | 225 | SR_PRIV int sr_log(int loglevel, const char *format, ...) |
b08024a8 UH |
226 | { |
227 | int ret; | |
228 | va_list args; | |
229 | ||
230 | va_start(args, format); | |
144f6660 | 231 | ret = sr_log_cb(sr_log_cb_data, loglevel, format, args); |
b08024a8 UH |
232 | va_end(args); |
233 | ||
234 | return ret; | |
235 | } | |
236 | ||
b4bd7088 | 237 | /** @private */ |
1a081ca6 | 238 | SR_PRIV int sr_spew(const char *format, ...) |
06dd80d4 UH |
239 | { |
240 | int ret; | |
241 | va_list args; | |
242 | ||
243 | va_start(args, format); | |
144f6660 | 244 | ret = sr_log_cb(sr_log_cb_data, SR_LOG_SPEW, format, args); |
06dd80d4 UH |
245 | va_end(args); |
246 | ||
247 | return ret; | |
248 | } | |
249 | ||
b4bd7088 | 250 | /** @private */ |
1a081ca6 | 251 | SR_PRIV int sr_dbg(const char *format, ...) |
b08024a8 UH |
252 | { |
253 | int ret; | |
254 | va_list args; | |
255 | ||
256 | va_start(args, format); | |
144f6660 | 257 | ret = sr_log_cb(sr_log_cb_data, SR_LOG_DBG, format, args); |
b08024a8 UH |
258 | va_end(args); |
259 | ||
260 | return ret; | |
261 | } | |
262 | ||
b4bd7088 | 263 | /** @private */ |
1a081ca6 | 264 | SR_PRIV int sr_info(const char *format, ...) |
b08024a8 UH |
265 | { |
266 | int ret; | |
267 | va_list args; | |
268 | ||
269 | va_start(args, format); | |
144f6660 | 270 | ret = sr_log_cb(sr_log_cb_data, SR_LOG_INFO, format, args); |
b08024a8 UH |
271 | va_end(args); |
272 | ||
273 | return ret; | |
274 | } | |
275 | ||
b4bd7088 | 276 | /** @private */ |
1a081ca6 | 277 | SR_PRIV int sr_warn(const char *format, ...) |
b08024a8 UH |
278 | { |
279 | int ret; | |
280 | va_list args; | |
281 | ||
282 | va_start(args, format); | |
144f6660 | 283 | ret = sr_log_cb(sr_log_cb_data, SR_LOG_WARN, format, args); |
b08024a8 UH |
284 | va_end(args); |
285 | ||
286 | return ret; | |
287 | } | |
288 | ||
b4bd7088 | 289 | /** @private */ |
1a081ca6 | 290 | SR_PRIV int sr_err(const char *format, ...) |
b08024a8 UH |
291 | { |
292 | int ret; | |
293 | va_list args; | |
294 | ||
295 | va_start(args, format); | |
144f6660 | 296 | ret = sr_log_cb(sr_log_cb_data, SR_LOG_ERR, format, args); |
b08024a8 UH |
297 | va_end(args); |
298 | ||
299 | return ret; | |
300 | } | |
7b870c38 UH |
301 | |
302 | /** @} */ |