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