diff options
| author | 2013-04-25 17:43:54 +0100 | |
|---|---|---|
| committer | 2013-04-25 17:43:54 +0100 | |
| commit | 4748e1ac72ede8ba1f7a28ec8c1b8d5bb0f0afd3 (patch) | |
| tree | 708d1ec3ee0fcc3a3f49be45457c5f641c108dc2 /src/debug.c | |
| parent | 927c34ee8e6974a34b1dfd7d1a4fde5eabbeca7a (diff) | |
| download | libimobiledevice-4748e1ac72ede8ba1f7a28ec8c1b8d5bb0f0afd3.tar.gz libimobiledevice-4748e1ac72ede8ba1f7a28ec8c1b8d5bb0f0afd3.tar.bz2 | |
common: Move debug and userpref code into libinternalcommon
Diffstat (limited to 'src/debug.c')
| -rw-r--r-- | src/debug.c | 169 |
1 files changed, 0 insertions, 169 deletions
diff --git a/src/debug.c b/src/debug.c deleted file mode 100644 index 0bb87a2..0000000 --- a/src/debug.c +++ /dev/null | |||
| @@ -1,169 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * debug.c | ||
| 3 | * contains utilitary functions for debugging | ||
| 4 | * | ||
| 5 | * Copyright (c) 2008 Jonathan Beck All Rights Reserved. | ||
| 6 | * Copyright (c) 2010 Martin S. All Rights Reserved. | ||
| 7 | * | ||
| 8 | * This library is free software; you can redistribute it and/or | ||
| 9 | * modify it under the terms of the GNU Lesser General Public | ||
| 10 | * License as published by the Free Software Foundation; either | ||
| 11 | * version 2.1 of the License, or (at your option) any later version. | ||
| 12 | * | ||
| 13 | * This library is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 16 | * Lesser General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU Lesser General Public | ||
| 19 | * License along with this library; if not, write to the Free Software | ||
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 21 | */ | ||
| 22 | |||
| 23 | #ifdef HAVE_CONFIG_H | ||
| 24 | #include <config.h> | ||
| 25 | #endif | ||
| 26 | #include <stdarg.h> | ||
| 27 | #define _GNU_SOURCE 1 | ||
| 28 | #define __USE_GNU 1 | ||
| 29 | #include <stdio.h> | ||
| 30 | #include <stdint.h> | ||
| 31 | #include <stdlib.h> | ||
| 32 | #include <time.h> | ||
| 33 | |||
| 34 | #include "debug.h" | ||
| 35 | #include "libimobiledevice/libimobiledevice.h" | ||
| 36 | |||
| 37 | #ifndef STRIP_DEBUG_CODE | ||
| 38 | #include "asprintf.h" | ||
| 39 | #endif | ||
| 40 | |||
| 41 | int debug_level = 0; | ||
| 42 | |||
| 43 | /** | ||
| 44 | * Sets the level of debugging. Currently the only acceptable values are 0 and | ||
| 45 | * 1. | ||
| 46 | * | ||
| 47 | * @param level Set to 0 for no debugging or 1 for debugging. | ||
| 48 | */ | ||
| 49 | void idevice_set_debug_level(int level) | ||
| 50 | { | ||
| 51 | debug_level = level; | ||
| 52 | } | ||
| 53 | |||
| 54 | #ifndef STRIP_DEBUG_CODE | ||
| 55 | static void debug_print_line(const char *func, const char *file, int line, const char *buffer) | ||
| 56 | { | ||
| 57 | char *str_time = NULL; | ||
| 58 | char *header = NULL; | ||
| 59 | time_t the_time; | ||
| 60 | |||
| 61 | time(&the_time); | ||
| 62 | str_time = (char*)malloc(255); | ||
| 63 | strftime(str_time, 254, "%H:%M:%S", localtime (&the_time)); | ||
| 64 | |||
| 65 | /* generate header text */ | ||
| 66 | (void)asprintf(&header, "%s %s:%d %s()", str_time, file, line, func); | ||
| 67 | free (str_time); | ||
| 68 | |||
| 69 | /* trim ending newlines */ | ||
| 70 | |||
| 71 | /* print header */ | ||
| 72 | printf ("%s: ", header); | ||
| 73 | |||
| 74 | /* print actual debug content */ | ||
| 75 | printf ("%s\n", buffer); | ||
| 76 | |||
| 77 | /* flush this output, as we need to debug */ | ||
| 78 | fflush (stdout); | ||
| 79 | |||
| 80 | free (header); | ||
| 81 | } | ||
| 82 | #endif | ||
| 83 | |||
| 84 | inline void debug_info_real(const char *func, const char *file, int line, const char *format, ...) | ||
| 85 | { | ||
| 86 | #ifndef STRIP_DEBUG_CODE | ||
| 87 | va_list args; | ||
| 88 | char *buffer = NULL; | ||
| 89 | |||
| 90 | if (!debug_level) | ||
| 91 | return; | ||
| 92 | |||
| 93 | /* run the real fprintf */ | ||
| 94 | va_start(args, format); | ||
| 95 | (void)vasprintf(&buffer, format, args); | ||
| 96 | va_end(args); | ||
| 97 | |||
| 98 | debug_print_line(func, file, line, buffer); | ||
| 99 | |||
| 100 | free(buffer); | ||
| 101 | #endif | ||
| 102 | } | ||
| 103 | |||
| 104 | inline void debug_buffer(const char *data, const int length) | ||
| 105 | { | ||
| 106 | #ifndef STRIP_DEBUG_CODE | ||
| 107 | int i; | ||
| 108 | int j; | ||
| 109 | unsigned char c; | ||
| 110 | |||
| 111 | if (debug_level) { | ||
| 112 | for (i = 0; i < length; i += 16) { | ||
| 113 | fprintf(stderr, "%04x: ", i); | ||
| 114 | for (j = 0; j < 16; j++) { | ||
| 115 | if (i + j >= length) { | ||
| 116 | fprintf(stderr, " "); | ||
| 117 | continue; | ||
| 118 | } | ||
| 119 | fprintf(stderr, "%02hhx ", *(data + i + j)); | ||
| 120 | } | ||
| 121 | fprintf(stderr, " | "); | ||
| 122 | for (j = 0; j < 16; j++) { | ||
| 123 | if (i + j >= length) | ||
| 124 | break; | ||
| 125 | c = *(data + i + j); | ||
| 126 | if ((c < 32) || (c > 127)) { | ||
| 127 | fprintf(stderr, "."); | ||
| 128 | continue; | ||
| 129 | } | ||
| 130 | fprintf(stderr, "%c", c); | ||
| 131 | } | ||
| 132 | fprintf(stderr, "\n"); | ||
| 133 | } | ||
| 134 | fprintf(stderr, "\n"); | ||
| 135 | } | ||
| 136 | #endif | ||
| 137 | } | ||
| 138 | |||
| 139 | inline void debug_buffer_to_file(const char *file, const char *data, const int length) | ||
| 140 | { | ||
| 141 | #ifndef STRIP_DEBUG_CODE | ||
| 142 | if (debug_level) { | ||
| 143 | FILE *f = fopen(file, "wb"); | ||
| 144 | fwrite(data, 1, length, f); | ||
| 145 | fflush(f); | ||
| 146 | fclose(f); | ||
| 147 | } | ||
| 148 | #endif | ||
| 149 | } | ||
| 150 | |||
| 151 | inline void debug_plist_real(const char *func, const char *file, int line, plist_t plist) | ||
| 152 | { | ||
| 153 | #ifndef STRIP_DEBUG_CODE | ||
| 154 | if (!plist) | ||
| 155 | return; | ||
| 156 | |||
| 157 | char *buffer = NULL; | ||
| 158 | uint32_t length = 0; | ||
| 159 | plist_to_xml(plist, &buffer, &length); | ||
| 160 | |||
| 161 | /* get rid of ending newline as one is already added in the debug line */ | ||
| 162 | if (buffer[length-1] == '\n') | ||
| 163 | buffer[length-1] = '\0'; | ||
| 164 | |||
| 165 | debug_info_real(func, file, line, "printing %i bytes plist:\n%s", length, buffer); | ||
| 166 | free(buffer); | ||
| 167 | #endif | ||
| 168 | } | ||
| 169 | |||
