summaryrefslogtreecommitdiffstats
path: root/common/debug.c
diff options
context:
space:
mode:
Diffstat (limited to 'common/debug.c')
-rw-r--r--common/debug.c167
1 files changed, 167 insertions, 0 deletions
diff --git a/common/debug.c b/common/debug.c
new file mode 100644
index 0000000..cf1bc2f
--- /dev/null
+++ b/common/debug.c
@@ -0,0 +1,167 @@
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 "src/idevice.h"
35#include "debug.h"
36#include "libimobiledevice/libimobiledevice.h"
37
38#ifndef STRIP_DEBUG_CODE
39#include "asprintf.h"
40#endif
41
42static int debug_level;
43
44void internal_set_debug_level(int level)
45{
46 debug_level = level;
47}
48
49#define MAX_PRINT_LEN (16*1024)
50
51#ifndef STRIP_DEBUG_CODE
52static void debug_print_line(const char *func, const char *file, int line, const char *buffer)
53{
54 char *str_time = NULL;
55 char *header = NULL;
56 time_t the_time;
57
58 time(&the_time);
59 str_time = (char*)malloc(255);
60 strftime(str_time, 254, "%H:%M:%S", localtime (&the_time));
61
62 /* generate header text */
63 if(asprintf(&header, "%s %s:%d %s()", str_time, file, line, func)<0){}
64 free (str_time);
65
66 /* trim ending newlines */
67
68 /* print header */
69 fprintf(stderr, "%s: ", header);
70
71 /* print actual debug content */
72 fprintf(stderr, "%s\n", buffer);
73
74 free (header);
75}
76#endif
77
78void debug_info_real(const char *func, const char *file, int line, const char *format, ...)
79{
80#ifndef STRIP_DEBUG_CODE
81 va_list args;
82 char *buffer = NULL;
83
84 if (!debug_level)
85 return;
86
87 /* run the real fprintf */
88 va_start(args, format);
89 if(vasprintf(&buffer, format, args)<0){}
90 va_end(args);
91
92 debug_print_line(func, file, line, buffer);
93
94 free(buffer);
95#endif
96}
97
98void debug_buffer(const char *data, const int length)
99{
100#ifndef STRIP_DEBUG_CODE
101 int i;
102 int j;
103 unsigned char c;
104
105 if (debug_level) {
106 for (i = 0; i < length; i += 16) {
107 fprintf(stderr, "%04x: ", i);
108 for (j = 0; j < 16; j++) {
109 if (i + j >= length) {
110 fprintf(stderr, " ");
111 continue;
112 }
113 fprintf(stderr, "%02x ", *(data + i + j) & 0xff);
114 }
115 fprintf(stderr, " | ");
116 for (j = 0; j < 16; j++) {
117 if (i + j >= length)
118 break;
119 c = *(data + i + j);
120 if ((c < 32) || (c > 127)) {
121 fprintf(stderr, ".");
122 continue;
123 }
124 fprintf(stderr, "%c", c);
125 }
126 fprintf(stderr, "\n");
127 }
128 fprintf(stderr, "\n");
129 }
130#endif
131}
132
133void debug_buffer_to_file(const char *file, const char *data, const int length)
134{
135#ifndef STRIP_DEBUG_CODE
136 if (debug_level) {
137 FILE *f = fopen(file, "wb");
138 fwrite(data, 1, length, f);
139 fflush(f);
140 fclose(f);
141 }
142#endif
143}
144
145void debug_plist_real(const char *func, const char *file, int line, plist_t plist)
146{
147#ifndef STRIP_DEBUG_CODE
148 if (!plist)
149 return;
150
151 char *buffer = NULL;
152 uint32_t length = 0;
153 plist_to_xml(plist, &buffer, &length);
154
155 /* get rid of ending newline as one is already added in the debug line */
156 if (buffer[length-1] == '\n')
157 buffer[length-1] = '\0';
158
159 if (length <= MAX_PRINT_LEN)
160 debug_info_real(func, file, line, "printing %i bytes plist:\n%s", length, buffer);
161 else
162 debug_info_real(func, file, line, "supress printing %i bytes plist...\n", length);
163
164 free(buffer);
165#endif
166}
167