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

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

USB buffer dump uses the generic DDF function

  • Property mode set to 100644
File size: 6.0 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 * Debugging and logging 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 <ddf/log.h>
41#include <usb/debug.h>
42
43/** Level of logging messages. */
44static usb_log_level_t log_level = USB_LOG_LEVEL_WARNING;
45
46/** Prefix for logging messages. */
47static const char *log_prefix = "usb";
48
49/** Serialization mutex for logging functions. */
50static FIBRIL_MUTEX_INITIALIZE(log_serializer);
51
52/** File where to store the log. */
53static FILE *log_stream = NULL;
54
55
56/** Enable logging.
57 *
58 * @param level Maximal enabled level (including this one).
59 * @param message_prefix Prefix for each printed message.
60 */
61void usb_log_enable(usb_log_level_t level, const char *message_prefix)
62{
63 log_prefix = message_prefix;
64 log_level = level;
65 if (log_stream == NULL) {
66 char *fname;
67 int rc = asprintf(&fname, "/log/%s", message_prefix);
68 if (rc > 0) {
69 log_stream = fopen(fname, "w");
70 free(fname);
71 }
72 }
73}
74
75/** Get log level name prefix.
76 *
77 * @param level Log level.
78 * @return String prefix for the message.
79 */
80static const char *log_level_name(usb_log_level_t level)
81{
82 switch (level) {
83 case USB_LOG_LEVEL_FATAL:
84 return " FATAL";
85 case USB_LOG_LEVEL_ERROR:
86 return " ERROR";
87 case USB_LOG_LEVEL_WARNING:
88 return " WARN";
89 case USB_LOG_LEVEL_INFO:
90 return " info";
91 default:
92 return "";
93 }
94}
95
96/** Print logging message.
97 *
98 * @param level Verbosity level of the message.
99 * @param format Formatting directive.
100 */
101void usb_log_printf(usb_log_level_t level, const char *format, ...)
102{
103 FILE *screen_stream = NULL;
104 switch (level) {
105 case USB_LOG_LEVEL_FATAL:
106 case USB_LOG_LEVEL_ERROR:
107 screen_stream = stderr;
108 break;
109 default:
110 screen_stream = stdout;
111 break;
112 }
113 assert(screen_stream != NULL);
114
115 va_list args;
116
117 /*
118 * Serialize access to log files.
119 * Print to screen only messages with higher level than the one
120 * specified during logging initialization.
121 * Print also to file, to it print one more (lower) level as well.
122 */
123 fibril_mutex_lock(&log_serializer);
124
125 const char *level_name = log_level_name(level);
126
127 if ((log_stream != NULL) && (level <= log_level + 1)) {
128 va_start(args, format);
129
130 fprintf(log_stream, "[%s]%s: ", log_prefix, level_name);
131 vfprintf(log_stream, format, args);
132 fflush(log_stream);
133
134 va_end(args);
135 }
136
137 if (level <= log_level) {
138 va_start(args, format);
139
140 fprintf(screen_stream, "[%s]%s: ", log_prefix, level_name);
141 vfprintf(screen_stream, format, args);
142 fflush(screen_stream);
143
144 va_end(args);
145 }
146
147 fibril_mutex_unlock(&log_serializer);
148}
149
150
151#define REMAINDER_STR_FMT " (%zu)..."
152/* string + terminator + number width (enough for 4GB)*/
153#define REMAINDER_STR_LEN (5 + 1 + 10)
154
155/** How many bytes to group together. */
156#define BUFFER_DUMP_GROUP_SIZE 4
157
158/** Size of the string for buffer dumps. */
159#define BUFFER_DUMP_LEN 240 /* Ought to be enough for everybody ;-). */
160
161/** Fibril local storage for the dumped buffer. */
162static fibril_local char buffer_dump[2][BUFFER_DUMP_LEN];
163/** Fibril local storage for buffer switching. */
164static fibril_local int buffer_dump_index = 0;
165
166/** Dump buffer into string.
167 *
168 * The function dumps given buffer into hexadecimal format and stores it
169 * in a static fibril local string.
170 * That means that you do not have to deallocate the string (actually, you
171 * can not do that) and you do not have to guard it against concurrent
172 * calls to it.
173 * The only limitation is that each second call rewrites the buffer again
174 * (internally, two buffer are used in cyclic manner).
175 * Thus, it is necessary to copy the buffer elsewhere (that includes printing
176 * to screen or writing to file).
177 * Since this function is expected to be used for debugging prints only,
178 * that is not a big limitation.
179 *
180 * @warning You cannot use this function more than twice in the same printf
181 * (see detailed explanation).
182 *
183 * @param buffer Buffer to be printed (can be NULL).
184 * @param size Size of the buffer in bytes (can be zero).
185 * @param dumped_size How many bytes to actually dump (zero means all).
186 * @return Dumped buffer as a static (but fibril local) string.
187 */
188const char *usb_debug_str_buffer(const uint8_t *buffer, size_t size,
189 size_t dumped_size)
190{
191 /*
192 * Remove previous string.
193 */
194 bzero(buffer_dump[buffer_dump_index], BUFFER_DUMP_LEN);
195
196 /* Do the actual dump. */
197 ddf_dump_buffer(buffer_dump[buffer_dump_index], BUFFER_DUMP_LEN,
198 buffer, 1, size, dumped_size);
199
200 /* Next time, use the other buffer. */
201 buffer_dump_index = 1 - buffer_dump_index;
202
203 /* Need to take the old one due to previous line. */
204 return buffer_dump[1 - buffer_dump_index];
205}
206
207
208/**
209 * @}
210 */
Note: See TracBrowser for help on using the repository browser.