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