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

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

USB logging goes into file as well

  • Property mode set to 100644
File size: 6.2 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 *stream = NULL;
209 switch (level) {
210 case USB_LOG_LEVEL_FATAL:
211 case USB_LOG_LEVEL_ERROR:
212 stream = stderr;
213 break;
214 default:
215 stream = stdout;
216 break;
217 }
218 assert(stream != NULL);
219
220 va_list args;
221 va_start(args, format);
222
223 /*
224 * Serialize access to log files.
225 * Always print to log file, to screen print only when the enabled
226 * log level is high enough.
227 */
228 fibril_mutex_lock(&log_serializer);
229
230 const char *level_name = log_level_name(level);
231
232 if (log_stream != NULL) {
233 fprintf(log_stream, "[%s]%s: ", log_prefix, level_name);
234 vfprintf(log_stream, format, args);
235 }
236
237 if (level <= log_level) {
238 fprintf(stream, "[%s]%s: ", log_prefix, level_name);
239 vfprintf(stream, format, args);
240 }
241
242 fibril_mutex_unlock(&log_serializer);
243
244 va_end(args);
245}
246
247/**
248 * @}
249 */
Note: See TracBrowser for help on using the repository browser.