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