| 16 You should have received a copy of the GNU Library General Public |
16 You should have received a copy of the GNU Library General Public |
| 17 License along with this library; if not, write to the Free |
17 License along with this library; if not, write to the Free |
| 18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 */ |
19 */ |
| 20 |
20 |
| |
21 |
| 21 #include <glib/gstring.h> |
22 #include <glib/gstring.h> |
| 22 |
23 |
| 23 #include "mw_debug.h" |
24 #include "mw_debug.h" |
| 24 |
25 |
| 25 |
26 |
| 26 #define FRM "%02x" |
27 |
| 27 #define FRMT "%02x%02x " |
28 #define FRMT1 "%02x" |
| |
29 #define FRMT2 FRMT1 FRMT1 " " |
| |
30 #define FRMT4 FRMT2 FRMT2 |
| |
31 #define FRMT8 FRMT4 FRMT4 |
| |
32 #define FRMT16 FRMT8 FRMT8 |
| |
33 |
| 28 #define BUF(n) ((unsigned char) buf[n]) |
34 #define BUF(n) ((unsigned char) buf[n]) |
| 29 #define ADVANCE(b, n, c) {b += c; n -= c;} |
35 #define ADVANCE(b, n, c) {b += c; n -= c;} |
| 30 |
36 |
| 31 |
37 |
| 32 #ifdef DEBUG |
38 |
| 33 /** writes hex pairs of buf to str */ |
39 /** writes hex pairs of buf to str */ |
| 34 static void t_pretty_print(GString *str, const char *buf, gsize len) { |
40 static void pretty_print(GString *str, const char *buf, gsize len) { |
| 35 while(len) { |
41 while(len) { |
| 36 if(len >= 16) { |
42 if(len >= 16) { |
| 37 g_string_append_printf(str, |
43 /* write a complete line */ |
| 38 FRMT FRMT FRMT FRMT FRMT FRMT FRMT FRMT "\n", |
44 g_string_append_printf(str, FRMT16, |
| 39 BUF(0), BUF(1), BUF(2), BUF(3), |
45 BUF(0), BUF(1), BUF(2), BUF(3), |
| 40 BUF(4), BUF(5), BUF(6), BUF(7), |
46 BUF(4), BUF(5), BUF(6), BUF(7), |
| 41 BUF(8), BUF(9), BUF(10), BUF(11), |
47 BUF(8), BUF(9), BUF(10), BUF(11), |
| 42 BUF(12), BUF(13), BUF(14), BUF(15)); |
48 BUF(12), BUF(13), BUF(14), BUF(15)); |
| 43 ADVANCE(buf, len, 16); |
49 ADVANCE(buf, len, 16); |
| 44 |
50 |
| 45 } else if(len == 2) { |
51 } else { |
| 46 g_string_append_printf(str, FRMT "\n", BUF(0), BUF(1)); |
52 /* write an incomplete line */ |
| 47 ADVANCE(buf, len, 2); |
53 if(len >= 8) { |
| |
54 g_string_append_printf(str, FRMT8, |
| |
55 BUF(0), BUF(1), BUF(2), BUF(3), |
| |
56 BUF(4), BUF(5), BUF(6), BUF(7)); |
| |
57 ADVANCE(buf, len, 8); |
| |
58 } |
| 48 |
59 |
| 49 } else if(len > 1) { |
60 if(len >= 4) { |
| 50 g_string_append_printf(str, FRMT, BUF(0), BUF(1)); |
61 g_string_append_printf(str, FRMT4, |
| 51 ADVANCE(buf, len, 2); |
62 BUF(0), BUF(1), BUF(2), BUF(3)); |
| |
63 ADVANCE(buf, len, 4); |
| |
64 } |
| 52 |
65 |
| 53 } else { |
66 if(len >= 2) { |
| 54 g_string_append_printf(str, FRM "\n", BUF(0)); |
67 g_string_append_printf(str, FRMT2, BUF(0), BUF(1)); |
| 55 ADVANCE(buf, len, 1); |
68 ADVANCE(buf, len, 2); |
| |
69 } |
| |
70 |
| |
71 if(len >= 1) { |
| |
72 g_string_append_printf(str, FRMT1, BUF(0)); |
| |
73 ADVANCE(buf, len, 1); |
| |
74 } |
| 56 } |
75 } |
| |
76 |
| |
77 /* append \n to each line but the last */ |
| |
78 if(len) g_string_append(str, "\n"); |
| 57 } |
79 } |
| 58 } |
|
| 59 #endif |
|
| 60 |
|
| 61 |
|
| 62 void pretty_print(const char *buf, gsize len) { |
|
| 63 #ifdef DEBUG |
|
| 64 GString *str; |
|
| 65 |
|
| 66 if(! len) return; |
|
| 67 |
|
| 68 g_return_if_fail(buf != NULL); |
|
| 69 |
|
| 70 str = g_string_new(NULL); |
|
| 71 t_pretty_print(str, buf, len); |
|
| 72 g_debug(str->str); |
|
| 73 g_string_free(str, TRUE); |
|
| 74 #endif |
|
| 75 ; |
|
| 76 } |
80 } |
| 77 |
81 |
| 78 |
82 |
| 79 void pretty_print_opaque(struct mwOpaque *o) { |
83 |
| 80 if(! o) return; |
84 void mw_debug_datav(const char *buf, gsize len, |
| 81 pretty_print(o->data, o->len); |
85 const char *msg, va_list args) { |
| |
86 GString *str; |
| |
87 |
| |
88 g_return_if_fail(buf != NULL || len == 0); |
| |
89 |
| |
90 str = g_string_new(NULL); |
| |
91 |
| |
92 if(msg) { |
| |
93 char *txt = g_strdup_vprintf(msg, args); |
| |
94 g_string_append_printf(str, "%s\n", txt); |
| |
95 g_free(txt); |
| |
96 } |
| |
97 pretty_print(str, buf, len); |
| |
98 |
| |
99 g_debug(str->str); |
| |
100 g_string_free(str, TRUE); |
| 82 } |
101 } |
| 83 |
102 |
| 84 |
103 |
| 85 void mw_debug_mailme_v(struct mwOpaque *block, |
|
| 86 const char *info, va_list args) { |
|
| 87 /* |
|
| 88 MW_MAILME_MESSAGE |
|
| 89 begin here |
|
| 90 info % args |
|
| 91 pretty_print |
|
| 92 end here |
|
| 93 */ |
|
| 94 |
104 |
| 95 #ifdef DEBUG |
105 void mw_debug_data(const char *buf, gsize len, |
| |
106 const char *msg, ...) { |
| |
107 va_list args; |
| |
108 |
| |
109 g_return_if_fail(buf != NULL || len == 0); |
| |
110 |
| |
111 va_start(args, msg); |
| |
112 mw_debug_datav(buf, len, msg, args); |
| |
113 va_end(args); |
| |
114 } |
| |
115 |
| |
116 |
| |
117 |
| |
118 void mw_debug_opaquev(struct mwOpaque *o, const char *txt, va_list args) { |
| |
119 g_return_if_fail(o != NULL); |
| |
120 mw_debug_datav(o->data, o->len, txt, args); |
| |
121 } |
| |
122 |
| |
123 |
| |
124 |
| |
125 void mw_debug_opaque(struct mwOpaque *o, const char *txt, ...) { |
| |
126 va_list args; |
| |
127 |
| |
128 g_return_if_fail(o != NULL); |
| |
129 |
| |
130 va_start(args, txt); |
| |
131 mw_debug_opaquev(o, txt, args); |
| |
132 va_end(args); |
| |
133 } |
| |
134 |
| |
135 |
| |
136 void mw_mailme_datav(const char *buf, gsize len, |
| |
137 const char *info, va_list args) { |
| |
138 |
| |
139 #if MW_MAILME |
| 96 GString *str; |
140 GString *str; |
| 97 char *txt; |
141 char *txt; |
| 98 |
142 |
| 99 str = g_string_new(MW_MAILME_MESSAGE "\n" |
143 str = g_string_new(MW_MAILME_MESSAGE "\n" |
| 100 " Please send mail to: " MW_MAILME_ADDRESS "\n" |
144 " Please send mail to: " MW_MAILME_ADDRESS "\n" |
| 101 MW_MAILME_CUT_START "\n"); |
145 MW_MAILME_CUT_START "\n"); |
| |
146 str = g_string_new(NULL); |
| 102 |
147 |
| 103 txt = g_strdup_vprintf(info, args); |
148 txt = g_strdup_vprintf(info, args); |
| 104 g_string_append(str, txt); |
149 g_string_append_printf(str, "%s\n", txt); |
| 105 g_free(txt); |
150 g_free(txt); |
| 106 |
151 |
| 107 g_string_append(str, "\n"); |
152 if(buf && len) pretty_print(str, buf, len); |
| 108 |
|
| 109 if(block) { |
|
| 110 t_pretty_print(str, block->data, block->len); |
|
| 111 } |
|
| 112 |
153 |
| 113 g_string_append(str, MW_MAILME_CUT_STOP); |
154 g_string_append(str, MW_MAILME_CUT_STOP); |
| 114 |
155 |
| 115 g_debug(str->str); |
156 g_debug(str->str); |
| 116 g_string_free(str, TRUE); |
157 g_string_free(str, TRUE); |
| |
158 |
| |
159 #else |
| |
160 mw_debug_datav(buf, len, info, args); |
| |
161 |
| 117 #endif |
162 #endif |
| 118 ; |
|
| 119 } |
163 } |
| 120 |
164 |
| 121 |
165 |
| 122 void mw_debug_mailme(struct mwOpaque *block, |
166 |
| 123 const char *info, ...) { |
167 void mw_mailme_data(const char *buf, gsize len, |
| |
168 const char *info, ...) { |
| 124 va_list args; |
169 va_list args; |
| 125 va_start(args, info); |
170 va_start(args, info); |
| 126 mw_debug_mailme_v(block, info, args); |
171 mw_mailme_datav(buf, len, info, args); |
| 127 va_end(args); |
172 va_end(args); |
| 128 } |
173 } |
| 129 |
174 |
| |
175 |
| |
176 |
| |
177 void mw_mailme_opaquev(struct mwOpaque *o, const char *info, va_list args) { |
| |
178 mw_mailme_datav(o->data, o->len, info, args); |
| |
179 } |
| |
180 |
| |
181 |
| |
182 |
| |
183 void mw_mailme_opaque(struct mwOpaque *o, const char *info, ...) { |
| |
184 va_list args; |
| |
185 va_start(args, info); |
| |
186 mw_mailme_opaquev(o, info, args); |
| |
187 va_end(args); |
| |
188 } |