]> sigrok.org Git - libsigrok.git/blame - log.c
Doxygen: Mark non-public stuff for exclusion.
[libsigrok.git] / log.c
CommitLineData
b08024a8
UH
1/*
2 * This file is part of the sigrok project.
3 *
0ae67ff7 4 * Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de>
b08024a8
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
21#include <stdarg.h>
22#include <stdio.h>
45c59c8b
BV
23#include "libsigrok.h"
24#include "libsigrok-internal.h"
b08024a8 25
b4bd7088
UH
26/**
27 * @file
28 *
29 * Logging support.
30 */
31
0ae67ff7 32/* Currently selected libsigrok loglevel. Default: SR_LOG_WARN. */
1352eedd
UH
33static int sr_loglevel = SR_LOG_WARN; /* Show errors+warnings per default. */
34
0ae67ff7 35/* Function prototype. */
1f9813eb
UH
36static int sr_logv(void *cb_data, int loglevel, const char *format,
37 va_list args);
0ae67ff7 38
b5118d6c
UH
39/* Pointer to the currently selected log callback. Default: sr_logv(). */
40static sr_log_callback_t sr_log_callback = sr_logv;
0ae67ff7
UH
41
42/*
b5118d6c 43 * Pointer to private data that can be passed to the log callback.
0ae67ff7
UH
44 * This can be used (for example) by C++ GUIs to pass a "this" pointer.
45 */
b5118d6c 46static void *sr_log_callback_data = NULL;
0ae67ff7
UH
47
48/* Log domain (a short string that is used as prefix for all messages). */
b4bd7088 49/** @cond PRIVATE */
0ae67ff7
UH
50#define LOGDOMAIN_MAXLEN 30
51#define LOGDOMAIN_DEFAULT "sr: "
b4bd7088 52/** @endcond */
0ae67ff7
UH
53static char sr_log_domain[LOGDOMAIN_MAXLEN + 1] = LOGDOMAIN_DEFAULT;
54
1352eedd
UH
55/**
56 * Set the libsigrok loglevel.
57 *
58 * This influences the amount of log messages (debug messages, error messages,
59 * and so on) libsigrok will output. Using SR_LOG_NONE disables all messages.
60 *
0ae67ff7
UH
61 * Note that this function itself will also output log messages. After the
62 * loglevel has changed, it will output a debug message with SR_LOG_DBG for
63 * example. Whether this message is shown depends on the (new) loglevel.
64 *
1352eedd 65 * @param loglevel The loglevel to set (SR_LOG_NONE, SR_LOG_ERR, SR_LOG_WARN,
06dd80d4 66 * SR_LOG_INFO, SR_LOG_DBG, or SR_LOG_SPEW).
44dae539 67 *
1352eedd
UH
68 * @return SR_OK upon success, SR_ERR_ARG upon invalid loglevel.
69 */
0ae67ff7 70SR_API int sr_log_loglevel_set(int loglevel)
1352eedd 71{
06dd80d4 72 if (loglevel < SR_LOG_NONE || loglevel > SR_LOG_SPEW) {
0ae67ff7 73 sr_err("Invalid loglevel %d.", loglevel);
1352eedd
UH
74 return SR_ERR_ARG;
75 }
76
77 sr_loglevel = loglevel;
78
0ae67ff7 79 sr_dbg("libsigrok loglevel set to %d.", loglevel);
1352eedd
UH
80
81 return SR_OK;
82}
83
84/**
85 * Get the libsigrok loglevel.
86 *
87 * @return The currently configured libsigrok loglevel.
88 */
0ae67ff7 89SR_API int sr_log_loglevel_get(void)
1352eedd
UH
90{
91 return sr_loglevel;
92}
93
0ae67ff7
UH
94/**
95 * Set the libsigrok logdomain string.
96 *
97 * @param logdomain The string to use as logdomain for libsigrok log
98 * messages from now on. Must not be NULL. The maximum
99 * length of the string is 30 characters (this does not
100 * include the trailing NUL-byte). Longer strings are
101 * silently truncated.
102 * In order to not use a logdomain, pass an empty string.
103 * The function makes its own copy of the input string, i.e.
104 * the caller does not need to keep it around.
44dae539 105 *
0ae67ff7
UH
106 * @return SR_OK upon success, SR_ERR_ARG upon invalid logdomain.
107 */
108SR_API int sr_log_logdomain_set(const char *logdomain)
109{
110 if (!logdomain) {
111 sr_err("log: %s: logdomain was NULL", __func__);
112 return SR_ERR_ARG;
113 }
114
115 /* TODO: Error handling. */
116 snprintf((char *)&sr_log_domain, LOGDOMAIN_MAXLEN, "%s", logdomain);
117
118 sr_dbg("Log domain set to '%s'.", (const char *)&sr_log_domain);
119
120 return SR_OK;
121}
122
123/**
124 * Get the currently configured libsigrok logdomain.
125 *
126 * @return A copy of the currently configured libsigrok logdomain
127 * string. The caller is responsible for g_free()ing the string when
128 * it is no longer needed.
129 */
130SR_API char *sr_log_logdomain_get(void)
131{
132 return g_strdup((const char *)&sr_log_domain);
133}
134
135/**
b5118d6c 136 * Set the libsigrok log callback to the specified function.
0ae67ff7 137 *
b5118d6c
UH
138 * @param cb Function pointer to the log callback function to use.
139 * Must not be NULL.
1f9813eb
UH
140 * @param cb_data Pointer to private data to be passed on. This can be used by
141 * the caller to pass arbitrary data to the log functions. This
142 * pointer is only stored or passed on by libsigrok, and is
143 * never used or interpreted in any way. The pointer is allowed
144 * to be NULL if the caller doesn't need/want to pass any data.
44dae539 145 *
0ae67ff7
UH
146 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
147 */
b5118d6c 148SR_API int sr_log_callback_set(sr_log_callback_t cb, void *cb_data)
0ae67ff7 149{
b5118d6c
UH
150 if (!cb) {
151 sr_err("log: %s: cb was NULL", __func__);
0ae67ff7
UH
152 return SR_ERR_ARG;
153 }
154
1f9813eb 155 /* Note: 'cb_data' is allowed to be NULL. */
0ae67ff7 156
b5118d6c
UH
157 sr_log_callback = cb;
158 sr_log_callback_data = cb_data;
0ae67ff7
UH
159
160 return SR_OK;
161}
162
163/**
b5118d6c 164 * Set the libsigrok log callback to the default built-in one.
0ae67ff7 165 *
b5118d6c 166 * Additionally, the internal 'sr_log_callback_data' pointer is set to NULL.
0ae67ff7
UH
167 *
168 * @return SR_OK upon success, a negative error code otherwise.
169 */
b5118d6c 170SR_API int sr_log_callback_set_default(void)
0ae67ff7
UH
171{
172 /*
173 * Note: No log output in this function, as it should safely work
b5118d6c 174 * even if the currently set log callback is buggy/broken.
0ae67ff7 175 */
b5118d6c
UH
176 sr_log_callback = sr_logv;
177 sr_log_callback_data = NULL;
0ae67ff7
UH
178
179 return SR_OK;
180}
181
1f9813eb 182static int sr_logv(void *cb_data, int loglevel, const char *format, va_list args)
b08024a8
UH
183{
184 int ret;
185
b5118d6c 186 /* This specific log callback doesn't need the void pointer data. */
1f9813eb 187 (void)cb_data;
0ae67ff7 188
1352eedd
UH
189 /* Only output messages of at least the selected loglevel(s). */
190 if (loglevel > sr_loglevel)
191 return SR_OK; /* TODO? */
b08024a8 192
0ae67ff7
UH
193 if (sr_log_domain[0] != '\0')
194 fprintf(stderr, "%s", sr_log_domain);
b08024a8
UH
195 ret = vfprintf(stderr, format, args);
196 fprintf(stderr, "\n");
197
198 return ret;
199}
200
b4bd7088 201/** @private */
1a081ca6 202SR_PRIV int sr_log(int loglevel, const char *format, ...)
b08024a8
UH
203{
204 int ret;
205 va_list args;
206
207 va_start(args, format);
b5118d6c 208 ret = sr_log_callback(sr_log_callback_data, loglevel, format, args);
b08024a8
UH
209 va_end(args);
210
211 return ret;
212}
213
b4bd7088 214/** @private */
1a081ca6 215SR_PRIV int sr_spew(const char *format, ...)
06dd80d4
UH
216{
217 int ret;
218 va_list args;
219
220 va_start(args, format);
b5118d6c 221 ret = sr_log_callback(sr_log_callback_data, SR_LOG_SPEW, format, args);
06dd80d4
UH
222 va_end(args);
223
224 return ret;
225}
226
b4bd7088 227/** @private */
1a081ca6 228SR_PRIV int sr_dbg(const char *format, ...)
b08024a8
UH
229{
230 int ret;
231 va_list args;
232
233 va_start(args, format);
b5118d6c 234 ret = sr_log_callback(sr_log_callback_data, SR_LOG_DBG, format, args);
b08024a8
UH
235 va_end(args);
236
237 return ret;
238}
239
b4bd7088 240/** @private */
1a081ca6 241SR_PRIV int sr_info(const char *format, ...)
b08024a8
UH
242{
243 int ret;
244 va_list args;
245
246 va_start(args, format);
b5118d6c 247 ret = sr_log_callback(sr_log_callback_data, SR_LOG_INFO, format, args);
b08024a8
UH
248 va_end(args);
249
250 return ret;
251}
252
b4bd7088 253/** @private */
1a081ca6 254SR_PRIV int sr_warn(const char *format, ...)
b08024a8
UH
255{
256 int ret;
257 va_list args;
258
259 va_start(args, format);
b5118d6c 260 ret = sr_log_callback(sr_log_callback_data, SR_LOG_WARN, format, args);
b08024a8
UH
261 va_end(args);
262
263 return ret;
264}
265
b4bd7088 266/** @private */
1a081ca6 267SR_PRIV int sr_err(const char *format, ...)
b08024a8
UH
268{
269 int ret;
270 va_list args;
271
272 va_start(args, format);
b5118d6c 273 ret = sr_log_callback(sr_log_callback_data, SR_LOG_ERR, format, args);
b08024a8
UH
274 va_end(args);
275
276 return ret;
277}