stupid fix for non-working printk-extension
[acx-mac80211.git] / log.c
blobb38d39a44cd625a4bd9bb2150e7bfc4fb759fa31
1 /*
2 * log.c: logging framework.
4 * This file should disappear some day, when the driver is known to work
5 * reliably. Until then, this will contain all logging routines used everywhere
6 * in the code.
8 * Copyright (c) 2008, Francis Galiegue <fgaliegue@gmail.com> for the ACX100
9 * driver project.
11 * This file is licensed under the GPL version 2.
13 #include <linux/jiffies.h>
14 #include <linux/module.h> /* Needed for MODULE_* */
16 #include "acx_config.h"
17 #include "acx_log.h"
20 * Forward declarations
23 static void acx_dump_bytes(const char *prefix, const void *data,
24 ssize_t len);
27 * HACK for debugging: if ACX_LOG_LEVEL is set to 2, set all printk_prefixes to
28 * KERN_EMERG.
30 * After all, we want ACX_LOG_LEVEL to 2 for heavy debugging, so...
32 static const char *const printk_levels[MAX_LOG_LEVEL + 1] = {
33 #if ACX_LOG_LEVEL < 2
34 KERN_WARNING,
35 KERN_INFO,
36 KERN_DEBUG
37 #else
38 KERN_EMERG,
39 KERN_EMERG,
40 KERN_EMERG
41 #endif
45 * "debug" module parameter, only if ACX_DEBUG is set.
47 * If not set, statically define it to ACX_DEFAULT_MSG.
49 #if ACX_DEBUG
50 unsigned int acx_debug = ACX_DEFAULT_MSG;
51 /* parameter is 'debug', corresponding var is acx_debug */
52 module_param_named(debug, acx_debug, uint, 0);
53 MODULE_PARM_DESC(debug, "Debug level mask (see L_xxx constants)");
54 #else
55 static unsigned int acx_debug = ACX_DEFAULT_MSG;
56 #endif
59 /**
60 * acx_log: the logging function
61 * @level: what level to log (LOG_WARNING, LOG_INFO or LOG_DEBUG).
62 * @what: what channel to log (any of the L_* values defined in acx_log.h).
63 * @fmt: the format string, and its arguments if any.
67 void acx_log(int level, int what, const char *fmt, ...)
69 va_list args;
70 const char *printk_level;
71 char fmt_end;
73 if (level > ACX_LOG_LEVEL)
74 return;
75 if (!(what & acx_debug))
76 return;
79 * FIXME: this shouldn't be necessary, but I don't rely on luck
81 if (level > MAX_LOG_LEVEL)
82 level = MAX_LOG_LEVEL;
84 fmt_end = fmt[strlen(fmt) - 1];
86 printk_level = printk_levels[level];
87 va_start(args, fmt);
89 printk("%sacx: ", printk_level);
90 vprintk(fmt, args);
91 va_end(args);
93 if (fmt_end != '\n') {
94 printk("\n");
95 printk(KERN_WARNING "MISSING NEWLINE, please fix\n");
97 return;
101 * acx_log_dump(): logs a message, and dumps a buffer. Basically, this is
102 * acx_log(), and a call to acx_dump_bytes() below.
103 * @level: see acx_log().
104 * @what: see acx_log().
105 * @buf: the buffer to dump.
106 * @buflen: the length of the buffer to dump.
109 void acx_log_dump(int level, int what, const void *buf, ssize_t buflen,
110 const char *fmt, ...)
112 va_list args;
113 const char *printk_level;
115 if (level > ACX_LOG_LEVEL)
116 return;
117 if (!(what & acx_debug))
118 return;
121 * FIXME: this shouldn't be necessary, but I don't rely on luck
123 if (level > MAX_LOG_LEVEL)
124 level = MAX_LOG_LEVEL;
126 printk_level = printk_levels[level];
128 va_start(args, fmt);
129 acx_log(level, what, fmt, args);
130 va_end(args);
132 acx_dump_bytes(printk_level, buf, buflen);
136 * acx_dump_bytes: hex dump of a buffer
137 * @printk_prefix: the KERN_* char constant, passed to this function by
138 * acx_log().
139 * @buf: the buffer to dump.
140 * @buflen: the length of the buffer.
142 * This function is static: it's not supposed to be called from anywhere else
143 * than this file. There is no "acx:" prefix here.
145 static void acx_dump_bytes(const char *printk_prefix, const void *data,
146 ssize_t len)
148 const u8 *ptr = (const u8 *)data;
149 unsigned int size = 0;
151 * buf holds:
152 * - the printk prefix (3 bytes);
153 * - the size printed as "0x%08X" (10 bytes);
154 * - the following semicolon (1 bytes);
155 * - 16 bytes printed as " %02X" (48 bytes);
156 * - the final '\0' (1 byte).
158 char buf[63], *p;
160 printk("%s--- BEGIN DUMP (%d bytes) ---\n", printk_prefix,
161 (int) len);
163 if (len <= 0)
164 return;
166 goto inside;
168 do {
169 p += sprintf(p, " %02X", *ptr);
170 size++, ptr++;
171 if (size % 16)
172 continue;
173 printk("%s\n", buf);
174 inside:
175 p = buf;
176 p += sprintf(p, "%s0x%08X:", printk_prefix, size);
177 } while (size < len);
179 if (size % 16)
180 printk("%s\n", buf);
182 printk("%s--- END DUMP ---\n", printk_prefix);
185 * Only in case of heavy debugging
188 #if ACX_LOG_LEVEL == 2
191 * __function_enter_exit: display entering/exiting of a function
192 * @fname: the function name.
193 * @enter_exit: 0 on enter, 1 on exit, 2 on exit with return value to be
194 * printed.
195 * @retcode: the return code to be printed if enter_exit is 2.
198 #define DEBUG_TSC 0
200 #if DEBUG_TSC
201 #define TIMESTAMP(d) unsigned long d; rdtscl(d)
202 #else
203 #define TIMESTAMP(d) unsigned long d = jiffies
204 #endif
207 * MAX_INDENT is the size of the spaces[] string below.
209 #define MAX_INDENT 10
210 void __function_enter_exit(const char *fname, int enter_exit,
211 int retcode)
213 static int indent = 0;
214 static const char spaces[] = " ";
215 const char *p = spaces + MAX_INDENT;
217 * Note that we MUST "declare" TIMESTAMP last: in case DEBUG_TSC is set,
218 * an rdtscl() is done on the argument, and, well, that's C.
220 TIMESTAMP(stamp);
221 stamp = stamp % 1000000;
223 switch (enter_exit) {
224 case __FUNCTION_ENTER:
225 if (indent < MAX_INDENT)
226 indent++;
227 break;
228 case __FUNCTION_EXIT:
229 case __FUNCTION_EXIT_WITHARG:
230 /* Nothing */
231 break;
232 default: /* Meh? */
233 return;
236 p -= indent;
238 switch (enter_exit) {
239 case __FUNCTION_ENTER:
240 acx_log(LOG_DEBUG, L_FUNC, "%08ld %s-> %s\n",
241 stamp, p, fname);
242 break;
243 case __FUNCTION_EXIT:
244 acx_log(LOG_DEBUG, L_FUNC, "%08ld %s<- %s\n",
245 stamp, p, fname);
246 break;
247 case __FUNCTION_EXIT_WITHARG:
248 acx_log(LOG_DEBUG, L_FUNC, "%08ld %s<- %s: %08X\n",
249 stamp, p, fname, retcode);
253 * The below test is enough: we already sanitized away illegal values of
254 * enter_exit at the beginning.
256 if (enter_exit != __FUNCTION_ENTER)
257 indent--;
261 #endif /* ACX_LOG_LEVEL == 2 */