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

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

USB logging uses standard logging

  • Property mode set to 100644
File size: 4.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 * 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/** Enable logging.
44 *
45 * @param level Maximal enabled level (including this one).
46 * @param message_prefix Prefix for each printed message.
47 */
48void usb_log_enable(log_level_t level, const char *message_prefix)
49{
50 log_init(message_prefix);
51}
52
53/** Print logging message.
54 *
55 * @param level Verbosity level of the message.
56 * @param format Formatting directive.
57 */
58void usb_log_printf(log_level_t level, const char *format, ...)
59{
60 va_list args;
61
62 va_start(args, format);
63 log_msgv(level, format, args);
64 va_end(args);
65}
66
67
68#define REMAINDER_STR_FMT " (%zu)..."
69/* string + terminator + number width (enough for 4GB)*/
70#define REMAINDER_STR_LEN (5 + 1 + 10)
71
72/** How many bytes to group together. */
73#define BUFFER_DUMP_GROUP_SIZE 4
74
75/** Size of the string for buffer dumps. */
76#define BUFFER_DUMP_LEN 240 /* Ought to be enough for everybody ;-). */
77
78/** Fibril local storage for the dumped buffer. */
79static fibril_local char buffer_dump[2][BUFFER_DUMP_LEN];
80/** Fibril local storage for buffer switching. */
81static fibril_local int buffer_dump_index = 0;
82
83/** Dump buffer into string.
84 *
85 * The function dumps given buffer into hexadecimal format and stores it
86 * in a static fibril local string.
87 * That means that you do not have to deallocate the string (actually, you
88 * can not do that) and you do not have to guard it against concurrent
89 * calls to it.
90 * The only limitation is that each second call rewrites the buffer again
91 * (internally, two buffer are used in cyclic manner).
92 * Thus, it is necessary to copy the buffer elsewhere (that includes printing
93 * to screen or writing to file).
94 * Since this function is expected to be used for debugging prints only,
95 * that is not a big limitation.
96 *
97 * @warning You cannot use this function more than twice in the same printf
98 * (see detailed explanation).
99 *
100 * @param buffer Buffer to be printed (can be NULL).
101 * @param size Size of the buffer in bytes (can be zero).
102 * @param dumped_size How many bytes to actually dump (zero means all).
103 * @return Dumped buffer as a static (but fibril local) string.
104 */
105const char *usb_debug_str_buffer(const uint8_t *buffer, size_t size,
106 size_t dumped_size)
107{
108 /*
109 * Remove previous string.
110 */
111 bzero(buffer_dump[buffer_dump_index], BUFFER_DUMP_LEN);
112
113 /* Do the actual dump. */
114 ddf_dump_buffer(buffer_dump[buffer_dump_index], BUFFER_DUMP_LEN,
115 buffer, 1, size, dumped_size);
116
117 /* Next time, use the other buffer. */
118 buffer_dump_index = 1 - buffer_dump_index;
119
120 /* Need to take the old one due to previous line. */
121 return buffer_dump[1 - buffer_dump_index];
122}
123
124
125/**
126 * @}
127 */
Note: See TracBrowser for help on using the repository browser.