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