]> sigrok.org Git - libsigrokdecode.git/blame - log.c
configure.ac: Look for python-config-3.x besides python3.x-config.
[libsigrokdecode.git] / log.c
CommitLineData
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
4895418c
UH
32/**
33 * @defgroup grp_logging Logging
34 *
35 * Controlling the libsigrokdecode message logging functionality.
36 *
37 * @{
38 */
39
66e4c27c 40/* Currently selected libsigrokdecode loglevel. Default: SRD_LOG_WARN. */
43c0a640
UH
41static int srd_loglevel = SRD_LOG_WARN; /* Show errors+warnings per default. */
42
66e4c27c 43/* Function prototype. */
41a5d962 44static int srd_logv(void *cb_data, int loglevel, const char *format,
e09023b9 45 va_list args);
66e4c27c 46
0082d592
UH
47/* Pointer to the currently selected log callback. Default: srd_logv(). */
48static srd_log_callback_t srd_log_callback = srd_logv;
66e4c27c
UH
49
50/*
0082d592 51 * Pointer to private data that can be passed to the log callback.
66e4c27c
UH
52 * This can be used (for example) by C++ GUIs to pass a "this" pointer.
53 */
0082d592 54static void *srd_log_callback_data = NULL;
66e4c27c 55
3a8b2e78 56/* Log domain (a short string that is used as prefix for all messages). */
57790bc8 57/** @cond PRIVATE */
3a8b2e78
UH
58#define LOGDOMAIN_MAXLEN 30
59#define LOGDOMAIN_DEFAULT "srd: "
57790bc8 60/** @endcond */
3a8b2e78
UH
61static char srd_log_domain[LOGDOMAIN_MAXLEN + 1] = LOGDOMAIN_DEFAULT;
62
43c0a640
UH
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 *
41106a07
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 SRD_LOG_DBG for
72 * example. Whether this message is shown depends on the (new) loglevel.
73 *
43c0a640
UH
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).
582c8473 76 *
a62186e4 77 * @return SRD_OK upon success, SRD_ERR_ARG upon invalid loglevel.
43c0a640 78 */
55c3c5f4 79SRD_API int srd_log_loglevel_set(int loglevel)
43c0a640
UH
80{
81 if (loglevel < SRD_LOG_NONE || loglevel > SRD_LOG_SPEW) {
d906d3f9 82 srd_err("Invalid loglevel %d.", loglevel);
a62186e4 83 return SRD_ERR_ARG;
43c0a640
UH
84 }
85
86 srd_loglevel = loglevel;
87
7a1712c4 88 srd_dbg("libsigrokdecode loglevel set to %d.", loglevel);
43c0a640
UH
89
90 return SRD_OK;
91}
92
93/**
94 * Get the libsigrokdecode loglevel.
95 *
96 * @return The currently configured libsigrokdecode loglevel.
97 */
55c3c5f4 98SRD_API int srd_log_loglevel_get(void)
43c0a640
UH
99{
100 return srd_loglevel;
101}
102
3a8b2e78 103/**
41106a07 104 * Set the libsigrokdecode logdomain string.
3a8b2e78 105 *
41106a07
UH
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.
582c8473 114 *
41106a07 115 * @return SRD_OK upon success, SRD_ERR_ARG upon invalid logdomain.
3a8b2e78 116 */
55c3c5f4 117SRD_API int srd_log_logdomain_set(const char *logdomain)
3a8b2e78
UH
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
41106a07 127 srd_dbg("Log domain set to '%s'.", (const char *)&srd_log_domain);
3a8b2e78
UH
128
129 return SRD_OK;
130}
131
132/**
41106a07 133 * Get the currently configured libsigrokdecode logdomain.
3a8b2e78 134 *
41106a07
UH
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.
3a8b2e78 138 */
55c3c5f4 139SRD_API char *srd_log_logdomain_get(void)
3a8b2e78 140{
41106a07 141 return g_strdup((const char *)&srd_log_domain);
3a8b2e78
UH
142}
143
66e4c27c 144/**
0082d592 145 * Set the libsigrokdecode log callback to the specified function.
66e4c27c 146 *
0082d592
UH
147 * @param cb Function pointer to the log callback function to use.
148 * Must not be NULL.
41a5d962
UH
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.
582c8473 155 *
66e4c27c
UH
156 * @return SRD_OK upon success, SRD_ERR_ARG upon invalid arguments.
157 */
0082d592 158SRD_API int srd_log_callback_set(srd_log_callback_t cb, void *cb_data)
66e4c27c 159{
0082d592
UH
160 if (!cb) {
161 srd_err("log: %s: cb was NULL", __func__);
66e4c27c
UH
162 return SRD_ERR_ARG;
163 }
164
41a5d962 165 /* Note: 'cb_data' is allowed to be NULL. */
66e4c27c 166
0082d592
UH
167 srd_log_callback = cb;
168 srd_log_callback_data = cb_data;
66e4c27c
UH
169
170 return SRD_OK;
171}
172
173/**
0082d592 174 * Set the libsigrokdecode log callback to the default built-in one.
66e4c27c 175 *
0082d592 176 * Additionally, the internal 'srd_log_callback_data' pointer is set to NULL.
66e4c27c 177 *
582c8473 178 * @return SRD_OK upon success, a (negative) error code otherwise.
66e4c27c 179 */
0082d592 180SRD_API int srd_log_callback_set_default(void)
66e4c27c
UH
181{
182 /*
183 * Note: No log output in this function, as it should safely work
0082d592 184 * even if the currently set log callback is buggy/broken.
66e4c27c 185 */
0082d592
UH
186 srd_log_callback = srd_logv;
187 srd_log_callback_data = NULL;
66e4c27c
UH
188
189 return SRD_OK;
190}
191
41a5d962 192static int srd_logv(void *cb_data, int loglevel, const char *format,
e09023b9 193 va_list args)
43c0a640
UH
194{
195 int ret;
196
0082d592 197 /* This specific log callback doesn't need the void pointer data. */
41a5d962 198 (void)cb_data;
66e4c27c 199
43c0a640
UH
200 /* Only output messages of at least the selected loglevel(s). */
201 if (loglevel > srd_loglevel)
202 return SRD_OK; /* TODO? */
203
3a8b2e78 204 if (srd_log_domain[0] != '\0')
568112ce 205 fprintf(stderr, "%s", srd_log_domain);
43c0a640
UH
206 ret = vfprintf(stderr, format, args);
207 fprintf(stderr, "\n");
208
209 return ret;
210}
211
57790bc8 212/** @private */
55c3c5f4 213SRD_PRIV int srd_log(int loglevel, const char *format, ...)
43c0a640
UH
214{
215 int ret;
216 va_list args;
217
218 va_start(args, format);
0082d592 219 ret = srd_log_callback(srd_log_callback_data, loglevel, format, args);
43c0a640
UH
220 va_end(args);
221
222 return ret;
223}
224
57790bc8 225/** @private */
55c3c5f4 226SRD_PRIV int srd_spew(const char *format, ...)
43c0a640
UH
227{
228 int ret;
229 va_list args;
230
231 va_start(args, format);
0082d592
UH
232 ret = srd_log_callback(srd_log_callback_data, SRD_LOG_SPEW,
233 format, args);
43c0a640
UH
234 va_end(args);
235
236 return ret;
237}
238
57790bc8 239/** @private */
55c3c5f4 240SRD_PRIV int srd_dbg(const char *format, ...)
43c0a640
UH
241{
242 int ret;
243 va_list args;
244
245 va_start(args, format);
0082d592
UH
246 ret = srd_log_callback(srd_log_callback_data, SRD_LOG_DBG,
247 format, args);
43c0a640
UH
248 va_end(args);
249
250 return ret;
251}
252
57790bc8 253/** @private */
55c3c5f4 254SRD_PRIV int srd_info(const char *format, ...)
43c0a640
UH
255{
256 int ret;
257 va_list args;
258
259 va_start(args, format);
0082d592
UH
260 ret = srd_log_callback(srd_log_callback_data, SRD_LOG_INFO,
261 format, args);
43c0a640
UH
262 va_end(args);
263
264 return ret;
265}
266
57790bc8 267/** @private */
55c3c5f4 268SRD_PRIV int srd_warn(const char *format, ...)
43c0a640
UH
269{
270 int ret;
271 va_list args;
272
273 va_start(args, format);
0082d592
UH
274 ret = srd_log_callback(srd_log_callback_data, SRD_LOG_WARN,
275 format, args);
43c0a640
UH
276 va_end(args);
277
278 return ret;
279}
280
57790bc8 281/** @private */
55c3c5f4 282SRD_PRIV int srd_err(const char *format, ...)
43c0a640
UH
283{
284 int ret;
285 va_list args;
286
287 va_start(args, format);
0082d592
UH
288 ret = srd_log_callback(srd_log_callback_data, SRD_LOG_ERR,
289 format, args);
43c0a640
UH
290 va_end(args);
291
292 return ret;
293}
4895418c
UH
294
295/** @} */