Fri, 20 Jan 2006 00:19:53 +0000
[gaim-migrate @ 15309]
Add .cvsignore actions to meanwhile so cvs shuts up about it.
| 12956 | 1 | |
| 2 | /* | |
| 3 | Meanwhile - Unofficial Lotus Sametime Community Client Library | |
| 4 | Copyright (C) 2004 Christopher (siege) O'Brien | |
| 5 | ||
| 6 | This library is free software; you can redistribute it and/or | |
| 7 | modify it under the terms of the GNU Library General Public | |
| 8 | License as published by the Free Software Foundation; either | |
| 9 | version 2 of the License, or (at your option) any later version. | |
| 10 | ||
| 11 | This library is distributed in the hope that it will be useful, | |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 14 | Library General Public License for more details. | |
| 15 | ||
| 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 | |
| 18 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 19 | */ | |
| 20 | ||
| 21 | ||
| 22 | #include <glib/gstring.h> | |
| 23 | ||
| 24 | #include "mw_debug.h" | |
| 25 | ||
| 26 | ||
| 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 | ||
| 34 | #define ADVANCE(b, n, c) {b += c; n -= c;} | |
| 35 | ||
| 36 | ||
| 37 | ||
| 38 | /** writes hex pairs of buf to str */ | |
| 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); | |
| 75 | } | |
| 76 | } | |
| 77 | ||
| 78 | ||
| 79 | ||
| 80 | void mw_debug_datav(const guchar *buf, gsize len, | |
| 81 | const char *msg, va_list args) { | |
| 82 | GString *str; | |
| 83 | ||
| 84 | g_return_if_fail(buf != NULL || len == 0); | |
| 85 | ||
| 86 | str = g_string_new(NULL); | |
| 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 | ||
| 95 | g_debug("%s", str->str); | |
| 96 | g_string_free(str, TRUE); | |
| 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 defined(MW_MAILME) && MW_MAILME | |
| 136 | GString *str; | |
| 137 | char *txt; | |
| 138 | ||
| 139 | str = g_string_new(MW_MAILME_MESSAGE "\n" | |
| 140 | " Please send mail to: " MW_MAILME_ADDRESS "\n" | |
| 141 | MW_MAILME_CUT_START "\n"); | |
| 142 | str = g_string_new(NULL); | |
| 143 | ||
| 144 | txt = g_strdup_vprintf(info, args); | |
| 145 | g_string_append_printf(str, "%s\n", txt); | |
| 146 | g_free(txt); | |
| 147 | ||
| 148 | if(buf && len) pretty_print(str, buf, len); | |
| 149 | ||
| 150 | g_string_append(str, MW_MAILME_CUT_STOP); | |
| 151 | ||
| 152 | g_debug(str->str); | |
| 153 | g_string_free(str, TRUE); | |
| 154 | ||
| 155 | #else | |
| 156 | mw_debug_datav(buf, len, info, args); | |
| 157 | ||
| 158 | #endif | |
| 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 | } |