source: mainline/uspace/lib/usb/src/debug.c@ d78a32f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d78a32f was d78a32f, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

Fix bad comment

  • Property mode set to 100644
File size: 9.1 KB
Line 
1/*
2 * Copyright (c) 2010-2011 Vojtech Horky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libusb
30 * @{
31 */
32/** @file
33 * @brief Debugging support.
34 */
35#include <adt/list.h>
36#include <fibril_synch.h>
37#include <errno.h>
38#include <stdlib.h>
39#include <stdio.h>
40#include <usb/debug.h>
41
42/** Debugging tag. */
43typedef struct {
44 /** Linked list member. */
45 link_t link;
46 /** Tag name.
47 * We always have a private copy of the name.
48 */
49 char *tag;
50 /** Enabled level of debugging. */
51 int level;
52} usb_debug_tag_t;
53
54/** Get instance of usb_debug_tag_t from link_t. */
55#define USB_DEBUG_TAG_INSTANCE(iterator) \
56 list_get_instance(iterator, usb_debug_tag_t, link)
57
58/** List of all known tags. */
59static LIST_INITIALIZE(tag_list);
60/** Mutex guard for the list of all tags. */
61static FIBRIL_MUTEX_INITIALIZE(tag_list_guard);
62
63/** Level of logging messages. */
64static usb_log_level_t log_level = USB_LOG_LEVEL_WARNING;
65/** Prefix for logging messages. */
66static const char *log_prefix = "usb";
67/** Serialization mutex for logging functions. */
68static FIBRIL_MUTEX_INITIALIZE(log_serializer);
69static FILE *log_stream = NULL;
70
71/** Find or create new tag with given name.
72 *
73 * @param tagname Tag name.
74 * @return Debug tag structure.
75 * @retval NULL Out of memory.
76 */
77static usb_debug_tag_t *get_tag(const char *tagname)
78{
79 link_t *link;
80 for (link = tag_list.next; \
81 link != &tag_list; \
82 link = link->next) {
83 usb_debug_tag_t *tag = USB_DEBUG_TAG_INSTANCE(link);
84 if (str_cmp(tag->tag, tagname) == 0) {
85 return tag;
86 }
87 }
88
89 /*
90 * Tag not found, we will create a new one.
91 */
92 usb_debug_tag_t *new_tag = malloc(sizeof(usb_debug_tag_t));
93 int rc = asprintf(&new_tag->tag, "%s", tagname);
94 if (rc < 0) {
95 free(new_tag);
96 return NULL;
97 }
98 list_initialize(&new_tag->link);
99 new_tag->level = 1;
100
101 /*
102 * Append it to the end of known tags.
103 */
104 list_append(&new_tag->link, &tag_list);
105
106 return new_tag;
107}
108
109/** Print debugging information.
110 * If the tag is used for the first time, its structures are automatically
111 * created and initial verbosity level is set to 1.
112 *
113 * @param tagname Tag name.
114 * @param level Level (verbosity) of the message.
115 * @param format Formatting string for printf().
116 */
117void usb_dprintf(const char *tagname, int level, const char *format, ...)
118{
119 fibril_mutex_lock(&tag_list_guard);
120 usb_debug_tag_t *tag = get_tag(tagname);
121 if (tag == NULL) {
122 printf("USB debug: FATAL ERROR - failed to create tag.\n");
123 goto leave;
124 }
125
126 if (tag->level < level) {
127 goto leave;
128 }
129
130 va_list args;
131 va_start(args, format);
132
133 printf("[%s:%d]: ", tagname, level);
134 vprintf(format, args);
135
136 va_end(args);
137
138leave:
139 fibril_mutex_unlock(&tag_list_guard);
140}
141
142/** Enable debugging prints for given tag.
143 *
144 * Setting level to <i>n</i> will cause that only printing messages
145 * with level lower or equal to <i>n</i> will be printed.
146 *
147 * @param tagname Tag name.
148 * @param level Enabled level.
149 */
150void usb_dprintf_enable(const char *tagname, int level)
151{
152 fibril_mutex_lock(&tag_list_guard);
153 usb_debug_tag_t *tag = get_tag(tagname);
154 if (tag == NULL) {
155 printf("USB debug: FATAL ERROR - failed to create tag.\n");
156 goto leave;
157 }
158
159 tag->level = level;
160
161leave:
162 fibril_mutex_unlock(&tag_list_guard);
163}
164
165/** Enable logging.
166 *
167 * @param level Maximal enabled level (including this one).
168 * @param message_prefix Prefix for each printed message.
169 */
170void usb_log_enable(usb_log_level_t level, const char *message_prefix)
171{
172 log_prefix = message_prefix;
173 log_level = level;
174 if (log_stream == NULL) {
175 char *fname;
176 int rc = asprintf(&fname, "/log/%s", message_prefix);
177 if (rc > 0) {
178 log_stream = fopen(fname, "w");
179 free(fname);
180 }
181 }
182}
183
184
185static const char *log_level_name(usb_log_level_t level)
186{
187 switch (level) {
188 case USB_LOG_LEVEL_FATAL:
189 return " FATAL";
190 case USB_LOG_LEVEL_ERROR:
191 return " ERROR";
192 case USB_LOG_LEVEL_WARNING:
193 return " WARN";
194 case USB_LOG_LEVEL_INFO:
195 return " info";
196 default:
197 return "";
198 }
199}
200
201/** Print logging message.
202 *
203 * @param level Verbosity level of the message.
204 * @param format Formatting directive.
205 */
206void usb_log_printf(usb_log_level_t level, const char *format, ...)
207{
208 FILE *screen_stream = NULL;
209 switch (level) {
210 case USB_LOG_LEVEL_FATAL:
211 case USB_LOG_LEVEL_ERROR:
212 screen_stream = stderr;
213 break;
214 default:
215 screen_stream = stdout;
216 break;
217 }
218 assert(screen_stream != NULL);
219
220 va_list args;
221
222 /*
223 * Serialize access to log files.
224 * Always print to log file, to screen print only when the enabled
225 * log level is high enough.
226 */
227 fibril_mutex_lock(&log_serializer);
228
229 const char *level_name = log_level_name(level);
230
231 if (log_stream != NULL) {
232 va_start(args, format);
233
234 fprintf(log_stream, "[%s]%s: ", log_prefix, level_name);
235 vfprintf(log_stream, format, args);
236 fflush(log_stream);
237
238 va_end(args);
239 }
240
241 if (level <= log_level) {
242 va_start(args, format);
243
244 fprintf(screen_stream, "[%s]%s: ", log_prefix, level_name);
245 vfprintf(screen_stream, format, args);
246 fflush(screen_stream);
247
248 va_end(args);
249 }
250
251 fibril_mutex_unlock(&log_serializer);
252}
253
254
255#define REMAINDER_STR_FMT " (%zu)..."
256/* string + terminator + number width (enough for 4GB)*/
257#define REMAINDER_STR_LEN (5 + 1 + 10)
258#define BUFFER_DUMP_GROUP_SIZE 4
259#define BUFFER_DUMP_LEN 240 /* Ought to be enough for everybody ;-). */
260static fibril_local char buffer_dump[BUFFER_DUMP_LEN];
261
262/** Dump buffer into string.
263 *
264 * The function dumps given buffer into hexadecimal format and stores it
265 * in a static fibril local string.
266 * That means that you do not have to deallocate the string (actually, you
267 * can not do that) and you do not have to guard it against concurrent
268 * calls to it.
269 * The only limitation is that each call rewrites the buffer again.
270 * Thus, it is necessary to copy the buffer elsewhere (that includes printing
271 * to screen or writing to file).
272 * Since this function is expected to be used for debugging prints only,
273 * that is not a big limitation.
274 *
275 * @warning You cannot use this function twice in the same printf
276 * (see detailed explanation).
277 *
278 * @param buffer Buffer to be printed (can be NULL).
279 * @param size Size of the buffer in bytes (can be zero).
280 * @param dumped_size How many bytes to actually dump (zero means all).
281 * @return Dumped buffer as a static (but fibril local) string.
282 */
283const char *usb_debug_str_buffer(uint8_t *buffer, size_t size,
284 size_t dumped_size)
285{
286 /*
287 * Remove previous string (that might also reveal double usage of
288 * this function).
289 */
290 bzero(buffer_dump, BUFFER_DUMP_LEN);
291
292 if (buffer == NULL) {
293 return "(null)";
294 }
295 if (size == 0) {
296 return "(empty)";
297 }
298 if ((dumped_size == 0) || (dumped_size > size)) {
299 dumped_size = size;
300 }
301
302 /* How many bytes are available in the output buffer. */
303 size_t buffer_remaining_size = BUFFER_DUMP_LEN - 1 - REMAINDER_STR_LEN;
304 char *it = buffer_dump;
305
306 size_t index = 0;
307
308 while (index < size) {
309 /* Determine space before the number. */
310 const char *space_before;
311 if (index == 0) {
312 space_before = "";
313 } else if ((index % BUFFER_DUMP_GROUP_SIZE) == 0) {
314 space_before = " ";
315 } else {
316 space_before = " ";
317 }
318
319 /*
320 * Add the byte as a hexadecimal number plus the space.
321 * We do it into temporary buffer to ensure that always
322 * the whole byte is printed.
323 */
324 int val = buffer[index];
325 char current_byte[16];
326 int printed = snprintf(current_byte, 16,
327 "%s%02x", space_before, val);
328 if (printed < 0) {
329 break;
330 }
331
332 if ((size_t) printed > buffer_remaining_size) {
333 break;
334 }
335
336 /* We can safely add 1, because space for end 0 is reserved. */
337 str_append(it, buffer_remaining_size + 1, current_byte);
338
339 buffer_remaining_size -= printed;
340 /* Point at the terminator 0. */
341 it += printed;
342 index++;
343
344 if (index >= dumped_size) {
345 break;
346 }
347 }
348
349 /* Add how many bytes were not printed. */
350 if (index < size) {
351 snprintf(it, REMAINDER_STR_LEN,
352 REMAINDER_STR_FMT, size - index);
353 }
354
355 return buffer_dump;
356}
357
358
359/**
360 * @}
361 */
Note: See TracBrowser for help on using the repository browser.