[bb95594] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 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 |
|
---|
[3b77628] | 29 | /** @addtogroup libusb
|
---|
[bb95594] | 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. */
|
---|
| 43 | typedef 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. */
|
---|
| 59 | static LIST_INITIALIZE(tag_list);
|
---|
| 60 | /** Mutex guard for the list of all tags. */
|
---|
| 61 | static FIBRIL_MUTEX_INITIALIZE(tag_list_guard);
|
---|
| 62 |
|
---|
| 63 | /** Find or create new tag with given name.
|
---|
| 64 | *
|
---|
| 65 | * @param tagname Tag name.
|
---|
| 66 | * @return Debug tag structure.
|
---|
| 67 | * @retval NULL Out of memory.
|
---|
| 68 | */
|
---|
| 69 | static usb_debug_tag_t *get_tag(const char *tagname)
|
---|
| 70 | {
|
---|
| 71 | link_t *link;
|
---|
| 72 | for (link = tag_list.next; \
|
---|
| 73 | link != &tag_list; \
|
---|
| 74 | link = link->next) {
|
---|
| 75 | usb_debug_tag_t *tag = USB_DEBUG_TAG_INSTANCE(link);
|
---|
| 76 | if (str_cmp(tag->tag, tagname) == 0) {
|
---|
| 77 | return tag;
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | /*
|
---|
| 82 | * Tag not found, we will create a new one.
|
---|
| 83 | */
|
---|
| 84 | usb_debug_tag_t *new_tag = malloc(sizeof(usb_debug_tag_t));
|
---|
| 85 | int rc = asprintf(&new_tag->tag, "%s", tagname);
|
---|
| 86 | if (rc < 0) {
|
---|
| 87 | free(new_tag);
|
---|
| 88 | return NULL;
|
---|
| 89 | }
|
---|
| 90 | list_initialize(&new_tag->link);
|
---|
| 91 | new_tag->level = 1;
|
---|
| 92 |
|
---|
| 93 | /*
|
---|
| 94 | * Append it to the end of known tags.
|
---|
| 95 | */
|
---|
| 96 | list_append(&new_tag->link, &tag_list);
|
---|
| 97 |
|
---|
| 98 | return new_tag;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | /** Print debugging information.
|
---|
| 102 | * If the tag is used for the first time, its structures are automatically
|
---|
| 103 | * created and initial verbosity level is set to 1.
|
---|
| 104 | *
|
---|
| 105 | * @param tagname Tag name.
|
---|
| 106 | * @param level Level (verbosity) of the message.
|
---|
| 107 | * @param format Formatting string for printf().
|
---|
| 108 | */
|
---|
| 109 | void usb_dprintf(const char *tagname, int level, const char *format, ...)
|
---|
| 110 | {
|
---|
| 111 | fibril_mutex_lock(&tag_list_guard);
|
---|
| 112 | usb_debug_tag_t *tag = get_tag(tagname);
|
---|
| 113 | if (tag == NULL) {
|
---|
| 114 | printf("USB debug: FATAL ERROR - failed to create tag.\n");
|
---|
| 115 | goto leave;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | if (tag->level < level) {
|
---|
| 119 | goto leave;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | va_list args;
|
---|
| 123 | va_start(args, format);
|
---|
| 124 |
|
---|
| 125 | printf("[%s:%d]: ", tagname, level);
|
---|
| 126 | vprintf(format, args);
|
---|
| 127 |
|
---|
| 128 | va_end(args);
|
---|
| 129 |
|
---|
| 130 | leave:
|
---|
| 131 | fibril_mutex_unlock(&tag_list_guard);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | /** Enable debugging prints for given tag.
|
---|
| 135 | *
|
---|
| 136 | * Setting level to <i>n</i> will cause that only printing messages
|
---|
| 137 | * with level lower or equal to <i>n</i> will be printed.
|
---|
| 138 | *
|
---|
| 139 | * @param tagname Tag name.
|
---|
| 140 | * @param level Enabled level.
|
---|
| 141 | */
|
---|
| 142 | void usb_dprintf_enable(const char *tagname, int level)
|
---|
| 143 | {
|
---|
| 144 | fibril_mutex_lock(&tag_list_guard);
|
---|
| 145 | usb_debug_tag_t *tag = get_tag(tagname);
|
---|
| 146 | if (tag == NULL) {
|
---|
| 147 | printf("USB debug: FATAL ERROR - failed to create tag.\n");
|
---|
| 148 | goto leave;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | tag->level = level;
|
---|
| 152 |
|
---|
| 153 | leave:
|
---|
| 154 | fibril_mutex_unlock(&tag_list_guard);
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 |
|
---|
| 158 | /**
|
---|
| 159 | * @}
|
---|
| 160 | */
|
---|