]>
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). | |
a62186e4 | 60 | * @return SRD_OK upon success, SRD_ERR_ARG upon invalid loglevel. |
43c0a640 | 61 | */ |
55c3c5f4 | 62 | SRD_API int srd_log_loglevel_set(int loglevel) |
43c0a640 UH |
63 | { |
64 | if (loglevel < SRD_LOG_NONE || loglevel > SRD_LOG_SPEW) { | |
d906d3f9 | 65 | srd_err("Invalid loglevel %d.", loglevel); |
a62186e4 | 66 | return SRD_ERR_ARG; |
43c0a640 UH |
67 | } |
68 | ||
69 | srd_loglevel = loglevel; | |
70 | ||
7a1712c4 | 71 | srd_dbg("libsigrokdecode loglevel set to %d.", loglevel); |
43c0a640 UH |
72 | |
73 | return SRD_OK; | |
74 | } | |
75 | ||
76 | /** | |
77 | * Get the libsigrokdecode loglevel. | |
78 | * | |
79 | * @return The currently configured libsigrokdecode loglevel. | |
80 | */ | |
55c3c5f4 | 81 | SRD_API int srd_log_loglevel_get(void) |
43c0a640 UH |
82 | { |
83 | return srd_loglevel; | |
84 | } | |
85 | ||
3a8b2e78 | 86 | /** |
41106a07 | 87 | * Set the libsigrokdecode logdomain string. |
3a8b2e78 | 88 | * |
41106a07 UH |
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. | |
3a8b2e78 | 98 | */ |
55c3c5f4 | 99 | SRD_API int srd_log_logdomain_set(const char *logdomain) |
3a8b2e78 UH |
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 | ||
41106a07 | 109 | srd_dbg("Log domain set to '%s'.", (const char *)&srd_log_domain); |
3a8b2e78 UH |
110 | |
111 | return SRD_OK; | |
112 | } | |
113 | ||
114 | /** | |
41106a07 | 115 | * Get the currently configured libsigrokdecode logdomain. |
3a8b2e78 | 116 | * |
41106a07 UH |
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. | |
3a8b2e78 | 120 | */ |
55c3c5f4 | 121 | SRD_API char *srd_log_logdomain_get(void) |
3a8b2e78 | 122 | { |
41106a07 | 123 | return g_strdup((const char *)&srd_log_domain); |
3a8b2e78 UH |
124 | } |
125 | ||
66e4c27c UH |
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. | |
e09023b9 UH |
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. | |
66e4c27c UH |
137 | * @return SRD_OK upon success, SRD_ERR_ARG upon invalid arguments. |
138 | */ | |
e09023b9 | 139 | SRD_API int srd_log_handler_set(srd_log_handler_t handler, void *user_data) |
66e4c27c UH |
140 | { |
141 | if (!handler) { | |
142 | srd_err("log: %s: handler was NULL", __func__); | |
143 | return SRD_ERR_ARG; | |
144 | } | |
145 | ||
e09023b9 | 146 | /* Note: 'user_data' is allowed to be NULL. */ |
66e4c27c | 147 | |
41106a07 | 148 | srd_log_handler = handler; |
e09023b9 | 149 | srd_log_handler_data = user_data; |
66e4c27c UH |
150 | |
151 | return SRD_OK; | |
152 | } | |
153 | ||
154 | /** | |
155 | * Set the libsigrokdecode log handler to the default built-in one. | |
156 | * | |
41106a07 | 157 | * Additionally, the internal 'srd_log_handler_data' pointer is set to NULL. |
66e4c27c UH |
158 | * |
159 | * @return SRD_OK upon success, a negative error code otherwise. | |
160 | */ | |
55c3c5f4 | 161 | SRD_API int srd_log_handler_set_default(void) |
66e4c27c UH |
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 | */ | |
41106a07 UH |
167 | srd_log_handler = srd_logv; |
168 | srd_log_handler_data = NULL; | |
66e4c27c UH |
169 | |
170 | return SRD_OK; | |
171 | } | |
172 | ||
e09023b9 UH |
173 | static int srd_logv(void *user_data, int loglevel, const char *format, |
174 | va_list args) | |
43c0a640 UH |
175 | { |
176 | int ret; | |
177 | ||
66e4c27c | 178 | /* This specific log handler doesn't need the void pointer data. */ |
e09023b9 | 179 | (void)user_data; |
66e4c27c | 180 | |
43c0a640 UH |
181 | /* Only output messages of at least the selected loglevel(s). */ |
182 | if (loglevel > srd_loglevel) | |
183 | return SRD_OK; /* TODO? */ | |
184 | ||
3a8b2e78 | 185 | if (srd_log_domain[0] != '\0') |
568112ce | 186 | fprintf(stderr, "%s", srd_log_domain); |
43c0a640 UH |
187 | ret = vfprintf(stderr, format, args); |
188 | fprintf(stderr, "\n"); | |
189 | ||
190 | return ret; | |
191 | } | |
192 | ||
55c3c5f4 | 193 | SRD_PRIV int srd_log(int loglevel, const char *format, ...) |
43c0a640 UH |
194 | { |
195 | int ret; | |
196 | va_list args; | |
197 | ||
198 | va_start(args, format); | |
41106a07 | 199 | ret = srd_log_handler(srd_log_handler_data, loglevel, format, args); |
43c0a640 UH |
200 | va_end(args); |
201 | ||
202 | return ret; | |
203 | } | |
204 | ||
55c3c5f4 | 205 | SRD_PRIV int srd_spew(const char *format, ...) |
43c0a640 UH |
206 | { |
207 | int ret; | |
208 | va_list args; | |
209 | ||
210 | va_start(args, format); | |
41106a07 | 211 | ret = srd_log_handler(srd_log_handler_data, SRD_LOG_SPEW, format, args); |
43c0a640 UH |
212 | va_end(args); |
213 | ||
214 | return ret; | |
215 | } | |
216 | ||
55c3c5f4 | 217 | SRD_PRIV int srd_dbg(const char *format, ...) |
43c0a640 UH |
218 | { |
219 | int ret; | |
220 | va_list args; | |
221 | ||
222 | va_start(args, format); | |
41106a07 | 223 | ret = srd_log_handler(srd_log_handler_data, SRD_LOG_DBG, format, args); |
43c0a640 UH |
224 | va_end(args); |
225 | ||
226 | return ret; | |
227 | } | |
228 | ||
55c3c5f4 | 229 | SRD_PRIV int srd_info(const char *format, ...) |
43c0a640 UH |
230 | { |
231 | int ret; | |
232 | va_list args; | |
233 | ||
234 | va_start(args, format); | |
41106a07 | 235 | ret = srd_log_handler(srd_log_handler_data, SRD_LOG_INFO, format, args); |
43c0a640 UH |
236 | va_end(args); |
237 | ||
238 | return ret; | |
239 | } | |
240 | ||
55c3c5f4 | 241 | SRD_PRIV int srd_warn(const char *format, ...) |
43c0a640 UH |
242 | { |
243 | int ret; | |
244 | va_list args; | |
245 | ||
246 | va_start(args, format); | |
41106a07 | 247 | ret = srd_log_handler(srd_log_handler_data, SRD_LOG_WARN, format, args); |
43c0a640 UH |
248 | va_end(args); |
249 | ||
250 | return ret; | |
251 | } | |
252 | ||
55c3c5f4 | 253 | SRD_PRIV int srd_err(const char *format, ...) |
43c0a640 UH |
254 | { |
255 | int ret; | |
256 | va_list args; | |
257 | ||
258 | va_start(args, format); | |
41106a07 | 259 | ret = srd_log_handler(srd_log_handler_data, SRD_LOG_ERR, format, args); |
43c0a640 UH |
260 | va_end(args); |
261 | ||
262 | return ret; | |
263 | } |