diff options
Diffstat (limited to 'src/debug.c')
| -rw-r--r-- | src/debug.c | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/src/debug.c b/src/debug.c new file mode 100644 index 0000000..2cdeebf --- /dev/null +++ b/src/debug.c | |||
| @@ -0,0 +1,151 @@ | |||
| 1 | /* | ||
| 2 | * debug.c | ||
| 3 | * contains utilitary functions for 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 | |||
| 22 | #include <stdarg.h> | ||
| 23 | #define _GNU_SOURCE 1 | ||
| 24 | #define __USE_GNU 1 | ||
| 25 | #include <stdio.h> | ||
| 26 | #include <stdint.h> | ||
| 27 | #include <stdlib.h> | ||
| 28 | |||
| 29 | #include "debug.h" | ||
| 30 | #include "libiphone/libiphone.h" | ||
| 31 | |||
| 32 | int debug_level = 0; | ||
| 33 | |||
| 34 | /** | ||
| 35 | * Sets the level of debugging. Currently the only acceptable values are 0 and | ||
| 36 | * 1. | ||
| 37 | * | ||
| 38 | * @param level Set to 0 for no debugging or 1 for debugging. | ||
| 39 | */ | ||
| 40 | void iphone_set_debug_level(int level) | ||
| 41 | { | ||
| 42 | debug_level = level; | ||
| 43 | } | ||
| 44 | |||
| 45 | static void debug_print_line(const char *func, const char *file, int line, const char *buffer) | ||
| 46 | { | ||
| 47 | char *str_time = NULL; | ||
| 48 | char *header = NULL; | ||
| 49 | time_t the_time; | ||
| 50 | |||
| 51 | time(&the_time); | ||
| 52 | str_time = g_new0 (gchar, 255); | ||
| 53 | strftime(str_time, 254, "%H:%M:%S", localtime (&the_time)); | ||
| 54 | |||
| 55 | /* generate header text */ | ||
| 56 | (void)asprintf(&header, "%s %s:%d %s()", str_time, file, line, func); | ||
| 57 | free (str_time); | ||
| 58 | |||
| 59 | /* always in light green */ | ||
| 60 | printf ("%s: ", header); | ||
| 61 | |||
| 62 | /* different colors according to the severity */ | ||
| 63 | printf ("%s\n", buffer); | ||
| 64 | |||
| 65 | /* flush this output, as we need to debug */ | ||
| 66 | fflush (stdout); | ||
| 67 | |||
| 68 | free (header); | ||
| 69 | } | ||
| 70 | |||
| 71 | inline void debug_info_real(const char *func, const char *file, int line, const char *format, ...) | ||
| 72 | { | ||
| 73 | #ifndef STRIP_DEBUG_CODE | ||
| 74 | va_list args; | ||
| 75 | char *buffer = NULL; | ||
| 76 | |||
| 77 | if (!debug_level) | ||
| 78 | return; | ||
| 79 | |||
| 80 | /* run the real fprintf */ | ||
| 81 | va_start(args, format); | ||
| 82 | (void)vasprintf(&buffer, format, args); | ||
| 83 | va_end(args); | ||
| 84 | |||
| 85 | debug_print_line(func, file, line, buffer); | ||
| 86 | |||
| 87 | free(buffer); | ||
| 88 | #endif | ||
| 89 | } | ||
| 90 | |||
| 91 | inline void debug_buffer(const char *data, const int length) | ||
| 92 | { | ||
| 93 | #ifndef STRIP_DEBUG_CODE | ||
| 94 | int i; | ||
| 95 | int j; | ||
| 96 | unsigned char c; | ||
| 97 | |||
| 98 | if (debug_level) { | ||
| 99 | for (i = 0; i < length; i += 16) { | ||
| 100 | fprintf(stderr, "%04x: ", i); | ||
| 101 | for (j = 0; j < 16; j++) { | ||
| 102 | if (i + j >= length) { | ||
| 103 | fprintf(stderr, " "); | ||
| 104 | continue; | ||
| 105 | } | ||
| 106 | fprintf(stderr, "%02hhx ", *(data + i + j)); | ||
| 107 | } | ||
| 108 | fprintf(stderr, " | "); | ||
| 109 | for (j = 0; j < 16; j++) { | ||
| 110 | if (i + j >= length) | ||
| 111 | break; | ||
| 112 | c = *(data + i + j); | ||
| 113 | if ((c < 32) || (c > 127)) { | ||
| 114 | fprintf(stderr, "."); | ||
| 115 | continue; | ||
| 116 | } | ||
| 117 | fprintf(stderr, "%c", c); | ||
| 118 | } | ||
| 119 | fprintf(stderr, "\n"); | ||
| 120 | } | ||
| 121 | fprintf(stderr, "\n"); | ||
| 122 | } | ||
| 123 | #endif | ||
| 124 | } | ||
| 125 | |||
| 126 | inline void debug_buffer_to_file(const char *file, const char *data, const int length) | ||
| 127 | { | ||
| 128 | #ifndef STRIP_DEBUG_CODE | ||
| 129 | if (debug_level) { | ||
| 130 | FILE *f = fopen(file, "w+"); | ||
| 131 | fwrite(data, 1, length, f); | ||
| 132 | fflush(f); | ||
| 133 | fclose(f); | ||
| 134 | } | ||
| 135 | #endif | ||
| 136 | } | ||
| 137 | |||
| 138 | inline void debug_plist(plist_t plist) | ||
| 139 | { | ||
| 140 | #ifndef STRIP_DEBUG_CODE | ||
| 141 | if (!plist) | ||
| 142 | return; | ||
| 143 | |||
| 144 | char *buffer = NULL; | ||
| 145 | uint32_t length = 0; | ||
| 146 | plist_to_xml(plist, &buffer, &length); | ||
| 147 | debug_info("plist size: %i\nbuffer :\n%s", length, buffer); | ||
| 148 | free(buffer); | ||
| 149 | #endif | ||
| 150 | } | ||
| 151 | |||
