~ubuntu-branches/ubuntu/intrepid/meanwhile/intrepid

« back to all changes in this revision

Viewing changes to src/mw_debug.c

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2006-01-13 12:38:18 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060113123818-zod7j3hd9z0iz5fy
Tags: 1.0.2-0ubuntu1
* New upstream release
* Rename libmeanwhile0 to libmeanwhile1

Show diffs side-by-side

added added

removed removed

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