]> sigrok.org Git - libsigrokdecode.git/blame_incremental - log.c
srd: finish up public/private API
[libsigrokdecode.git] / log.c
... / ...
CommitLineData
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de>
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 "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
22#include "sigrokdecode-internal.h"
23#include <stdarg.h>
24#include <stdio.h>
25
26/* Currently selected libsigrokdecode loglevel. Default: SRD_LOG_WARN. */
27static int srd_loglevel = SRD_LOG_WARN; /* Show errors+warnings per default. */
28
29/* Function prototype. */
30static int srd_logv(void *user_data, int loglevel, const char *format,
31 va_list args);
32
33/* Pointer to the currently selected log handler. Default: srd_logv(). */
34static srd_log_handler_t srd_log_handler = srd_logv;
35
36/*
37 * Pointer to private data that can be passed to the log handler.
38 * This can be used (for example) by C++ GUIs to pass a "this" pointer.
39 */
40static void *srd_log_handler_data = NULL;
41
42/* Log domain (a short string that is used as prefix for all messages). */
43#define LOGDOMAIN_MAXLEN 30
44#define LOGDOMAIN_DEFAULT "srd: "
45static char srd_log_domain[LOGDOMAIN_MAXLEN + 1] = LOGDOMAIN_DEFAULT;
46
47/**
48 * Set the libsigrokdecode loglevel.
49 *
50 * This influences the amount of log messages (debug messages, error messages,
51 * and so on) libsigrokdecode will output. Using SRD_LOG_NONE disables all
52 * messages.
53 *
54 * Note that this function itself will also output log messages. After the
55 * loglevel has changed, it will output a debug message with SRD_LOG_DBG for
56 * example. Whether this message is shown depends on the (new) loglevel.
57 *
58 * @param loglevel The loglevel to set (SRD_LOG_NONE, SRD_LOG_ERR,
59 * SRD_LOG_WARN, SRD_LOG_INFO, SRD_LOG_DBG, or SRD_LOG_SPEW).
60 * @return SRD_OK upon success, SRD_ERR_ARG upon invalid loglevel.
61 */
62SRD_API int srd_log_loglevel_set(int loglevel)
63{
64 if (loglevel < SRD_LOG_NONE || loglevel > SRD_LOG_SPEW) {
65 srd_err("Invalid loglevel %d.", loglevel);
66 return SRD_ERR_ARG;
67 }
68
69 srd_loglevel = loglevel;
70
71 srd_dbg("libsigrokdecode loglevel set to %d.", loglevel);
72
73 return SRD_OK;
74}
75
76/**
77 * Get the libsigrokdecode loglevel.
78 *
79 * @return The currently configured libsigrokdecode loglevel.
80 */
81SRD_API int srd_log_loglevel_get(void)
82{
83 return srd_loglevel;
84}
85
86/**
87 * Set the libsigrokdecode logdomain string.
88 *
89 * @param logdomain The string to use as logdomain for libsigrokdecode 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.
97 * @return SRD_OK upon success, SRD_ERR_ARG upon invalid logdomain.
98 */
99SRD_API int srd_log_logdomain_set(const char *logdomain)
100{
101 if (!logdomain) {
102 srd_err("log: %s: logdomain was NULL", __func__);
103 return SRD_ERR_ARG;
104 }
105
106 /* TODO: Error handling. */
107 snprintf((char *)&srd_log_domain, LOGDOMAIN_MAXLEN, "%s", logdomain);
108
109 srd_dbg("Log domain set to '%s'.", (const char *)&srd_log_domain);
110
111 return SRD_OK;
112}
113
114/**
115 * Get the currently configured libsigrokdecode logdomain.
116 *
117 * @return A copy of the currently configured libsigrokdecode logdomain
118 * string. The caller is responsible for g_free()ing the string when
119 * it is no longer needed.
120 */
121SRD_API char *srd_log_logdomain_get(void)
122{
123 return g_strdup((const char *)&srd_log_domain);
124}
125
126/**
127 * Set the libsigrokdecode 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 user_data Pointer to private data to be passed on. This can be used
132 * by the caller to pass arbitrary data to the log functions.
133 * This pointer is only stored or passed on by libsigrokdecode,
134 * and is never used or interpreted in any way. The pointer
135 * is allowed to be NULL if the caller doesn't need/want to
136 * pass any data.
137 * @return SRD_OK upon success, SRD_ERR_ARG upon invalid arguments.
138 */
139SRD_API int srd_log_handler_set(srd_log_handler_t handler, void *user_data)
140{
141 if (!handler) {
142 srd_err("log: %s: handler was NULL", __func__);
143 return SRD_ERR_ARG;
144 }
145
146 /* Note: 'user_data' is allowed to be NULL. */
147
148 srd_log_handler = handler;
149 srd_log_handler_data = user_data;
150
151 return SRD_OK;
152}
153
154/**
155 * Set the libsigrokdecode log handler to the default built-in one.
156 *
157 * Additionally, the internal 'srd_log_handler_data' pointer is set to NULL.
158 *
159 * @return SRD_OK upon success, a negative error code otherwise.
160 */
161SRD_API int srd_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 srd_log_handler = srd_logv;
168 srd_log_handler_data = NULL;
169
170 return SRD_OK;
171}
172
173static int srd_logv(void *user_data, int loglevel, const char *format,
174 va_list args)
175{
176 int ret;
177
178 /* This specific log handler doesn't need the void pointer data. */
179 (void)user_data;
180
181 /* Only output messages of at least the selected loglevel(s). */
182 if (loglevel > srd_loglevel)
183 return SRD_OK; /* TODO? */
184
185 if (srd_log_domain[0] != '\0')
186 fprintf(stderr, "%s", srd_log_domain);
187 ret = vfprintf(stderr, format, args);
188 fprintf(stderr, "\n");
189
190 return ret;
191}
192
193SRD_PRIV int srd_log(int loglevel, const char *format, ...)
194{
195 int ret;
196 va_list args;
197
198 va_start(args, format);
199 ret = srd_log_handler(srd_log_handler_data, loglevel, format, args);
200 va_end(args);
201
202 return ret;
203}
204
205SRD_PRIV int srd_spew(const char *format, ...)
206{
207 int ret;
208 va_list args;
209
210 va_start(args, format);
211 ret = srd_log_handler(srd_log_handler_data, SRD_LOG_SPEW, format, args);
212 va_end(args);
213
214 return ret;
215}
216
217SRD_PRIV int srd_dbg(const char *format, ...)
218{
219 int ret;
220 va_list args;
221
222 va_start(args, format);
223 ret = srd_log_handler(srd_log_handler_data, SRD_LOG_DBG, format, args);
224 va_end(args);
225
226 return ret;
227}
228
229SRD_PRIV int srd_info(const char *format, ...)
230{
231 int ret;
232 va_list args;
233
234 va_start(args, format);
235 ret = srd_log_handler(srd_log_handler_data, SRD_LOG_INFO, format, args);
236 va_end(args);
237
238 return ret;
239}
240
241SRD_PRIV int srd_warn(const char *format, ...)
242{
243 int ret;
244 va_list args;
245
246 va_start(args, format);
247 ret = srd_log_handler(srd_log_handler_data, SRD_LOG_WARN, format, args);
248 va_end(args);
249
250 return ret;
251}
252
253SRD_PRIV int srd_err(const char *format, ...)
254{
255 int ret;
256 va_list args;
257
258 va_start(args, format);
259 ret = srd_log_handler(srd_log_handler_data, SRD_LOG_ERR, format, args);
260 va_end(args);
261
262 return ret;
263}