File: mw_debug.c

package info (click to toggle)
meanwhile 1.0.2-5
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,732 kB
  • ctags: 3,503
  • sloc: ansic: 13,856; sh: 8,360; makefile: 145
file content (184 lines) | stat: -rw-r--r-- 4,179 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

/*
  Meanwhile - Unofficial Lotus Sametime Community Client Library
  Copyright (C) 2004  Christopher (siege) O'Brien
  
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Library General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Library General Public License for more details.
  
  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/


#include <glib.h>

#include "mw_debug.h"



#define FRMT1            "%02x"
#define FRMT2            FRMT1 FRMT1 " "
#define FRMT4            FRMT2 FRMT2
#define FRMT8            FRMT4 FRMT4
#define FRMT16           FRMT8 FRMT8

#define ADVANCE(b, n, c)  {b += c; n -= c;}



/** writes hex pairs of buf to str */
static void pretty_print(GString *str, const guchar *buf, gsize len) {
  while(len >= 16) {
    /* write a complete line */
    g_string_append_printf(str, FRMT16,
			   buf[0],  buf[1],  buf[2],  buf[3],
			   buf[4],  buf[5],  buf[6],  buf[7],
			   buf[8],  buf[9],  buf[10], buf[11],
			   buf[12], buf[13], buf[14], buf[15]);
    ADVANCE(buf, len, 16);
    
    /* append \n to each line but the last */
    if(len) g_string_append(str, "\n");
  }

  /* write an incomplete line */
  if(len >= 8) {
    g_string_append_printf(str, FRMT8,
			   buf[0], buf[1], buf[2], buf[3],
			   buf[4], buf[5], buf[6], buf[7]);
    ADVANCE(buf, len, 8);
  }
  
  if(len >= 4) {
    g_string_append_printf(str, FRMT4,
			   buf[0], buf[1], buf[2], buf[3]);
    ADVANCE(buf, len, 4);
  }

  if(len >= 2) {
    g_string_append_printf(str, FRMT2, buf[0], buf[1]);
    ADVANCE(buf, len, 2);
  }

  if(len >= 1) {
    g_string_append_printf(str, FRMT1, buf[0]);
    ADVANCE(buf, len, 1);
  }
}



void mw_debug_datav(const guchar *buf, gsize len,
		    const char *msg, va_list args) {
  GString *str;

  g_return_if_fail(buf != NULL || len == 0);

  str = g_string_new(NULL);

  if(msg) {
    char *txt = g_strdup_vprintf(msg, args);
    g_string_append_printf(str, "%s\n", txt);
    g_free(txt);
  }
  pretty_print(str, buf, len);

  g_debug(str->str);
  g_string_free(str, TRUE);
}



void mw_debug_data(const guchar *buf, gsize len,
		   const char *msg, ...) {
  va_list args;
  
  g_return_if_fail(buf != NULL || len == 0);

  va_start(args, msg);
  mw_debug_datav(buf, len, msg, args);
  va_end(args);
}



void mw_debug_opaquev(struct mwOpaque *o, const char *txt, va_list args) {
  g_return_if_fail(o != NULL);
  mw_debug_datav(o->data, o->len, txt, args);
}



void mw_debug_opaque(struct mwOpaque *o, const char *txt, ...) {
  va_list args;

  g_return_if_fail(o != NULL);

  va_start(args, txt);
  mw_debug_opaquev(o, txt, args);
  va_end(args);
}


void mw_mailme_datav(const guchar *buf, gsize len,
		     const char *info, va_list args) {

#if MW_MAILME
  GString *str;
  char *txt;

  str = g_string_new(MW_MAILME_MESSAGE "\n"
		     "  Please send mail to: " MW_MAILME_ADDRESS "\n"
		     MW_MAILME_CUT_START "\n");
  str = g_string_new(NULL);

  txt = g_strdup_vprintf(info, args);
  g_string_append_printf(str, "%s\n", txt);
  g_free(txt);

  if(buf && len) pretty_print(str, buf, len);

  g_string_append(str, MW_MAILME_CUT_STOP);

  g_debug(str->str);
  g_string_free(str, TRUE);

#else
  mw_debug_datav(buf, len, info, args);

#endif
}



void mw_mailme_data(const guchar *buf, gsize len,
		    const char *info, ...) {
  va_list args;
  va_start(args, info);
  mw_mailme_datav(buf, len, info, args);
  va_end(args);
}



void mw_mailme_opaquev(struct mwOpaque *o, const char *info, va_list args) {
  mw_mailme_datav(o->data, o->len, info, args);
}



void mw_mailme_opaque(struct mwOpaque *o, const char *info, ...) {
  va_list args;
  va_start(args, info);
  mw_mailme_opaquev(o, info, args);
  va_end(args);
}