]> sigrok.org Git - libsigrokdecode.git/blame_incremental - log.c
configure.ac: Look for python-config-3.x besides python3.x-config.
[libsigrokdecode.git] / log.c
... / ...
CommitLineData
1/*
2 * This file is part of the sigrok 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 "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
22#include "sigrokdecode-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. */
41static int srd_loglevel = SRD_LOG_WARN; /* Show errors+warnings per default. */
42
43/* Function prototype. */
44static 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(). */
48static 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 */
54static 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 */
61static 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 */
79SRD_API int srd_log_loglevel_set(int loglevel)
80{
81 if (loglevel < SRD_LOG_NONE || loglevel > SRD_LOG_SPEW) {
82 srd_err("Invalid loglevel %d.", loglevel);
83 return SRD_ERR_ARG;
84 }
85
86 srd_loglevel = loglevel;
87
88 srd_dbg("libsigrokdecode loglevel set to %d.", loglevel);
89
90 return SRD_OK;
91}
92
93/**
94 * Get the libsigrokdecode loglevel.
95 *
96 * @return The currently configured libsigrokdecode loglevel.
97 */
98SRD_API int srd_log_loglevel_get(void)
99{
100 return srd_loglevel;
101}
102
103/**
104 * Set the libsigrokdecode logdomain string.
105 *
106 * @param logdomain The string to use as logdomain for libsigrokdecode log
107 * messages from now on. Must not be NULL. The maximum
108 * length of the string is 30 characters (this does not
109 * include the trailing NUL-byte). Longer strings are
110 * silently truncated.
111 * In order to not use a logdomain, pass an empty string.
112 * The function makes its own copy of the input string, i.e.
113 * the caller does not need to keep it around.
114 *
115 * @return SRD_OK upon success, SRD_ERR_ARG upon invalid logdomain.
116 */
117SRD_API int srd_log_logdomain_set(const char *logdomain)
118{
119 if (!logdomain) {
120 srd_err("log: %s: logdomain was NULL", __func__);
121 return SRD_ERR_ARG;
122 }
123
124 /* TODO: Error handling. */
125 snprintf((char *)&srd_log_domain, LOGDOMAIN_MAXLEN, "%s", logdomain);
126
127 srd_dbg("Log domain set to '%s'.", (const char *)&srd_log_domain);
128
129 return SRD_OK;
130}
131
132/**
133 * Get the currently configured libsigrokdecode logdomain.
134 *
135 * @return A copy of the currently configured libsigrokdecode logdomain
136 * string. The caller is responsible for g_free()ing the string when
137 * it is no longer needed.
138 */
139SRD_API char *srd_log_logdomain_get(void)
140{
141 return g_strdup((const char *)&srd_log_domain);
142}
143
144/**
145 * Set the libsigrokdecode log callback to the specified function.
146 *
147 * @param cb Function pointer to the log callback function to use.
148 * Must not be NULL.
149 * @param cb_data Pointer to private data to be passed on. This can be used
150 * by the caller to pass arbitrary data to the log functions.
151 * This pointer is only stored or passed on by libsigrokdecode,
152 * and is never used or interpreted in any way. The pointer
153 * is allowed to be NULL if the caller doesn't need/want to
154 * pass any data.
155 *
156 * @return SRD_OK upon success, SRD_ERR_ARG upon invalid arguments.
157 */
158SRD_API int srd_log_callback_set(srd_log_callback_t cb, void *cb_data)
159{
160 if (!cb) {
161 srd_err("log: %s: cb was NULL", __func__);
162 return SRD_ERR_ARG;
163 }
164
165 /* Note: 'cb_data' is allowed to be NULL. */
166
167 srd_log_callback = cb;
168 srd_log_callback_data = cb_data;
169
170 return SRD_OK;
171}
172
173/**
174 * Set the libsigrokdecode log callback to the default built-in one.
175 *
176 * Additionally, the internal 'srd_log_callback_data' pointer is set to NULL.
177 *
178 * @return SRD_OK upon success, a (negative) error code otherwise.
179 */
180SRD_API int srd_log_callback_set_default(void)
181{
182 /*
183 * Note: No log output in this function, as it should safely work
184 * even if the currently set log callback is buggy/broken.
185 */
186 srd_log_callback = srd_logv;
187 srd_log_callback_data = NULL;
188
189 return SRD_OK;
190}
191
192static int srd_logv(void *cb_data, int loglevel, const char *format,
193 va_list args)
194{
195 int ret;
196
197 /* This specific log callback doesn't need the void pointer data. */
198 (void)cb_data;
199
200 /* Only output messages of at least the selected loglevel(s). */
201 if (loglevel > srd_loglevel)
202 return SRD_OK; /* TODO? */
203
204 if (srd_log_domain[0] != '\0')
205 fprintf(stderr, "%s", srd_log_domain);
206 ret = vfprintf(stderr, format, args);
207 fprintf(stderr, "\n");
208
209 return ret;
210}
211
212/** @private */
213SRD_PRIV int srd_log(int loglevel, const char *format, ...)
214{
215 int ret;
216 va_list args;
217
218 va_start(args, format);
219 ret = srd_log_callback(srd_log_callback_data, loglevel, format, args);
220 va_end(args);
221
222 return ret;
223}
224
225/** @private */
226SRD_PRIV int srd_spew(const char *format, ...)
227{
228 int ret;
229 va_list args;
230
231 va_start(args, format);
232 ret = srd_log_callback(srd_log_callback_data, SRD_LOG_SPEW,
233 format, args);
234 va_end(args);
235
236 return ret;
237}
238
239/** @private */
240SRD_PRIV int srd_dbg(const char *format, ...)
241{
242 int ret;
243 va_list args;
244
245 va_start(args, format);
246 ret = srd_log_callback(srd_log_callback_data, SRD_LOG_DBG,
247 format, args);
248 va_end(args);
249
250 return ret;
251}
252
253/** @private */
254SRD_PRIV int srd_info(const char *format, ...)
255{
256 int ret;
257 va_list args;
258
259 va_start(args, format);
260 ret = srd_log_callback(srd_log_callback_data, SRD_LOG_INFO,
261 format, args);
262 va_end(args);
263
264 return ret;
265}
266
267/** @private */
268SRD_PRIV int srd_warn(const char *format, ...)
269{
270 int ret;
271 va_list args;
272
273 va_start(args, format);
274 ret = srd_log_callback(srd_log_callback_data, SRD_LOG_WARN,
275 format, args);
276 va_end(args);
277
278 return ret;
279}
280
281/** @private */
282SRD_PRIV int srd_err(const char *format, ...)
283{
284 int ret;
285 va_list args;
286
287 va_start(args, format);
288 ret = srd_log_callback(srd_log_callback_data, SRD_LOG_ERR,
289 format, args);
290 va_end(args);
291
292 return ret;
293}
294
295/** @} */