]> sigrok.org Git - libsigrokdecode.git/blame - log.c
srd: Melexis MLX90614 Infrared Thermometer decoder.
[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
43c0a640
UH
21#include "sigrokdecode.h"
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(). */
33static srd_log_handler_t srd_handler = srd_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 *srd_handler_data = NULL;
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 *
53 * @param loglevel The loglevel to set (SRD_LOG_NONE, SRD_LOG_ERR,
54 * SRD_LOG_WARN, SRD_LOG_INFO, SRD_LOG_DBG, or SRD_LOG_SPEW).
a62186e4 55 * @return SRD_OK upon success, SRD_ERR_ARG upon invalid loglevel.
43c0a640
UH
56 */
57int srd_set_loglevel(int loglevel)
58{
59 if (loglevel < SRD_LOG_NONE || loglevel > SRD_LOG_SPEW) {
d906d3f9 60 srd_err("Invalid loglevel %d.", loglevel);
a62186e4 61 return SRD_ERR_ARG;
43c0a640
UH
62 }
63
64 srd_loglevel = loglevel;
65
7a1712c4 66 srd_dbg("libsigrokdecode loglevel set to %d.", loglevel);
43c0a640
UH
67
68 return SRD_OK;
69}
70
71/**
72 * Get the libsigrokdecode loglevel.
73 *
74 * @return The currently configured libsigrokdecode loglevel.
75 */
76int srd_get_loglevel(void)
77{
78 return srd_loglevel;
79}
80
3a8b2e78
UH
81/**
82 * TODO.
83 *
84 * @param logdomain TODO
85 * @return TODO.
86 */
87int srd_log_set_logdomain(const char *logdomain)
88{
89 if (!logdomain) {
90 srd_err("log: %s: logdomain was NULL", __func__);
91 return SRD_ERR_ARG;
92 }
93
94 /* TODO: Error handling. */
95 snprintf((char *)&srd_log_domain, LOGDOMAIN_MAXLEN, "%s", logdomain);
96
97 srd_dbg("log domain set to '%s'", (const char *)&srd_log_domain);
98
99 return SRD_OK;
100}
101
102/**
103 * TODO.
104 *
105 * @return TODO.
106 */
107char *srd_log_get_logdomain(void)
108{
109 return g_strdup((char *)srd_log_domain);
110}
111
66e4c27c
UH
112/**
113 * Set the libsigrokdecode log handler to the specified function.
114 *
115 * @param handler Function pointer to the log handler function to use.
116 * Must not be NULL.
117 * @param data Pointer to private data to be passed on. This can be used by
118 * the caller pass arbitrary data to the log functions. This
119 * pointer is only stored or passed on by libsigrokdecode, and
120 * is never used or interpreted in any way.
121 * @return SRD_OK upon success, SRD_ERR_ARG upon invalid arguments.
122 */
123int srd_log_set_handler(srd_log_handler_t handler, void *data)
124{
125 if (!handler) {
126 srd_err("log: %s: handler was NULL", __func__);
127 return SRD_ERR_ARG;
128 }
129
130 /* Note: 'data' is allowed to be NULL. */
131
132 srd_handler = handler;
133 srd_handler_data = data;
134
135 return SRD_OK;
136}
137
138/**
139 * Set the libsigrokdecode log handler to the default built-in one.
140 *
141 * Additionally, the internal 'srd_handler_data' pointer is set to NULL.
142 *
143 * @return SRD_OK upon success, a negative error code otherwise.
144 */
145int srd_log_set_default_handler(void)
146{
147 /*
148 * Note: No log output in this function, as it should safely work
149 * even if the currently set log handler is buggy/broken.
150 */
151 srd_handler = srd_logv;
152 srd_handler_data = NULL;
153
154 return SRD_OK;
155}
156
157static int srd_logv(void *data, int loglevel, const char *format, va_list args)
43c0a640
UH
158{
159 int ret;
160
66e4c27c
UH
161 /* This specific log handler doesn't need the void pointer data. */
162 (void)data;
163
43c0a640
UH
164 /* Only output messages of at least the selected loglevel(s). */
165 if (loglevel > srd_loglevel)
166 return SRD_OK; /* TODO? */
167
3a8b2e78
UH
168 if (srd_log_domain[0] != '\0')
169 fprintf(stderr, srd_log_domain);
43c0a640
UH
170 ret = vfprintf(stderr, format, args);
171 fprintf(stderr, "\n");
172
173 return ret;
174}
175
176int srd_log(int loglevel, const char *format, ...)
177{
178 int ret;
179 va_list args;
180
181 va_start(args, format);
66e4c27c 182 ret = srd_handler(srd_handler_data, loglevel, format, args);
43c0a640
UH
183 va_end(args);
184
185 return ret;
186}
187
188int srd_spew(const char *format, ...)
189{
190 int ret;
191 va_list args;
192
193 va_start(args, format);
66e4c27c 194 ret = srd_handler(srd_handler_data, SRD_LOG_SPEW, format, args);
43c0a640
UH
195 va_end(args);
196
197 return ret;
198}
199
200int srd_dbg(const char *format, ...)
201{
202 int ret;
203 va_list args;
204
205 va_start(args, format);
66e4c27c 206 ret = srd_handler(srd_handler_data, SRD_LOG_DBG, format, args);
43c0a640
UH
207 va_end(args);
208
209 return ret;
210}
211
212int srd_info(const char *format, ...)
213{
214 int ret;
215 va_list args;
216
217 va_start(args, format);
66e4c27c 218 ret = srd_handler(srd_handler_data, SRD_LOG_INFO, format, args);
43c0a640
UH
219 va_end(args);
220
221 return ret;
222}
223
224int srd_warn(const char *format, ...)
225{
226 int ret;
227 va_list args;
228
229 va_start(args, format);
66e4c27c 230 ret = srd_handler(srd_handler_data, SRD_LOG_WARN, format, args);
43c0a640
UH
231 va_end(args);
232
233 return ret;
234}
235
236int srd_err(const char *format, ...)
237{
238 int ret;
239 va_list args;
240
241 va_start(args, format);
66e4c27c 242 ret = srd_handler(srd_handler_data, SRD_LOG_ERR, format, args);
43c0a640
UH
243 va_end(args);
244
245 return ret;
246}