aboutsummaryrefslogtreecommitdiff
path: root/libmpio/debug.c
blob: 938739223f0e29ea8ac092c8f94783cf5b9d7fb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* 
 * debug.c
 *
 * Authors: Dirk Meyer  <dmeyer@tzi.de>
 *          Andreas Büsching <crunchy@tzi.de>
 *
 * $Id: debug.c,v 1.1.1.1 2002/08/28 16:10:51 salmoon Exp $
 */

#include "debug.h"

#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>

#define DCOLOR "_color"
#define DFILE "_file"
#define DSUFFIX "_debug"
#define LEVEL_HEXDUMP 5

char *__debug_color = NULL;
int __debug_level = 0;
FILE *__debug_fd;

void 
debug_init(void) {
  char *env_var = malloc(strlen(DPACKAGE) + strlen(DFILE) + 1);
  const char *env;
  
  /* check debug output file */
  strcpy(env_var, DPACKAGE);
  strcat(env_var, DFILE);

  if ((env = getenv(env_var))) {
    if (__debug_fd && fileno(__debug_fd) != -1) {
      fclose(__debug_fd);
    }
    
    __debug_fd = fopen(env, "a");
    if (!__debug_fd) {
      __debug_fd = stderr;
    }
  } else {
    __debug_fd = stderr;
  }

  free(env_var);

  /* check debug level */
  env_var = malloc(strlen(DPACKAGE) + strlen(DSUFFIX) + 1);
  strcpy(env_var, DPACKAGE);
  strcat(env_var, DSUFFIX);
  
  if ((env = getenv(env_var)))
    if (isdigit(env[0]))
      __debug_level = strtol(env, NULL, 10);
    else
      __debug_level = 1;
  else
    __debug_level = -1;

  free(env_var);

  /* check debug color */
  env_var = malloc(strlen(DPACKAGE) + strlen(DCOLOR) + 1);
  
  strcpy(env_var, DPACKAGE);
  strcat(env_var, DCOLOR);
  
  if (__debug_color) free(__debug_color);
  __debug_color = NULL;
  
  if ((env = getenv(env_var))) {
    if (env[0] != '\0') {
      __debug_color = malloc(4 + strlen(env));
      sprintf(__debug_color, "\033[%sm", env);
    } else 
      __debug_color = malloc(6);
    strcpy(__debug_color, "\033[32m");
  } else {
    __debug_color = NULL;
  }
  
  free(env_var);
}

int
debug_file(char *filename)
{
  if (__debug_fd && fileno(__debug_fd) != -1) {
    fclose(__debug_fd);
  }

  __debug_fd = fopen(filename, "a");
  if (!__debug_fd) {
    perror("fopen:");
    __debug_fd = stderr;
    return 0;
  }

  return 1;
}

int
debug_level(int level)
{
  int tmp = __debug_level;
  
  __debug_level = level;
  
  return tmp;
}

int
debug_level_get(void)
{
  return __debug_level;
}


void
_hexdump (const char *package, const char* file, int line, 
	  const char* function, const char* data, int len)
{
  char buf[17];
  int i;

  if (_use_debug(LEVEL_HEXDUMP)) {
    fprintf (__debug_fd, "%s%s:\033[m %s(%d): %s: data=%p len=%d\n", 
	    __debug_color, package, file, line, function, data, len);
    for (i = 0; data != NULL && i < len; i++) {
      if (i % 16 == 0) 
	fprintf(__debug_fd, "\033[30m%s:\033[m %04x: ", package, i);
      fprintf(__debug_fd, "%02x ", (unsigned int)(unsigned char)data[i]);
      buf[i % 16] = (data[i] >= 32 && data[i] <= 126) ? data[i] : '.';
      buf[i % 16 + 1] = '\0';
      if (i % 4 == 3) fprintf(__debug_fd, " ");
      if (i % 16 == 15) fprintf(__debug_fd, "%s\n", buf);
    }
    if (i % 16 != 0) {
      for (; i % 16 != 0; i++) 
	fprintf (__debug_fd, (i % 4 == 3) ? "    " : "   ");
      fprintf(__debug_fd, "%s\n", buf);
    }
  }
}

