]> sigrok.org Git - libsigrokdecode.git/blame - log.c
lpc: Initial start-/end-sample support.
[libsigrokdecode.git] / log.c
CommitLineData
43c0a640 1/*
50bd5d25 2 * This file is part of the libsigrokdecode project.
43c0a640 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
c1f86f02
UH
21#include "libsigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
22#include "libsigrokdecode-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.
8c664ca2
UH
78 *
79 * @since 0.1.0
43c0a640 80 */
55c3c5f4 81SRD_API int srd_log_loglevel_set(int loglevel)
43c0a640
UH
82{
83 if (loglevel < SRD_LOG_NONE || loglevel > SRD_LOG_SPEW) {
d906d3f9 84 srd_err("Invalid loglevel %d.", loglevel);
a62186e4 85 return SRD_ERR_ARG;
43c0a640
UH
86 }
87
88 srd_loglevel = loglevel;
89
7a1712c4 90 srd_dbg("libsigrokdecode loglevel set to %d.", loglevel);
43c0a640
UH
91
92 return SRD_OK;
93}
94
95/**
96 * Get the libsigrokdecode loglevel.
97 *
98 * @return The currently configured libsigrokdecode loglevel.
8c664ca2
UH
99 *
100 * @since 0.1.0
43c0a640 101 */
55c3c5f4 102SRD_API int srd_log_loglevel_get(void)
43c0a640
UH
103{
104 return srd_loglevel;
105}
106
3a8b2e78 107/**
41106a07 108 * Set the libsigrokdecode logdomain string.
3a8b2e78 109 *
41106a07
UH
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.
582c8473 118 *
41106a07 119 * @return SRD_OK upon success, SRD_ERR_ARG upon invalid logdomain.
8c664ca2
UH
120 *
121 * @since 0.1.0
3a8b2e78 122 */
55c3c5f4 123SRD_API int srd_log_logdomain_set(const char *logdomain)
3a8b2e78
UH
124{
125 if (!logdomain) {
126 srd_err("log: %s: logdomain was NULL", __func__);
127 return SRD_ERR_ARG;
128 }
129
130 /* TODO: Error handling. */
131 snprintf((char *)&srd_log_domain, LOGDOMAIN_MAXLEN, "%s", logdomain);
132
41106a07 133 srd_dbg("Log domain set to '%s'.", (const char *)&srd_log_domain);
3a8b2e78
UH
134
135 return SRD_OK;
136}
137
138/**
41106a07 139 * Get the currently configured libsigrokdecode logdomain.
3a8b2e78 140 *
41106a07
UH
141 * @return A copy of the currently configured libsigrokdecode logdomain
142 * string. The caller is responsible for g_free()ing the string when
143 * it is no longer needed.
8c664ca2
UH
144 *
145 * @since 0.1.0
3a8b2e78 146 */
55c3c5f4 147SRD_API char *srd_log_logdomain_get(void)
3a8b2e78 148{
41106a07 149 return g_strdup((const char *)&srd_log_domain);
3a8b2e78
UH
150}
151
66e4c27c 152/**
0082d592 153 * Set the libsigrokdecode log callback to the specified function.
66e4c27c 154 *
0082d592
UH
155 * @param cb Function pointer to the log callback function to use.
156 * Must not be NULL.
41a5d962
UH
157 * @param cb_data Pointer to private data to be passed on. This can be used
158 * by the caller to pass arbitrary data to the log functions.
159 * This pointer is only stored or passed on by libsigrokdecode,
160 * and is never used or interpreted in any way. The pointer
161 * is allowed to be NULL if the caller doesn't need/want to
162 * pass any data.
582c8473 163 *
66e4c27c 164 * @return SRD_OK upon success, SRD_ERR_ARG upon invalid arguments.
8c664ca2
UH
165 *
166 * @since 0.1.0
66e4c27c 167 */
0082d592 168SRD_API int srd_log_callback_set(srd_log_callback_t cb, void *cb_data)
66e4c27c 169{
0082d592
UH
170 if (!cb) {
171 srd_err("log: %s: cb was NULL", __func__);
66e4c27c
UH
172 return SRD_ERR_ARG;
173 }
174
41a5d962 175 /* Note: 'cb_data' is allowed to be NULL. */
66e4c27c 176
0082d592
UH
177 srd_log_callback = cb;
178 srd_log_callback_data = cb_data;
66e4c27c
UH
179
180 return SRD_OK;
181}
182
183/**
0082d592 184 * Set the libsigrokdecode log callback to the default built-in one.
66e4c27c 185 *
0082d592 186 * Additionally, the internal 'srd_log_callback_data' pointer is set to NULL.
66e4c27c 187 *
582c8473 188 * @return SRD_OK upon success, a (negative) error code otherwise.
8c664ca2
UH
189 *
190 * @since 0.1.0
66e4c27c 191 */
0082d592 192SRD_API int srd_log_callback_set_default(void)
66e4c27c
UH
193{
194 /*
195 * Note: No log output in this function, as it should safely work
0082d592 196 * even if the currently set log callback is buggy/broken.
66e4c27c 197 */
0082d592
UH
198 srd_log_callback = srd_logv;
199 srd_log_callback_data = NULL;
66e4c27c
UH
200
201 return SRD_OK;
202}
203
41a5d962 204static int srd_logv(void *cb_data, int loglevel, const char *format,
e09023b9 205 va_list args)
43c0a640
UH
206{
207 int ret;
208
0082d592 209 /* This specific log callback doesn't need the void pointer data. */
41a5d962 210 (void)cb_data;
66e4c27c 211
43c0a640
UH
212 /* Only output messages of at least the selected loglevel(s). */
213 if (loglevel > srd_loglevel)
214 return SRD_OK; /* TODO? */
215
3a8b2e78 216 if (srd_log_domain[0] != '\0')
568112ce 217 fprintf(stderr, "%s", srd_log_domain);
43c0a640
UH
218 ret = vfprintf(stderr, format, args);
219 fprintf(stderr, "\n");
220
221 return ret;
222}
223
57790bc8 224/** @private */
55c3c5f4 225SRD_PRIV int srd_log(int loglevel, const char *format, ...)
43c0a640
UH
226{
227 int ret;
228 va_list args;
229
230 va_start(args, format);
0082d592 231 ret = srd_log_callback(srd_log_callback_data, loglevel, format, args);
43c0a640
UH
232 va_end(args);
233
234 return ret;
235}
236
57790bc8 237/** @private */
55c3c5f4 238SRD_PRIV int srd_spew(const char *format, ...)
43c0a640
UH
239{
240 int ret;
241 va_list args;
242
243 va_start(args, format);
0082d592
UH
244 ret = srd_log_callback(srd_log_callback_data, SRD_LOG_SPEW,
245 format, args);
43c0a640
UH
246 va_end(args);
247
248 return ret;
249}
250
57790bc8 251/** @private */
55c3c5f4 252SRD_PRIV int srd_dbg(const char *format, ...)
43c0a640
UH
253{
254 int ret;
255 va_list args;
256
257 va_start(args, format);
0082d592
UH
258 ret = srd_log_callback(srd_log_callback_data, SRD_LOG_DBG,
259 format, args);
43c0a640
UH
260 va_end(args);
261
262 return ret;
263}
264
57790bc8 265/** @private */
55c3c5f4 266SRD_PRIV int srd_info(const char *format, ...)
43c0a640
UH
267{
268 int ret;
269 va_list args;
270
271 va_start(args, format);
0082d592
UH
272 ret = srd_log_callback(srd_log_callback_data, SRD_LOG_INFO,
273 format, args);
43c0a640
UH
274 va_end(args);
275
276 return ret;
277}
278
57790bc8 279/** @private */
55c3c5f4 280SRD_PRIV int srd_warn(const char *format, ...)
43c0a640
UH
281{
282 int ret;
283 va_list args;
284
285 va_start(args, format);
0082d592
UH
286 ret = srd_log_callback(srd_log_callback_data, SRD_LOG_WARN,
287 format, args);
43c0a640
UH
288 va_end(args);
289
290 return ret;
291}
292
57790bc8 293/** @private */
55c3c5f4 294SRD_PRIV int srd_err(const char *format, ...)
43c0a640
UH
295{
296 int ret;
297 va_list args;
298
299 va_start(args, format);
0082d592
UH
300 ret = srd_log_callback(srd_log_callback_data, SRD_LOG_ERR,
301 format, args);
43c0a640
UH
302 va_end(args);
303
304 return ret;
305}
4895418c
UH
306
307/** @} */