]>
Commit | Line | Data |
---|---|---|
43c0a640 UH |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
66e4c27c | 4 | * Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de> |
43c0a640 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 | ||
41106a07 | 21 | #include "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */ |
43c0a640 | 22 | #include "sigrokdecode-internal.h" |
bc5f5a43 BV |
23 | #include <stdarg.h> |
24 | #include <stdio.h> | |
43c0a640 | 25 | |
54fdeeef UH |
26 | /** |
27 | * @file | |
28 | * | |
29 | * Controlling the libsigrokdecode message logging functionality. | |
30 | */ | |
31 | ||
66e4c27c | 32 | /* Currently selected libsigrokdecode loglevel. Default: SRD_LOG_WARN. */ |
43c0a640 UH |
33 | static int srd_loglevel = SRD_LOG_WARN; /* Show errors+warnings per default. */ |
34 | ||
66e4c27c | 35 | /* Function prototype. */ |
41a5d962 | 36 | static int srd_logv(void *cb_data, int loglevel, const char *format, |
e09023b9 | 37 | va_list args); |
66e4c27c | 38 | |
0082d592 UH |
39 | /* Pointer to the currently selected log callback. Default: srd_logv(). */ |
40 | static srd_log_callback_t srd_log_callback = srd_logv; | |
66e4c27c UH |
41 | |
42 | /* | |
0082d592 | 43 | * Pointer to private data that can be passed to the log callback. |
66e4c27c UH |
44 | * This can be used (for example) by C++ GUIs to pass a "this" pointer. |
45 | */ | |
0082d592 | 46 | static void *srd_log_callback_data = NULL; |
66e4c27c | 47 | |
3a8b2e78 | 48 | /* Log domain (a short string that is used as prefix for all messages). */ |
57790bc8 | 49 | /** @cond PRIVATE */ |
3a8b2e78 UH |
50 | #define LOGDOMAIN_MAXLEN 30 |
51 | #define LOGDOMAIN_DEFAULT "srd: " | |
57790bc8 | 52 | /** @endcond */ |
3a8b2e78 UH |
53 | static char srd_log_domain[LOGDOMAIN_MAXLEN + 1] = LOGDOMAIN_DEFAULT; |
54 | ||
43c0a640 UH |
55 | /** |
56 | * Set the libsigrokdecode loglevel. | |
57 | * | |
58 | * This influences the amount of log messages (debug messages, error messages, | |
59 | * and so on) libsigrokdecode will output. Using SRD_LOG_NONE disables all | |
60 | * messages. | |
61 | * | |
41106a07 UH |
62 | * Note that this function itself will also output log messages. After the |
63 | * loglevel has changed, it will output a debug message with SRD_LOG_DBG for | |
64 | * example. Whether this message is shown depends on the (new) loglevel. | |
65 | * | |
43c0a640 UH |
66 | * @param loglevel The loglevel to set (SRD_LOG_NONE, SRD_LOG_ERR, |
67 | * SRD_LOG_WARN, SRD_LOG_INFO, SRD_LOG_DBG, or SRD_LOG_SPEW). | |
582c8473 | 68 | * |
a62186e4 | 69 | * @return SRD_OK upon success, SRD_ERR_ARG upon invalid loglevel. |
43c0a640 | 70 | */ |
55c3c5f4 | 71 | SRD_API int srd_log_loglevel_set(int loglevel) |
43c0a640 UH |
72 | { |
73 | if (loglevel < SRD_LOG_NONE || loglevel > SRD_LOG_SPEW) { | |
d906d3f9 | 74 | srd_err("Invalid loglevel %d.", loglevel); |
a62186e4 | 75 | return SRD_ERR_ARG; |
43c0a640 UH |
76 | } |
77 | ||
78 | srd_loglevel = loglevel; | |
79 | ||
7a1712c4 | 80 | srd_dbg("libsigrokdecode loglevel set to %d.", loglevel); |
43c0a640 UH |
81 | |
82 | return SRD_OK; | |
83 | } | |
84 | ||
85 | /** | |
86 | * Get the libsigrokdecode loglevel. | |
87 | * | |
88 | * @return The currently configured libsigrokdecode loglevel. | |
89 | */ | |
55c3c5f4 | 90 | SRD_API int srd_log_loglevel_get(void) |
43c0a640 UH |
91 | { |
92 | return srd_loglevel; | |
93 | } | |
94 | ||
3a8b2e78 | 95 | /** |
41106a07 | 96 | * Set the libsigrokdecode logdomain string. |
3a8b2e78 | 97 | * |
41106a07 UH |
98 | * @param logdomain The string to use as logdomain for libsigrokdecode log |
99 | * messages from now on. Must not be NULL. The maximum | |
100 | * length of the string is 30 characters (this does not | |
101 | * include the trailing NUL-byte). Longer strings are | |
102 | * silently truncated. | |
103 | * In order to not use a logdomain, pass an empty string. | |
104 | * The function makes its own copy of the input string, i.e. | |
105 | * the caller does not need to keep it around. | |
582c8473 | 106 | * |
41106a07 | 107 | * @return SRD_OK upon success, SRD_ERR_ARG upon invalid logdomain. |
3a8b2e78 | 108 | */ |
55c3c5f4 | 109 | SRD_API int srd_log_logdomain_set(const char *logdomain) |
3a8b2e78 UH |
110 | { |
111 | if (!logdomain) { | |
112 | srd_err("log: %s: logdomain was NULL", __func__); | |
113 | return SRD_ERR_ARG; | |
114 | } | |
115 | ||
116 | /* TODO: Error handling. */ | |
117 | snprintf((char *)&srd_log_domain, LOGDOMAIN_MAXLEN, "%s", logdomain); | |
118 | ||
41106a07 | 119 | srd_dbg("Log domain set to '%s'.", (const char *)&srd_log_domain); |
3a8b2e78 UH |
120 | |
121 | return SRD_OK; | |
122 | } | |
123 | ||
124 | /** | |
41106a07 | 125 | * Get the currently configured libsigrokdecode logdomain. |
3a8b2e78 | 126 | * |
41106a07 UH |
127 | * @return A copy of the currently configured libsigrokdecode logdomain |
128 | * string. The caller is responsible for g_free()ing the string when | |
129 | * it is no longer needed. | |
3a8b2e78 | 130 | */ |
55c3c5f4 | 131 | SRD_API char *srd_log_logdomain_get(void) |
3a8b2e78 | 132 | { |
41106a07 | 133 | return g_strdup((const char *)&srd_log_domain); |
3a8b2e78 UH |
134 | } |
135 | ||
66e4c27c | 136 | /** |
0082d592 | 137 | * Set the libsigrokdecode log callback to the specified function. |
66e4c27c | 138 | * |
0082d592 UH |
139 | * @param cb Function pointer to the log callback function to use. |
140 | * Must not be NULL. | |
41a5d962 UH |
141 | * @param cb_data Pointer to private data to be passed on. This can be used |
142 | * by the caller to pass arbitrary data to the log functions. | |
143 | * This pointer is only stored or passed on by libsigrokdecode, | |
144 | * and is never used or interpreted in any way. The pointer | |
145 | * is allowed to be NULL if the caller doesn't need/want to | |
146 | * pass any data. | |
582c8473 | 147 | * |
66e4c27c UH |
148 | * @return SRD_OK upon success, SRD_ERR_ARG upon invalid arguments. |
149 | */ | |
0082d592 | 150 | SRD_API int srd_log_callback_set(srd_log_callback_t cb, void *cb_data) |
66e4c27c | 151 | { |
0082d592 UH |
152 | if (!cb) { |
153 | srd_err("log: %s: cb was NULL", __func__); | |
66e4c27c UH |
154 | return SRD_ERR_ARG; |
155 | } | |
156 | ||
41a5d962 | 157 | /* Note: 'cb_data' is allowed to be NULL. */ |
66e4c27c | 158 | |
0082d592 UH |
159 | srd_log_callback = cb; |
160 | srd_log_callback_data = cb_data; | |
66e4c27c UH |
161 | |
162 | return SRD_OK; | |
163 | } | |
164 | ||
165 | /** | |
0082d592 | 166 | * Set the libsigrokdecode log callback to the default built-in one. |
66e4c27c | 167 | * |
0082d592 | 168 | * Additionally, the internal 'srd_log_callback_data' pointer is set to NULL. |
66e4c27c | 169 | * |
582c8473 | 170 | * @return SRD_OK upon success, a (negative) error code otherwise. |
66e4c27c | 171 | */ |
0082d592 | 172 | SRD_API int srd_log_callback_set_default(void) |
66e4c27c UH |
173 | { |
174 | /* | |
175 | * Note: No log output in this function, as it should safely work | |
0082d592 | 176 | * even if the currently set log callback is buggy/broken. |
66e4c27c | 177 | */ |
0082d592 UH |
178 | srd_log_callback = srd_logv; |
179 | srd_log_callback_data = NULL; | |
66e4c27c UH |
180 | |
181 | return SRD_OK; | |
182 | } | |
183 | ||
41a5d962 | 184 | static int srd_logv(void *cb_data, int loglevel, const char *format, |
e09023b9 | 185 | va_list args) |
43c0a640 UH |
186 | { |
187 | int ret; | |
188 | ||
0082d592 | 189 | /* This specific log callback doesn't need the void pointer data. */ |
41a5d962 | 190 | (void)cb_data; |
66e4c27c | 191 | |
43c0a640 UH |
192 | /* Only output messages of at least the selected loglevel(s). */ |
193 | if (loglevel > srd_loglevel) | |
194 | return SRD_OK; /* TODO? */ | |
195 | ||
3a8b2e78 | 196 | if (srd_log_domain[0] != '\0') |
568112ce | 197 | fprintf(stderr, "%s", srd_log_domain); |
43c0a640 UH |
198 | ret = vfprintf(stderr, format, args); |
199 | fprintf(stderr, "\n"); | |
200 | ||
201 | return ret; | |
202 | } | |
203 | ||
57790bc8 | 204 | /** @private */ |
55c3c5f4 | 205 | SRD_PRIV int srd_log(int loglevel, const char *format, ...) |
43c0a640 UH |
206 | { |
207 | int ret; | |
208 | va_list args; | |
209 | ||
210 | va_start(args, format); | |
0082d592 | 211 | ret = srd_log_callback(srd_log_callback_data, loglevel, format, args); |
43c0a640 UH |
212 | va_end(args); |
213 | ||
214 | return ret; | |
215 | } | |
216 | ||
57790bc8 | 217 | /** @private */ |
55c3c5f4 | 218 | SRD_PRIV int srd_spew(const char *format, ...) |
43c0a640 UH |
219 | { |
220 | int ret; | |
221 | va_list args; | |
222 | ||
223 | va_start(args, format); | |
0082d592 UH |
224 | ret = srd_log_callback(srd_log_callback_data, SRD_LOG_SPEW, |
225 | format, args); | |
43c0a640 UH |
226 | va_end(args); |
227 | ||
228 | return ret; | |
229 | } | |
230 | ||
57790bc8 | 231 | /** @private */ |
55c3c5f4 | 232 | SRD_PRIV int srd_dbg(const char *format, ...) |
43c0a640 UH |
233 | { |
234 | int ret; | |
235 | va_list args; | |
236 | ||
237 | va_start(args, format); | |
0082d592 UH |
238 | ret = srd_log_callback(srd_log_callback_data, SRD_LOG_DBG, |
239 | format, args); | |
43c0a640 UH |
240 | va_end(args); |
241 | ||
242 | return ret; | |
243 | } | |
244 | ||
57790bc8 | 245 | /** @private */ |
55c3c5f4 | 246 | SRD_PRIV int srd_info(const char *format, ...) |
43c0a640 UH |
247 | { |
248 | int ret; | |
249 | va_list args; | |
250 | ||
251 | va_start(args, format); | |
0082d592 UH |
252 | ret = srd_log_callback(srd_log_callback_data, SRD_LOG_INFO, |
253 | format, args); | |
43c0a640 UH |
254 | va_end(args); |
255 | ||
256 | return ret; | |
257 | } | |
258 | ||
57790bc8 | 259 | /** @private */ |
55c3c5f4 | 260 | SRD_PRIV int srd_warn(const char *format, ...) |
43c0a640 UH |
261 | { |
262 | int ret; | |
263 | va_list args; | |
264 | ||
265 | va_start(args, format); | |
0082d592 UH |
266 | ret = srd_log_callback(srd_log_callback_data, SRD_LOG_WARN, |
267 | format, args); | |
43c0a640 UH |
268 | va_end(args); |
269 | ||
270 | return ret; | |
271 | } | |
272 | ||
57790bc8 | 273 | /** @private */ |
55c3c5f4 | 274 | SRD_PRIV int srd_err(const char *format, ...) |
43c0a640 UH |
275 | { |
276 | int ret; | |
277 | va_list args; | |
278 | ||
279 | va_start(args, format); | |
0082d592 UH |
280 | ret = srd_log_callback(srd_log_callback_data, SRD_LOG_ERR, |
281 | format, args); | |
43c0a640 UH |
282 | va_end(args); |
283 | ||
284 | return ret; | |
285 | } |