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