]>
Commit | Line | Data |
---|---|---|
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 |
27 | static int srd_loglevel = SRD_LOG_WARN; /* Show errors+warnings per default. */ |
28 | ||
66e4c27c | 29 | /* Function prototype. */ |
e09023b9 UH |
30 | static int srd_logv(void *user_data, int loglevel, const char *format, |
31 | va_list args); | |
66e4c27c UH |
32 | |
33 | /* Pointer to the currently selected log handler. Default: srd_logv(). */ | |
41106a07 | 34 | static srd_log_handler_t srd_log_handler = srd_logv; |
66e4c27c UH |
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 | */ | |
41106a07 | 40 | static void *srd_log_handler_data = NULL; |
66e4c27c | 41 | |
3a8b2e78 UH |
42 | /* Log domain (a short string that is used as prefix for all messages). */ |
43 | #define LOGDOMAIN_MAXLEN 30 | |
44 | #define LOGDOMAIN_DEFAULT "srd: " | |
45 | static char srd_log_domain[LOGDOMAIN_MAXLEN + 1] = LOGDOMAIN_DEFAULT; | |
46 | ||
43c0a640 UH |
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 | * | |
41106a07 UH |
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 | * | |
43c0a640 UH |
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). | |
582c8473 | 60 | * |
a62186e4 | 61 | * @return SRD_OK upon success, SRD_ERR_ARG upon invalid loglevel. |
43c0a640 | 62 | */ |
55c3c5f4 | 63 | SRD_API int srd_log_loglevel_set(int loglevel) |
43c0a640 UH |
64 | { |
65 | if (loglevel < SRD_LOG_NONE || loglevel > SRD_LOG_SPEW) { | |
d906d3f9 | 66 | srd_err("Invalid loglevel %d.", loglevel); |
a62186e4 | 67 | return SRD_ERR_ARG; |
43c0a640 UH |
68 | } |
69 | ||
70 | srd_loglevel = loglevel; | |
71 | ||
7a1712c4 | 72 | srd_dbg("libsigrokdecode loglevel set to %d.", loglevel); |
43c0a640 UH |
73 | |
74 | return SRD_OK; | |
75 | } | |
76 | ||
77 | /** | |
78 | * Get the libsigrokdecode loglevel. | |
79 | * | |
80 | * @return The currently configured libsigrokdecode loglevel. | |
81 | */ | |
55c3c5f4 | 82 | SRD_API int srd_log_loglevel_get(void) |
43c0a640 UH |
83 | { |
84 | return srd_loglevel; | |
85 | } | |
86 | ||
3a8b2e78 | 87 | /** |
41106a07 | 88 | * Set the libsigrokdecode logdomain string. |
3a8b2e78 | 89 | * |
41106a07 UH |
90 | * @param logdomain The string to use as logdomain for libsigrokdecode log |
91 | * messages from now on. Must not be NULL. The maximum | |
92 | * length of the string is 30 characters (this does not | |
93 | * include the trailing NUL-byte). Longer strings are | |
94 | * silently truncated. | |
95 | * In order to not use a logdomain, pass an empty string. | |
96 | * The function makes its own copy of the input string, i.e. | |
97 | * the caller does not need to keep it around. | |
582c8473 | 98 | * |
41106a07 | 99 | * @return SRD_OK upon success, SRD_ERR_ARG upon invalid logdomain. |
3a8b2e78 | 100 | */ |
55c3c5f4 | 101 | SRD_API int srd_log_logdomain_set(const char *logdomain) |
3a8b2e78 UH |
102 | { |
103 | if (!logdomain) { | |
104 | srd_err("log: %s: logdomain was NULL", __func__); | |
105 | return SRD_ERR_ARG; | |
106 | } | |
107 | ||
108 | /* TODO: Error handling. */ | |
109 | snprintf((char *)&srd_log_domain, LOGDOMAIN_MAXLEN, "%s", logdomain); | |
110 | ||
41106a07 | 111 | srd_dbg("Log domain set to '%s'.", (const char *)&srd_log_domain); |
3a8b2e78 UH |
112 | |
113 | return SRD_OK; | |
114 | } | |
115 | ||
116 | /** | |
41106a07 | 117 | * Get the currently configured libsigrokdecode logdomain. |
3a8b2e78 | 118 | * |
41106a07 UH |
119 | * @return A copy of the currently configured libsigrokdecode logdomain |
120 | * string. The caller is responsible for g_free()ing the string when | |
121 | * it is no longer needed. | |
3a8b2e78 | 122 | */ |
55c3c5f4 | 123 | SRD_API char *srd_log_logdomain_get(void) |
3a8b2e78 | 124 | { |
41106a07 | 125 | return g_strdup((const char *)&srd_log_domain); |
3a8b2e78 UH |
126 | } |
127 | ||
66e4c27c UH |
128 | /** |
129 | * Set the libsigrokdecode log handler to the specified function. | |
130 | * | |
131 | * @param handler Function pointer to the log handler function to use. | |
132 | * Must not be NULL. | |
e09023b9 UH |
133 | * @param user_data Pointer to private data to be passed on. This can be used |
134 | * by the caller to pass arbitrary data to the log functions. | |
135 | * This pointer is only stored or passed on by libsigrokdecode, | |
136 | * and is never used or interpreted in any way. The pointer | |
137 | * is allowed to be NULL if the caller doesn't need/want to | |
138 | * pass any data. | |
582c8473 | 139 | * |
66e4c27c UH |
140 | * @return SRD_OK upon success, SRD_ERR_ARG upon invalid arguments. |
141 | */ | |
e09023b9 | 142 | SRD_API int srd_log_handler_set(srd_log_handler_t handler, void *user_data) |
66e4c27c UH |
143 | { |
144 | if (!handler) { | |
145 | srd_err("log: %s: handler was NULL", __func__); | |
146 | return SRD_ERR_ARG; | |
147 | } | |
148 | ||
e09023b9 | 149 | /* Note: 'user_data' is allowed to be NULL. */ |
66e4c27c | 150 | |
41106a07 | 151 | srd_log_handler = handler; |
e09023b9 | 152 | srd_log_handler_data = user_data; |
66e4c27c UH |
153 | |
154 | return SRD_OK; | |
155 | } | |
156 | ||
157 | /** | |
158 | * Set the libsigrokdecode log handler to the default built-in one. | |
159 | * | |
41106a07 | 160 | * Additionally, the internal 'srd_log_handler_data' pointer is set to NULL. |
66e4c27c | 161 | * |
582c8473 | 162 | * @return SRD_OK upon success, a (negative) error code otherwise. |
66e4c27c | 163 | */ |
55c3c5f4 | 164 | SRD_API int srd_log_handler_set_default(void) |
66e4c27c UH |
165 | { |
166 | /* | |
167 | * Note: No log output in this function, as it should safely work | |
168 | * even if the currently set log handler is buggy/broken. | |
169 | */ | |
41106a07 UH |
170 | srd_log_handler = srd_logv; |
171 | srd_log_handler_data = NULL; | |
66e4c27c UH |
172 | |
173 | return SRD_OK; | |
174 | } | |
175 | ||
e09023b9 UH |
176 | static int srd_logv(void *user_data, int loglevel, const char *format, |
177 | va_list args) | |
43c0a640 UH |
178 | { |
179 | int ret; | |
180 | ||
66e4c27c | 181 | /* This specific log handler doesn't need the void pointer data. */ |
e09023b9 | 182 | (void)user_data; |
66e4c27c | 183 | |
43c0a640 UH |
184 | /* Only output messages of at least the selected loglevel(s). */ |
185 | if (loglevel > srd_loglevel) | |
186 | return SRD_OK; /* TODO? */ | |
187 | ||
3a8b2e78 | 188 | if (srd_log_domain[0] != '\0') |
568112ce | 189 | fprintf(stderr, "%s", srd_log_domain); |
43c0a640 UH |
190 | ret = vfprintf(stderr, format, args); |
191 | fprintf(stderr, "\n"); | |
192 | ||
193 | return ret; | |
194 | } | |
195 | ||
55c3c5f4 | 196 | SRD_PRIV int srd_log(int loglevel, const char *format, ...) |
43c0a640 UH |
197 | { |
198 | int ret; | |
199 | va_list args; | |
200 | ||
201 | va_start(args, format); | |
41106a07 | 202 | ret = srd_log_handler(srd_log_handler_data, loglevel, format, args); |
43c0a640 UH |
203 | va_end(args); |
204 | ||
205 | return ret; | |
206 | } | |
207 | ||
55c3c5f4 | 208 | SRD_PRIV int srd_spew(const char *format, ...) |
43c0a640 UH |
209 | { |
210 | int ret; | |
211 | va_list args; | |
212 | ||
213 | va_start(args, format); | |
41106a07 | 214 | ret = srd_log_handler(srd_log_handler_data, SRD_LOG_SPEW, format, args); |
43c0a640 UH |
215 | va_end(args); |
216 | ||
217 | return ret; | |
218 | } | |
219 | ||
55c3c5f4 | 220 | SRD_PRIV int srd_dbg(const char *format, ...) |
43c0a640 UH |
221 | { |
222 | int ret; | |
223 | va_list args; | |
224 | ||
225 | va_start(args, format); | |
41106a07 | 226 | ret = srd_log_handler(srd_log_handler_data, SRD_LOG_DBG, format, args); |
43c0a640 UH |
227 | va_end(args); |
228 | ||
229 | return ret; | |
230 | } | |
231 | ||
55c3c5f4 | 232 | SRD_PRIV int srd_info(const char *format, ...) |
43c0a640 UH |
233 | { |
234 | int ret; | |
235 | va_list args; | |
236 | ||
237 | va_start(args, format); | |
41106a07 | 238 | ret = srd_log_handler(srd_log_handler_data, SRD_LOG_INFO, format, args); |
43c0a640 UH |
239 | va_end(args); |
240 | ||
241 | return ret; | |
242 | } | |
243 | ||
55c3c5f4 | 244 | SRD_PRIV int srd_warn(const char *format, ...) |
43c0a640 UH |
245 | { |
246 | int ret; | |
247 | va_list args; | |
248 | ||
249 | va_start(args, format); | |
41106a07 | 250 | ret = srd_log_handler(srd_log_handler_data, SRD_LOG_WARN, format, args); |
43c0a640 UH |
251 | va_end(args); |
252 | ||
253 | return ret; | |
254 | } | |
255 | ||
55c3c5f4 | 256 | SRD_PRIV int srd_err(const char *format, ...) |
43c0a640 UH |
257 | { |
258 | int ret; | |
259 | va_list args; | |
260 | ||
261 | va_start(args, format); | |
41106a07 | 262 | ret = srd_log_handler(srd_log_handler_data, SRD_LOG_ERR, format, args); |
43c0a640 UH |
263 | va_end(args); |
264 | ||
265 | return ret; | |
266 | } |