carl9170 firmware: import 1.7.0
[carl9170fw.git] / tools / carlu / src / debug.c
1 /*
2  * carl9170user - userspace testing utility for ar9170 devices
3  *
4  * Random assortment of debug stuff
5  *
6  * Copyright 2009, 2010 Christian Lamparter <chunkeey@googlemail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <stdbool.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <ctype.h>
34
35 #include "debug.h"
36
37 bool print_message_debug_level;
38 enum debug_level_t debug_level;
39 FILE *_stdout;
40 FILE *_stddbg;
41 FILE *_stderr;
42
43 void init_debug()
44 {
45         debug_level = VERBOSE;
46         debug_level = INFO;
47         print_message_debug_level = false;
48
49         _stdout = stdout;
50         _stddbg = stdout;
51         _stderr = stderr;
52 }
53
54 FILE *dbg_lvl_to_fh(const enum debug_level_t lvl)
55 {
56         switch (lvl) {
57         case ERROR:
58         case WARNING:
59                 return _stderr;
60         case INFO:
61                 return _stdout;
62         case VERBOSE:
63                 return _stddbg;
64         default:
65                 BUG_ON(1);
66         }
67 }
68
69 void print_hex_dump_bytes(const enum debug_level_t lvl, const char *pre,
70                           const void *buf, size_t len)
71 {
72         char line[58];
73         char str[17] = { 0 };
74         const unsigned char *tmp = (void *) buf;
75         char *pbuf = line;
76         size_t i;
77
78         for (i = 0; i < len; i++) {
79                 if (i % 16 == 0) {
80                         if (pbuf != line) {
81                                 __fprintf(lvl, "%s%s: %s\n", pre, line, str);
82                                 pbuf = line;
83                         }
84
85                         pbuf += sprintf(pbuf, "0x%04lx: ", (unsigned long)i);
86                 }
87
88                 pbuf += sprintf(pbuf, "%.2x ", tmp[i]);
89                 str[i % 16] = (isprint(tmp[i]) && isascii(tmp[i])) ? tmp[i] : '.';
90         }
91         if (pbuf != line) {
92                 if ((i % 16)) {
93                         str[i % 16] = '\0';
94
95                         for (i = 0; i < (16 - (i % 16)); i++)
96                                 pbuf += sprintf(pbuf, "   ");
97                 }
98
99                 __fprintf(lvl, "%s%s: %s\n", pre, line, str);
100         }
101 }