[bb95594] | 1 | /*
|
---|
[6028ec8] | 2 | * Copyright (c) 2010-2011 Vojtech Horky
|
---|
[bb95594] | 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
|
---|
[a6add7a] | 33 | * Debugging and logging support.
|
---|
[bb95594] | 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 |
|
---|
[6028ec8] | 42 | /** Level of logging messages. */
|
---|
| 43 | static usb_log_level_t log_level = USB_LOG_LEVEL_WARNING;
|
---|
[a6add7a] | 44 |
|
---|
[6028ec8] | 45 | /** Prefix for logging messages. */
|
---|
| 46 | static const char *log_prefix = "usb";
|
---|
[a6add7a] | 47 |
|
---|
[6028ec8] | 48 | /** Serialization mutex for logging functions. */
|
---|
| 49 | static FIBRIL_MUTEX_INITIALIZE(log_serializer);
|
---|
[bb95594] | 50 |
|
---|
[a6add7a] | 51 | /** File where to store the log. */
|
---|
| 52 | static FILE *log_stream = NULL;
|
---|
[bb95594] | 53 |
|
---|
| 54 |
|
---|
[6028ec8] | 55 | /** Enable logging.
|
---|
| 56 | *
|
---|
| 57 | * @param level Maximal enabled level (including this one).
|
---|
| 58 | * @param message_prefix Prefix for each printed message.
|
---|
| 59 | */
|
---|
| 60 | void usb_log_enable(usb_log_level_t level, const char *message_prefix)
|
---|
| 61 | {
|
---|
| 62 | log_prefix = message_prefix;
|
---|
| 63 | log_level = level;
|
---|
[aa389fd] | 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 | }
|
---|
[6028ec8] | 72 | }
|
---|
| 73 |
|
---|
[a6add7a] | 74 | /** Get log level name prefix.
|
---|
| 75 | *
|
---|
| 76 | * @param level Log level.
|
---|
| 77 | * @return String prefix for the message.
|
---|
| 78 | */
|
---|
[6028ec8] | 79 | static 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 | */
|
---|
| 100 | void usb_log_printf(usb_log_level_t level, const char *format, ...)
|
---|
| 101 | {
|
---|
[3d31af5] | 102 | FILE *screen_stream = NULL;
|
---|
[6028ec8] | 103 | switch (level) {
|
---|
| 104 | case USB_LOG_LEVEL_FATAL:
|
---|
| 105 | case USB_LOG_LEVEL_ERROR:
|
---|
[3d31af5] | 106 | screen_stream = stderr;
|
---|
[6028ec8] | 107 | break;
|
---|
| 108 | default:
|
---|
[3d31af5] | 109 | screen_stream = stdout;
|
---|
[6028ec8] | 110 | break;
|
---|
| 111 | }
|
---|
[3d31af5] | 112 | assert(screen_stream != NULL);
|
---|
[6028ec8] | 113 |
|
---|
| 114 | va_list args;
|
---|
| 115 |
|
---|
[aa389fd] | 116 | /*
|
---|
| 117 | * Serialize access to log files.
|
---|
| 118 | * Always print to log file, to screen print only when the enabled
|
---|
| 119 | * log level is high enough.
|
---|
| 120 | */
|
---|
[6028ec8] | 121 | fibril_mutex_lock(&log_serializer);
|
---|
[aa389fd] | 122 |
|
---|
| 123 | const char *level_name = log_level_name(level);
|
---|
| 124 |
|
---|
| 125 | if (log_stream != NULL) {
|
---|
[ef7f043] | 126 | va_start(args, format);
|
---|
| 127 |
|
---|
[aa389fd] | 128 | fprintf(log_stream, "[%s]%s: ", log_prefix, level_name);
|
---|
| 129 | vfprintf(log_stream, format, args);
|
---|
[3d31af5] | 130 | fflush(log_stream);
|
---|
[ef7f043] | 131 |
|
---|
| 132 | va_end(args);
|
---|
[aa389fd] | 133 | }
|
---|
| 134 |
|
---|
| 135 | if (level <= log_level) {
|
---|
[ef7f043] | 136 | va_start(args, format);
|
---|
| 137 |
|
---|
[3d31af5] | 138 | fprintf(screen_stream, "[%s]%s: ", log_prefix, level_name);
|
---|
| 139 | vfprintf(screen_stream, format, args);
|
---|
| 140 | fflush(screen_stream);
|
---|
[ef7f043] | 141 |
|
---|
| 142 | va_end(args);
|
---|
[aa389fd] | 143 | }
|
---|
| 144 |
|
---|
[6028ec8] | 145 | fibril_mutex_unlock(&log_serializer);
|
---|
| 146 | }
|
---|
[bb95594] | 147 |
|
---|
[019cff6] | 148 |
|
---|
[ff4185e6] | 149 | #define REMAINDER_STR_FMT " (%zu)..."
|
---|
| 150 | /* string + terminator + number width (enough for 4GB)*/
|
---|
| 151 | #define REMAINDER_STR_LEN (5 + 1 + 10)
|
---|
[a6add7a] | 152 |
|
---|
| 153 | /** How many bytes to group together. */
|
---|
[ff4185e6] | 154 | #define BUFFER_DUMP_GROUP_SIZE 4
|
---|
[a6add7a] | 155 |
|
---|
| 156 | /** Size of the string for buffer dumps. */
|
---|
[ff4185e6] | 157 | #define BUFFER_DUMP_LEN 240 /* Ought to be enough for everybody ;-). */
|
---|
[a6add7a] | 158 |
|
---|
| 159 | /** Fibril local storage for the dumped buffer. */
|
---|
[019cff6] | 160 | static fibril_local char buffer_dump[BUFFER_DUMP_LEN];
|
---|
| 161 |
|
---|
| 162 | /** Dump buffer into string.
|
---|
| 163 | *
|
---|
| 164 | * The function dumps given buffer into hexadecimal format and stores it
|
---|
| 165 | * in a static fibril local string.
|
---|
| 166 | * That means that you do not have to deallocate the string (actually, you
|
---|
[d78a32f] | 167 | * can not do that) and you do not have to guard it against concurrent
|
---|
[019cff6] | 168 | * calls to it.
|
---|
| 169 | * The only limitation is that each call rewrites the buffer again.
|
---|
| 170 | * Thus, it is necessary to copy the buffer elsewhere (that includes printing
|
---|
| 171 | * to screen or writing to file).
|
---|
| 172 | * Since this function is expected to be used for debugging prints only,
|
---|
| 173 | * that is not a big limitation.
|
---|
| 174 | *
|
---|
| 175 | * @warning You cannot use this function twice in the same printf
|
---|
| 176 | * (see detailed explanation).
|
---|
| 177 | *
|
---|
| 178 | * @param buffer Buffer to be printed (can be NULL).
|
---|
| 179 | * @param size Size of the buffer in bytes (can be zero).
|
---|
| 180 | * @param dumped_size How many bytes to actually dump (zero means all).
|
---|
| 181 | * @return Dumped buffer as a static (but fibril local) string.
|
---|
| 182 | */
|
---|
[1efe89b] | 183 | const char *usb_debug_str_buffer(const uint8_t *buffer, size_t size,
|
---|
[019cff6] | 184 | size_t dumped_size)
|
---|
| 185 | {
|
---|
[ff4185e6] | 186 | /*
|
---|
| 187 | * Remove previous string (that might also reveal double usage of
|
---|
| 188 | * this function).
|
---|
| 189 | */
|
---|
| 190 | bzero(buffer_dump, BUFFER_DUMP_LEN);
|
---|
| 191 |
|
---|
[019cff6] | 192 | if (buffer == NULL) {
|
---|
| 193 | return "(null)";
|
---|
| 194 | }
|
---|
| 195 | if (size == 0) {
|
---|
| 196 | return "(empty)";
|
---|
| 197 | }
|
---|
| 198 | if ((dumped_size == 0) || (dumped_size > size)) {
|
---|
| 199 | dumped_size = size;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[ff4185e6] | 202 | /* How many bytes are available in the output buffer. */
|
---|
| 203 | size_t buffer_remaining_size = BUFFER_DUMP_LEN - 1 - REMAINDER_STR_LEN;
|
---|
[019cff6] | 204 | char *it = buffer_dump;
|
---|
| 205 |
|
---|
| 206 | size_t index = 0;
|
---|
| 207 |
|
---|
[ff4185e6] | 208 | while (index < size) {
|
---|
| 209 | /* Determine space before the number. */
|
---|
| 210 | const char *space_before;
|
---|
| 211 | if (index == 0) {
|
---|
| 212 | space_before = "";
|
---|
| 213 | } else if ((index % BUFFER_DUMP_GROUP_SIZE) == 0) {
|
---|
| 214 | space_before = " ";
|
---|
| 215 | } else {
|
---|
| 216 | space_before = " ";
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | /*
|
---|
| 220 | * Add the byte as a hexadecimal number plus the space.
|
---|
| 221 | * We do it into temporary buffer to ensure that always
|
---|
| 222 | * the whole byte is printed.
|
---|
| 223 | */
|
---|
| 224 | int val = buffer[index];
|
---|
| 225 | char current_byte[16];
|
---|
| 226 | int printed = snprintf(current_byte, 16,
|
---|
| 227 | "%s%02x", space_before, val);
|
---|
| 228 | if (printed < 0) {
|
---|
| 229 | break;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | if ((size_t) printed > buffer_remaining_size) {
|
---|
| 233 | break;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | /* We can safely add 1, because space for end 0 is reserved. */
|
---|
| 237 | str_append(it, buffer_remaining_size + 1, current_byte);
|
---|
| 238 |
|
---|
| 239 | buffer_remaining_size -= printed;
|
---|
| 240 | /* Point at the terminator 0. */
|
---|
| 241 | it += printed;
|
---|
[019cff6] | 242 | index++;
|
---|
[ff4185e6] | 243 |
|
---|
[019cff6] | 244 | if (index >= dumped_size) {
|
---|
| 245 | break;
|
---|
| 246 | }
|
---|
| 247 | }
|
---|
| 248 |
|
---|
[ff4185e6] | 249 | /* Add how many bytes were not printed. */
|
---|
| 250 | if (index < size) {
|
---|
| 251 | snprintf(it, REMAINDER_STR_LEN,
|
---|
| 252 | REMAINDER_STR_FMT, size - index);
|
---|
| 253 | }
|
---|
[019cff6] | 254 |
|
---|
| 255 | return buffer_dump;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 |
|
---|
[bb95594] | 259 | /**
|
---|
| 260 | * @}
|
---|
| 261 | */
|
---|