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