wimax-tools  1.4.4
debug.h
Go to the documentation of this file.
1 /*
2  * Linux WiMax
3  * User Space API Debug Support
4  *
5  *
6  * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
7  * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  * * Neither the name of Intel Corporation nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  *
36  * Simple debug printing macros
37  *
38  * FIXME: doc
39  * Invoke like:
40  *
41  * #define D_LOCAL 4
42  * #include "debug.h"
43  *
44  * At the end of your include files.
45  */
46 #include <wimaxll.h>
47 
48 /* Master debug switch; !0 enables, 0 disables */
49 #define D_MASTER (!0)
50 
51 /* Local (per-file) debug switch; #define before #including */
52 #ifndef D_LOCAL
53 #define D_LOCAL 0
54 #endif
55 
56 #undef __d_printf
57 #undef d_fnstart
58 #undef d_fnend
59 #undef d_printf
60 #undef d_dump
61 
62 static inline
63 void __d_dev_head(char *head, size_t size, const struct wimaxll_handle *_dev)
64 {
65  if (_dev == NULL)
66  snprintf(head, size, "libwimax: ");
67  else if ((unsigned long)_dev < 4096) {
68  fprintf(stderr, "libwimax: E: Corrupt "
69  "device handle %p\n", _dev);
70  snprintf(head, size, "libwimax[dev_n/a]: ");
71  } else
72  snprintf(head, size,
73  "libwimax[%s]: ", _dev->name);
74 }
75 
76 
77 #define __d_printf(l, _tag, _dev, f, a...) \
78 do { \
79  const struct wimaxll_handle *__dev = (_dev); \
80  if (D_MASTER && D_LOCAL >= (l)) { \
81  char __head[64] = ""; \
82  __d_dev_head(__head, sizeof(__head), __dev); \
83  fprintf(stderr, "%s%s" _tag ": " f, __head, \
84  __func__, ## a); \
85  } \
86 } while (0 && _dev)
87 
88 #define d_fnstart(l, _dev, f, a...) __d_printf(l, " FNSTART", _dev, f, ## a)
89 #define d_fnend(l, _dev, f, a...) __d_printf(l, " FNEND", _dev, f, ## a)
90 #define d_printf(l, _dev, f, a...) __d_printf(l, "", _dev, f, ## a)
91 #define d_test(l) (D_MASTER && D_LOCAL >= (l))
92 
93 static inline
94 void __d_dump(const struct wimaxll_handle *dev,
95  const void *_ptr, size_t size)
96 {
97  const unsigned char *ptr = _ptr;
98  char str[64];
99  size_t cnt, itr;
100  for (itr = cnt = 0; cnt < size; cnt++) {
101  itr += snprintf(str + itr, sizeof(str) - itr,
102  "%02x ", ptr[cnt]);
103  if ((cnt > 0 && (cnt + 1) % 8 == 0) || (cnt == size - 1)) {
104  __d_printf(D_LOCAL, "", dev, "%s\n", str);
105  itr = 0;
106  }
107  }
108 }
109 
110 #define d_dump(l, dev, ptr, size) \
111 do { \
112  if (d_test(l)) \
113  __d_dump(dev, ptr, size); \
114 } while (0)
A WiMax control pipe handle.
Definition: internal.h:219
char name[__WIMAXLL_IFNAME_LEN]
Definition: internal.h:222
#define __d_printf(l, _tag, _dev, f, a...)
Definition: debug.h:77