diff options
Diffstat (limited to 'src/debug.c')
| -rw-r--r-- | src/debug.c | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/src/debug.c b/src/debug.c new file mode 100644 index 0000000..b5f74ad --- /dev/null +++ b/src/debug.c | |||
| @@ -0,0 +1,131 @@ | |||
| 1 | /* | ||
| 2 | * debug.c | ||
| 3 | * contains utilitary methos for logging and debugging | ||
| 4 | * | ||
| 5 | * Copyright (c) 2008 Jonathan Beck All Rights Reserved. | ||
| 6 | * | ||
| 7 | * This library is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * This library is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with this library; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | #include <stdarg.h> | ||
| 22 | #include <stdio.h> | ||
| 23 | #include <stdint.h> | ||
| 24 | |||
| 25 | #include "debug.h" | ||
| 26 | #include "libiphone/libiphone.h" | ||
| 27 | |||
| 28 | int toto_debug = 0; | ||
| 29 | uint16_t dbg_mask = 0; | ||
| 30 | |||
| 31 | /** | ||
| 32 | * Sets the level of debugging. Currently the only acceptable values are 0 and | ||
| 33 | * 1. | ||
| 34 | * | ||
| 35 | * @param level Set to 0 for no debugging or 1 for debugging. | ||
| 36 | */ | ||
| 37 | void iphone_set_debug_level(int level) | ||
| 38 | { | ||
| 39 | toto_debug = level; | ||
| 40 | } | ||
| 41 | |||
| 42 | |||
| 43 | /** | ||
| 44 | * Set debug ids to display. Values can be OR-ed | ||
| 45 | * | ||
| 46 | * @param level Set to 0 for no debugging or 1 for debugging. | ||
| 47 | */ | ||
| 48 | void iphone_set_debug_mask(uint16_t mask) | ||
| 49 | { | ||
| 50 | dbg_mask = mask; | ||
| 51 | } | ||
| 52 | |||
| 53 | void log_debug_msg(const char *format, ...) | ||
| 54 | { | ||
| 55 | #ifndef STRIP_DEBUG_CODE | ||
| 56 | |||
| 57 | va_list args; | ||
| 58 | /* run the real fprintf */ | ||
| 59 | va_start(args, format); | ||
| 60 | |||
| 61 | if (toto_debug) | ||
| 62 | vfprintf(stderr, format, args); | ||
| 63 | |||
| 64 | va_end(args); | ||
| 65 | |||
| 66 | #endif | ||
| 67 | } | ||
| 68 | |||
| 69 | void log_dbg_msg(uint16_t id, const char *format, ...) | ||
| 70 | { | ||
| 71 | #ifndef STRIP_DEBUG_CODE | ||
| 72 | if (id & dbg_mask) { | ||
| 73 | va_list args; | ||
| 74 | /* run the real fprintf */ | ||
| 75 | va_start(args, format); | ||
| 76 | |||
| 77 | vfprintf(stderr, format, args); | ||
| 78 | |||
| 79 | va_end(args); | ||
| 80 | } | ||
| 81 | #endif | ||
| 82 | } | ||
| 83 | |||
| 84 | inline void log_debug_buffer(const char *data, const int length) | ||
| 85 | { | ||
| 86 | #ifndef STRIP_DEBUG_CODE | ||
| 87 | int i; | ||
| 88 | int j; | ||
| 89 | unsigned char c; | ||
| 90 | |||
| 91 | if (toto_debug) { | ||
| 92 | for (i = 0; i < length; i += 16) { | ||
| 93 | fprintf(stderr, "%04x: ", i); | ||
| 94 | for (j = 0; j < 16; j++) { | ||
| 95 | if (i + j >= length) { | ||
| 96 | fprintf(stderr, " "); | ||
| 97 | continue; | ||
| 98 | } | ||
| 99 | fprintf(stderr, "%02hhx ", *(data + i + j)); | ||
| 100 | } | ||
| 101 | fprintf(stderr, " | "); | ||
| 102 | for (j = 0; j < 16; j++) { | ||
| 103 | if (i + j >= length) | ||
| 104 | break; | ||
| 105 | c = *(data + i + j); | ||
| 106 | if ((c < 32) || (c > 127)) { | ||
| 107 | fprintf(stderr, "."); | ||
| 108 | continue; | ||
| 109 | } | ||
| 110 | fprintf(stderr, "%c", c); | ||
| 111 | } | ||
| 112 | fprintf(stderr, "\n"); | ||
| 113 | } | ||
| 114 | fprintf(stderr, "\n"); | ||
| 115 | } | ||
| 116 | #endif | ||
| 117 | } | ||
| 118 | |||
| 119 | inline void dump_debug_buffer(const char *file, const char *data, const int length) | ||
| 120 | { | ||
| 121 | #ifndef STRIP_DEBUG_CODE | ||
| 122 | /* run the real fprintf */ | ||
| 123 | if (toto_debug) { | ||
| 124 | FILE *my_ssl_packet = fopen(file, "w+"); | ||
| 125 | fwrite(data, 1, length, my_ssl_packet); | ||
| 126 | fflush(my_ssl_packet); | ||
| 127 | fprintf(stderr, "%s: Wrote SSL packet to drive, too.\n", __func__); | ||
| 128 | fclose(my_ssl_packet); | ||
| 129 | } | ||
| 130 | #endif | ||
| 131 | } | ||
