]>
Commit | Line | Data |
---|---|---|
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 <stdarg.h> | |
22 | #include <stdio.h> | |
23 | #include "sigrok.h" | |
24 | #include "sigrok-internal.h" | |
25 | ||
26 | /* Currently selected libsigrok loglevel. Default: SR_LOG_WARN. */ | |
27 | static int sr_loglevel = SR_LOG_WARN; /* Show errors+warnings per default. */ | |
28 | ||
29 | /* Function prototype. */ | |
30 | static int sr_logv(void *data, int loglevel, const char *format, va_list args); | |
31 | ||
32 | /* Pointer to the currently selected log handler. Default: sr_logv(). */ | |
33 | static sr_log_handler_t sr_log_handler = sr_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 | */ | |
39 | static void *sr_log_handler_data = NULL; | |
40 | ||
41 | /* Log domain (a short string that is used as prefix for all messages). */ | |
42 | #define LOGDOMAIN_MAXLEN 30 | |
43 | #define LOGDOMAIN_DEFAULT "sr: " | |
44 | static char sr_log_domain[LOGDOMAIN_MAXLEN + 1] = LOGDOMAIN_DEFAULT; | |
45 | ||
46 | /** | |
47 | * Set the libsigrok loglevel. | |
48 | * | |
49 | * This influences the amount of log messages (debug messages, error messages, | |
50 | * and so on) libsigrok will output. Using SR_LOG_NONE disables all messages. | |
51 | * | |
52 | * Note that this function itself will also output log messages. After the | |
53 | * loglevel has changed, it will output a debug message with SR_LOG_DBG for | |
54 | * example. Whether this message is shown depends on the (new) loglevel. | |
55 | * | |
56 | * @param loglevel The loglevel to set (SR_LOG_NONE, SR_LOG_ERR, SR_LOG_WARN, | |
57 | * SR_LOG_INFO, SR_LOG_DBG, or SR_LOG_SPEW). | |
58 | * @return SR_OK upon success, SR_ERR_ARG upon invalid loglevel. | |
59 | */ | |
60 | SR_API int sr_log_loglevel_set(int loglevel) | |
61 | { | |
62 | if (loglevel < SR_LOG_NONE || loglevel > SR_LOG_SPEW) { | |
63 | sr_err("Invalid loglevel %d.", loglevel); | |
64 | return SR_ERR_ARG; | |
65 | } | |
66 | ||
67 | sr_loglevel = loglevel; | |
68 | ||
69 | sr_dbg("libsigrok loglevel set to %d.", loglevel); | |
70 | ||
71 | return SR_OK; | |
72 | } | |
73 | ||
74 | /** | |
75 | * Get the libsigrok loglevel. | |
76 | * | |
77 | * @return The currently configured libsigrok loglevel. | |
78 | */ | |
79 | SR_API int sr_log_loglevel_get(void) | |
80 | { | |
81 | return sr_loglevel; | |
82 | } | |
83 | ||
84 | /** | |
85 | * Set the libsigrok logdomain string. | |
86 | * | |
87 | * @param logdomain The string to use as logdomain for libsigrok log | |
88 | * messages from now on. Must not be NULL. The maximum | |
89 | * length of the string is 30 characters (this does not | |
90 | * include the trailing NUL-byte). Longer strings are | |
91 | * silently truncated. | |
92 | * In order to not use a logdomain, pass an empty string. | |
93 | * The function makes its own copy of the input string, i.e. | |
94 | * the caller does not need to keep it around. | |
95 | * @return SR_OK upon success, SR_ERR_ARG upon invalid logdomain. | |
96 | */ | |
97 | SR_API int sr_log_logdomain_set(const char *logdomain) | |
98 | { | |
99 | if (!logdomain) { | |
100 | sr_err("log: %s: logdomain was NULL", __func__); | |
101 | return SR_ERR_ARG; | |
102 | } | |
103 | ||
104 | /* TODO: Error handling. */ | |
105 | snprintf((char *)&sr_log_domain, LOGDOMAIN_MAXLEN, "%s", logdomain); | |
106 | ||
107 | sr_dbg("Log domain set to '%s'.", (const char *)&sr_log_domain); | |
108 | ||
109 | return SR_OK; | |
110 | } | |
111 | ||
112 | /** | |
113 | * Get the currently configured libsigrok logdomain. | |
114 | * | |
115 | * @return A copy of the currently configured libsigrok logdomain | |
116 | * string. The caller is responsible for g_free()ing the string when | |
117 | * it is no longer needed. | |
118 | */ | |
119 | SR_API char *sr_log_logdomain_get(void) | |
120 | { | |
121 | return g_strdup((const char *)&sr_log_domain); | |
122 | } | |
123 | ||
124 | /** | |
125 | * Set the libsigrok log handler to the specified function. | |
126 | * | |
127 | * @param handler Function pointer to the log handler function to use. | |
128 | * Must not be NULL. | |
129 | * @param data Pointer to private data to be passed on. This can be used by | |
130 | * the caller to pass arbitrary data to the log functions. This | |
131 | * pointer is only stored or passed on by libsigrok, and | |
132 | * is never used or interpreted in any way. The pointer is allowed | |
133 | * to be NULL if the caller doesn't need/want to pass any data. | |
134 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments. | |
135 | */ | |
136 | SR_API int sr_log_handler_set(sr_log_handler_t handler, void *data) | |
137 | { | |
138 | if (!handler) { | |
139 | sr_err("log: %s: handler was NULL", __func__); | |
140 | return SR_ERR_ARG; | |
141 | } | |
142 | ||
143 | /* Note: 'data' is allowed to be NULL. */ | |
144 | ||
145 | sr_log_handler = handler; | |
146 | sr_log_handler_data = data; | |
147 | ||
148 | return SR_OK; | |
149 | } | |
150 | ||
151 | /** | |
152 | * Set the libsigrok log handler to the default built-in one. | |
153 | * | |
154 | * Additionally, the internal 'sr_log_handler_data' pointer is set to NULL. | |
155 | * | |
156 | * @return SR_OK upon success, a negative error code otherwise. | |
157 | */ | |
158 | SR_API int sr_log_handler_set_default(void) | |
159 | { | |
160 | /* | |
161 | * Note: No log output in this function, as it should safely work | |
162 | * even if the currently set log handler is buggy/broken. | |
163 | */ | |
164 | sr_log_handler = sr_logv; | |
165 | sr_log_handler_data = NULL; | |
166 | ||
167 | return SR_OK; | |
168 | } | |
169 | ||
170 | static int sr_logv(void *data, int loglevel, const char *format, va_list args) | |
171 | { | |
172 | int ret; | |
173 | ||
174 | /* This specific log handler doesn't need the void pointer data. */ | |
175 | (void)data; | |
176 | ||
177 | /* Only output messages of at least the selected loglevel(s). */ | |
178 | if (loglevel > sr_loglevel) | |
179 | return SR_OK; /* TODO? */ | |
180 | ||
181 | if (sr_log_domain[0] != '\0') | |
182 | fprintf(stderr, "%s", sr_log_domain); | |
183 | ret = vfprintf(stderr, format, args); | |
184 | fprintf(stderr, "\n"); | |
185 | ||
186 | return ret; | |
187 | } | |
188 | ||
189 | SR_PRIV int sr_log(int loglevel, const char *format, ...) | |
190 | { | |
191 | int ret; | |
192 | va_list args; | |
193 | ||
194 | va_start(args, format); | |
195 | ret = sr_log_handler(sr_log_handler_data, loglevel, format, args); | |
196 | va_end(args); | |
197 | ||
198 | return ret; | |
199 | } | |
200 | ||
201 | SR_PRIV int sr_spew(const char *format, ...) | |
202 | { | |
203 | int ret; | |
204 | va_list args; | |
205 | ||
206 | va_start(args, format); | |
207 | ret = sr_log_handler(sr_log_handler_data, SR_LOG_SPEW, format, args); | |
208 | va_end(args); | |
209 | ||
210 | return ret; | |
211 | } | |
212 | ||
213 | SR_PRIV int sr_dbg(const char *format, ...) | |
214 | { | |
215 | int ret; | |
216 | va_list args; | |
217 | ||
218 | va_start(args, format); | |
219 | ret = sr_log_handler(sr_log_handler_data, SR_LOG_DBG, format, args); | |
220 | va_end(args); | |
221 | ||
222 | return ret; | |
223 | } | |
224 | ||
225 | SR_PRIV int sr_info(const char *format, ...) | |
226 | { | |
227 | int ret; | |
228 | va_list args; | |
229 | ||
230 | va_start(args, format); | |
231 | ret = sr_log_handler(sr_log_handler_data, SR_LOG_INFO, format, args); | |
232 | va_end(args); | |
233 | ||
234 | return ret; | |
235 | } | |
236 | ||
237 | SR_PRIV int sr_warn(const char *format, ...) | |
238 | { | |
239 | int ret; | |
240 | va_list args; | |
241 | ||
242 | va_start(args, format); | |
243 | ret = sr_log_handler(sr_log_handler_data, SR_LOG_WARN, format, args); | |
244 | va_end(args); | |
245 | ||
246 | return ret; | |
247 | } | |
248 | ||
249 | SR_PRIV int sr_err(const char *format, ...) | |
250 | { | |
251 | int ret; | |
252 | va_list args; | |
253 | ||
254 | va_start(args, format); | |
255 | ret = sr_log_handler(sr_log_handler_data, SR_LOG_ERR, format, args); | |
256 | va_end(args); | |
257 | ||
258 | return ret; | |
259 | } |