File: debug.c

package info (click to toggle)
everybuddy 0.2.1beta6-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,936 kB
  • ctags: 5,627
  • sloc: ansic: 50,783; sh: 8,559; yacc: 191; makefile: 182; perl: 97; lex: 50
file content (200 lines) | stat: -rw-r--r-- 3,237 bytes parent folder | download | duplicates (2)
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
/*
 * Debugging library routines
 */
#if defined(ENABLE_LIBYAHOO_DEBUG)

#if defined(HAVE_CONFIG_H)
#include "config.h"
#else
#define HAVE_STDARG_H
#define HAVE_STRING_H
#endif

#include "debug.h"

#include <stdlib.h>
#include <stdio.h>
#if defined(HAVE_STDARG_H)
#include <stdarg.h>
#endif

#if defined(HAVE_STRING_H)
#include <string.h>
#elif defined(HAVE_STRINGS_H)
#include <strings.h>
#endif

static FILE *debugfile = stdout;
static char *DBG_EnabledKeys[128] =
{NULL};							/* should be more than enough */
static int DBG_MaxEnabledKey = -1;

/* these routines currently do a really inneficient linear search, 
   but it's not like they need to be really fast - they are just for
   debugging purposes */

int DBG_Open(char *file)
{
	if (debugfile != stdout)
	{
		fclose(debugfile);
		debugfile = NULL;
	}

	if (!file)
	{
		fprintf(stderr, "DBG_Open Error: File is null, using stdout.\n");
		debugfile = stdout;
		return 1;
	}

	if ((debugfile = fopen(file, "a")) == NULL)
	{
		fprintf(stderr, "DBG_Open Error: Couldn't open output file.\n");
		fprintf(stderr, "                Falling back to stdout.\n");
		debugfile = stdout;
		return 1;
	}

	return 0;
}

int DBG_Disable(char *key)
{
	int i;

	if (!key)
	{
		fprintf(stderr, "DBG_Disable Error: key is null.\n");
		return 1;
	}
	for (i = 0; i <= DBG_MaxEnabledKey; i++)
	{
		if (DBG_EnabledKeys[i] && !strcmp(DBG_EnabledKeys[i], key))
		{
			free(DBG_EnabledKeys[i]);
			DBG_EnabledKeys[i] = NULL;
			return 0;
		}
	}
	return 1;
}

int DBG_Enable(char *key)
{
	int i;

	if (!key)
	{
		fprintf(stderr, "DBG_Enable Error: key is null.\n");
		return 1;
	}
	for (i = 0; i <= DBG_MaxEnabledKey; i++)
	{
		if (DBG_EnabledKeys[i] && !strcmp(DBG_EnabledKeys[i], key))
		{
			return 0;			/* already enabled */
		}
	}
	DBG_EnabledKeys[DBG_MaxEnabledKey + 1] = NULL;
	for (i = 0; i <= DBG_MaxEnabledKey + 1; i++)
	{
		if (!DBG_EnabledKeys[i])
		{
			DBG_EnabledKeys[i] = strdup(key);
			if (i > DBG_MaxEnabledKey)
			{
				DBG_MaxEnabledKey = i;
			}
			return 0;			/* already enabled */
		}
	}
	return 0;
}

int DBG_Print(char *key, char *format,...)
{
	va_list args;

	if (!key)
	{
		fprintf(stderr, "DBG_Print Error: key is null.\n");
		return 1;
	}
	if (!format)
	{
		fprintf(stderr, "DBG_Print Error: format is null.\n");
		return 1;
	}

	if (DBG_IsEnabled(key) || DBG_IsEnabled("all"))
	{
		va_start(args, format);
		vfprintf(debugfile, format, args);
		va_end(args);

		fflush(debugfile);
	}
	return 0;
}

int DBG_Close(void)
{
	if (debugfile && debugfile != stdout)
	{
		fclose(debugfile);
		debugfile = NULL;
	}
	return 0;
}

int DBG_IsEnabled(char *key)
{
	int i;

	for (i = 0; i <= DBG_MaxEnabledKey; i++)
	{
		if (!strcmp(DBG_EnabledKeys[i], key))
		{
			return 1;			/* is enabled */
		}
	}
	return 0;
}

#else

/* Debugging is not enabled, so do an inline routines that do nothing */
/* Hopefully the compiler will completely optimize these routines away */

inline int DBG_Open(char *file)
{
	return 0;
}

inline int DBG_Disable(char *key)
{
	return 0;
}

inline int DBG_Enable(char *key)
{
	return 0;
}

inline int DBG_Print(char *key, char *format,...)
{
	return 0;
}

inline int DBG_Close(void)
{
	return 0;
}

inline int DBG_IsEnabled(char *key)
{
	return 0;
}

#endif