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