wimax-tools 1.4.4

/home/users/builder/rpm/BUILD/wimax-tools-1.4.4/lib/debug.h

Go to the documentation of this file.
00001 /*
00002  * Linux WiMax
00003  * User Space API Debug Support
00004  *
00005  *
00006  * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
00007  * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted provided that the following conditions
00011  * are met:
00012  *
00013  *   * Redistributions of source code must retain the above copyright
00014  *     notice, this list of conditions and the following disclaimer.
00015  *   * Redistributions in binary form must reproduce the above copyright
00016  *     notice, this list of conditions and the following disclaimer in
00017  *     the documentation and/or other materials provided with the
00018  *     distribution.
00019  *   * Neither the name of Intel Corporation nor the names of its
00020  *     contributors may be used to endorse or promote products derived
00021  *     from this software without specific prior written permission.
00022  *
00023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00026  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00027  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00028  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00029  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00030  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00031  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  *
00036  * Simple debug printing macros
00037  *
00038  * FIXME: doc
00039  * Invoke like:
00040  *
00041  * #define D_LOCAL 4
00042  * #include "debug.h"
00043  *
00044  * At the end of your include files.
00045  */
00046 #include <wimaxll.h>
00047 
00048 /* Master debug switch; !0 enables, 0 disables */
00049 #define D_MASTER (!0)
00050 
00051 /* Local (per-file) debug switch; #define before #including */
00052 #ifndef D_LOCAL
00053 #define D_LOCAL 0
00054 #endif
00055 
00056 #undef __d_printf
00057 #undef d_fnstart
00058 #undef d_fnend
00059 #undef d_printf
00060 #undef d_dump
00061 
00062 static inline
00063 void __d_dev_head(char *head, size_t size, const struct wimaxll_handle *_dev)
00064 {
00065         if (_dev == NULL)
00066                 snprintf(head, size, "libwimax: ");
00067         else if ((unsigned long)_dev < 4096) {
00068                 fprintf(stderr, "libwimax: E: Corrupt "
00069                         "device handle %p\n", _dev);
00070                 snprintf(head, size, "libwimax[dev_n/a]: ");
00071         } else
00072                 snprintf(head, size,
00073                          "libwimax[%s]: ", _dev->name);
00074 }
00075 
00076 
00077 #define __d_printf(l, _tag, _dev, f, a...)                              \
00078 do {                                                                    \
00079         const struct wimaxll_handle *__dev = (_dev);                    \
00080         if (D_MASTER && D_LOCAL >= (l)) {                               \
00081                 char __head[64] = "";                                   \
00082                 __d_dev_head(__head, sizeof(__head), __dev);            \
00083                 fprintf(stderr, "%s%s" _tag ": " f, __head,             \
00084                         __func__, ## a);                                \
00085         }                                                               \
00086 } while (0 && _dev)
00087 
00088 #define d_fnstart(l, _dev, f, a...) __d_printf(l, " FNSTART", _dev, f, ## a)
00089 #define d_fnend(l, _dev, f, a...) __d_printf(l, " FNEND", _dev, f, ## a)
00090 #define d_printf(l, _dev, f, a...) __d_printf(l, "", _dev, f, ## a)
00091 #define d_test(l) (D_MASTER && D_LOCAL >= (l))
00092 
00093 static inline
00094 void __d_dump(const struct wimaxll_handle *dev,
00095               const void *_ptr, size_t size)
00096 {
00097         const unsigned char *ptr = _ptr;
00098         char str[64];
00099         size_t cnt, itr;
00100         for (itr = cnt = 0; cnt < size; cnt++) {
00101                 itr += snprintf(str + itr, sizeof(str) - itr,
00102                                 "%02x ", ptr[cnt]);
00103                 if ((cnt > 0 && (cnt + 1) % 8 == 0) || (cnt == size - 1)) {
00104                         __d_printf(D_LOCAL, "", dev, "%s\n", str);
00105                         itr = 0;
00106                 }
00107         }
00108 }
00109 
00110 #define d_dump(l, dev, ptr, size)               \
00111 do {                                            \
00112         if (d_test(l))                          \
00113                 __d_dump(dev, ptr, size);       \
00114 } while (0)