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

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

Do not print all levels to file

Only screen level + 1 is printed to the file.

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