void
_hexdump_n (const char *package, const int n, const char* file, int line, 
	  const char* function, const char* data, int len)
{
  char buf[17];
  int i;

  if (_use_debug(n)) {
    fprintf (__debug_fd, "%s%s:\033[m %s(%d): %s: data=%p len=%d\n", 
	    __debug_color, package, file, line, function, data, len);
    for (i = 0; data != NULL && i < len; i++) {
      if (i % 16 == 0) 
	fprintf(__debug_fd, "\033[30m%s:\033[m %04x: ", package, i);
      fprintf(__debug_fd, "%02x ", (unsigned int)(unsigned char)data[i]);
      buf[i % 16] = (data[i] >= 32 && data[i] <= 126) ? data[i] : '.';
      buf[i % 16 + 1] = '\0';
      if (i % 4 == 3) fprintf(__debug_fd, " ");
      if (i % 16 == 15) fprintf(__debug_fd, "%s\n", buf);
    }
    if (i % 16 != 0) {
      for (; i % 16 != 0; i++) 
	fprintf (__debug_fd, (i % 4 == 3) ? "    " : "   ");
      fprintf(__debug_fd, "%s\n", buf);
    }
  }
}


void
_hexdump_text (const char *text,
	       const char *package, const char *file, int line, 
	       const char *function, const char *data, int len)
{
  if (_use_debug(LEVEL_HEXDUMP)) {
    fprintf(__debug_fd, "%s%s: %s(%d): %s: %s\033[m", __debug_color, 
	   package, file, line, function, text);
    _hexdump(package, file, line, function, data, len);
  }
}



void
_error(const char *package, const char *file, int line, 
       const char *function, int fatal, const char *format, ...) 
{
  char foo[2048];
  va_list ap;
  va_start(ap, format);
  
  vsnprintf(foo, sizeof(foo) - strlen(format) - 1, format, ap);
  if (_use_debug(0)) 
    fprintf(__debug_fd, "\033[31m%s: %s(%d): %s: %s\033[m", 
	   package, file, line, function, foo);
  else
    fprintf(__debug_fd, "%s: %s(%d): %s: %s", 
	   package, file, line, function, foo);
  fflush(__debug_fd);

  if (fatal) {
    fprintf(__debug_fd, "\033[31mfatal error -- exit programm\033[m\n");
    exit(1);
  }
  
  va_end(ap);
}

void
_debug(const char *package, const char *file, int line, 
       const char *function, const char *format, ...)
{
  char foo[2048];
  va_list ap;
  va_start(ap, format);
  
  vsnprintf(foo, sizeof(foo) - strlen(format) - 1, format, ap);
  
  if (_use_debug(0)) {
    fprintf(__debug_fd, "%s%s: %s(%d): %s: %s\033[m",
	    ( __debug_color ? __debug_color : ""), 
	    package, file, line, function, foo);
    fflush(__debug_fd);
  }
  
  va_end(ap);
}

void
_debug_n(const char *package, const int n, const char *file, 
	 int line, const char *function, const char *format, ...)
{
  char foo[2048];
  va_list ap;
  va_start(ap, format);
  
  vsnprintf(foo, sizeof(foo) - strlen(format) - 1, format, ap);
  
  if (_use_debug(n)) {
    fprintf(__debug_fd, "%s%s: %s(%d): %s: %s\033[m",
	    ( __debug_color ? __debug_color : ""), 
	    package, file, line, function, foo);
    fflush(__debug_fd);
  }
  
  va_end(ap);
}

void
_octetstr(const char *package, const char *file, int line, 
	  const char *function, const uint8_t *str,
	  const unsigned int len, const char *what)
{
  if (_use_debug(LEVEL_HEXDUMP)) {
    unsigned int i;
    
    fprintf(__debug_fd, "%s%s: %s(%d): %s: ", 
	    package, file, function, line, (what?what:""));
    for (i = 0; i < len; i++) {
      if (i < (len - 1))
	fprintf(__debug_fd, "%03d.", str [i]);
      else
	fprintf(__debug_fd, "%03d", str [i]);
    }
  }
}

int
_use_debug(int level)
{
  if (__debug_level == -1) return 0;
  
  if (level <= __debug_level) {
    return 1;
  }
    
  return 0;
}

/* end of debug.c */