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