diff options
| author | 2010-01-12 19:09:36 +0100 | |
|---|---|---|
| committer | 2010-01-12 19:09:36 +0100 | |
| commit | 342f4e929888c0aaa088e39fb98a74957bf45fa7 (patch) | |
| tree | 905bafb1b353b8ac21e3fb1f9c773d218a5c878e /src/debug.c | |
| parent | bf3dc421b2b5ccfe2fcd3cd4ec1ef90f39599600 (diff) | |
| download | libimobiledevice-342f4e929888c0aaa088e39fb98a74957bf45fa7.tar.gz libimobiledevice-342f4e929888c0aaa088e39fb98a74957bf45fa7.tar.bz2 | |
Refactor and unify internal debug system for ease of use and verbosity
This introduces a new debug_info macro which automatically prints
the calling function, file and line number information instead of
having that information passed to every old log_debug_msg call.
Diffstat (limited to 'src/debug.c')
| -rw-r--r-- | src/debug.c | 70 |
1 files changed, 51 insertions, 19 deletions
diff --git a/src/debug.c b/src/debug.c index 78f3a17..1f9aff1 100644 --- a/src/debug.c +++ b/src/debug.c | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | /* | 1 | /* |
| 2 | * debug.c | 2 | * debug.c |
| 3 | * contains utilitary methos for logging and debugging | 3 | * contains utilitary functions for debugging |
| 4 | * | 4 | * |
| 5 | * Copyright (c) 2008 Jonathan Beck All Rights Reserved. | 5 | * Copyright (c) 2008 Jonathan Beck All Rights Reserved. |
| 6 | * | 6 | * |
| @@ -18,14 +18,18 @@ | |||
| 18 | * License along with this library; if not, write to the Free Software | 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 | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ | 20 | */ |
| 21 | |||
| 21 | #include <stdarg.h> | 22 | #include <stdarg.h> |
| 23 | #define _GNU_SOURCE 1 | ||
| 24 | #define __USE_GNU 1 | ||
| 22 | #include <stdio.h> | 25 | #include <stdio.h> |
| 23 | #include <stdint.h> | 26 | #include <stdint.h> |
| 27 | #include <stdlib.h> | ||
| 24 | 28 | ||
| 25 | #include "debug.h" | 29 | #include "debug.h" |
| 26 | #include "libiphone/libiphone.h" | 30 | #include "libiphone/libiphone.h" |
| 27 | 31 | ||
| 28 | int toto_debug = 0; | 32 | int debug_level = 0; |
| 29 | 33 | ||
| 30 | /** | 34 | /** |
| 31 | * Sets the level of debugging. Currently the only acceptable values are 0 and | 35 | * Sets the level of debugging. Currently the only acceptable values are 0 and |
| @@ -35,33 +39,63 @@ int toto_debug = 0; | |||
| 35 | */ | 39 | */ |
| 36 | void iphone_set_debug_level(int level) | 40 | void iphone_set_debug_level(int level) |
| 37 | { | 41 | { |
| 38 | toto_debug = level; | 42 | debug_level = level; |
| 39 | } | 43 | } |
| 40 | 44 | ||
| 41 | void log_debug_msg(const char *format, ...) | 45 | static void debug_print_line(const char *func, const char *file, int line, const char *buffer) |
| 42 | { | 46 | { |
| 43 | #ifndef STRIP_DEBUG_CODE | 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 | asprintf(&header, "%s %s %s:%d", str_time, file, func, line); | ||
| 57 | free (str_time); | ||
| 58 | |||
| 59 | /* always in light green */ | ||
| 60 | printf ("%s\n", 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 | } | ||
| 44 | 70 | ||
| 71 | inline void debug_info_real(const char *func, const char *file, int line, const char *format, ...) | ||
| 72 | { | ||
| 73 | #ifndef STRIP_DEBUG_CODE | ||
| 45 | va_list args; | 74 | va_list args; |
| 46 | /* run the real fprintf */ | 75 | char *buffer = NULL; |
| 47 | va_start(args, format); | ||
| 48 | 76 | ||
| 49 | if (toto_debug) | 77 | if (!debug_level) |
| 50 | vfprintf(stderr, format, args); | 78 | return; |
| 51 | 79 | ||
| 80 | /* run the real fprintf */ | ||
| 81 | va_start(args, format); | ||
| 82 | vasprintf(&buffer, format, args); | ||
| 52 | va_end(args); | 83 | va_end(args); |
| 53 | 84 | ||
| 85 | debug_print_line(func, file, line, buffer); | ||
| 86 | |||
| 87 | free(buffer); | ||
| 54 | #endif | 88 | #endif |
| 55 | } | 89 | } |
| 56 | 90 | ||
| 57 | inline void log_debug_buffer(const char *data, const int length) | 91 | inline void debug_buffer(const char *data, const int length) |
| 58 | { | 92 | { |
| 59 | #ifndef STRIP_DEBUG_CODE | 93 | #ifndef STRIP_DEBUG_CODE |
| 60 | int i; | 94 | int i; |
| 61 | int j; | 95 | int j; |
| 62 | unsigned char c; | 96 | unsigned char c; |
| 63 | 97 | ||
| 64 | if (toto_debug) { | 98 | if (debug_level) { |
| 65 | for (i = 0; i < length; i += 16) { | 99 | for (i = 0; i < length; i += 16) { |
| 66 | fprintf(stderr, "%04x: ", i); | 100 | fprintf(stderr, "%04x: ", i); |
| 67 | for (j = 0; j < 16; j++) { | 101 | for (j = 0; j < 16; j++) { |
| @@ -89,16 +123,14 @@ inline void log_debug_buffer(const char *data, const int length) | |||
| 89 | #endif | 123 | #endif |
| 90 | } | 124 | } |
| 91 | 125 | ||
| 92 | inline void dump_debug_buffer(const char *file, const char *data, const int length) | 126 | inline void debug_buffer_to_file(const char *file, const char *data, const int length) |
| 93 | { | 127 | { |
| 94 | #ifndef STRIP_DEBUG_CODE | 128 | #ifndef STRIP_DEBUG_CODE |
| 95 | /* run the real fprintf */ | 129 | if (debug_level) { |
| 96 | if (toto_debug) { | 130 | FILE *f = fopen(file, "w+"); |
| 97 | FILE *my_ssl_packet = fopen(file, "w+"); | 131 | fwrite(data, 1, length, f); |
| 98 | fwrite(data, 1, length, my_ssl_packet); | 132 | fflush(f); |
| 99 | fflush(my_ssl_packet); | 133 | fclose(f); |
| 100 | fprintf(stderr, "%s: Wrote SSL packet to drive, too.\n", __func__); | ||
| 101 | fclose(my_ssl_packet); | ||
| 102 | } | 134 | } |
| 103 | #endif | 135 | #endif |
| 104 | } | 136 | } |
