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