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