-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutility.cc
81 lines (61 loc) · 1.5 KB
/
utility.cc
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
/* File: utiliy.cc
* ---------------
* Implementation of simple printing functions to report failures or
* debugging information triggered by keys.
*/
#include "utility.h"
#include <stdarg.h>
#include "list.h"
static List<const char*> debugKeys;
static const int BufferSize = 2048;
void Failure(const char *format, ...)
{
va_list args;
char errbuf[BufferSize];
va_start(args, format);
vsprintf(errbuf, format, args);
va_end(args);
fflush(stdout);
fprintf(stderr,"\n*** Failure: %s\n\n", errbuf);
abort();
}
int IndexOf(const char *key)
{
for (int i = 0; i < debugKeys.NumElements(); i++)
if (!strcmp(debugKeys.Nth(i), key)) return i;
return -1;
}
bool IsDebugOn(const char *key)
{
return (IndexOf(key) != -1);
}
void SetDebugForKey(const char *key, bool value)
{
int k = IndexOf(key);
if (!value && k != -1)
debugKeys.RemoveAt(k);
else if (value && k == -1)
debugKeys.Append(key);
}
void PrintDebug(const char *key, const char *format, ...)
{
va_list args;
char buf[BufferSize];
if (!IsDebugOn(key))
return;
va_start(args, format);
vsprintf(buf, format, args);
va_end(args);
printf("+++ (%s): %s%s", key, buf, buf[strlen(buf)-1] != '\n'? "\n" : "");
}
void ParseCommandLine(int argc, char *argv[])
{
if (argc == 1)
return;
if (strcmp(argv[1], "-d") != 0) { // first arg is not -d
printf("Usage: -d <debug-key-1> <debug-key-2> ... \n");
exit(2);
}
for (int i = 2; i < argc; i++)
SetDebugForKey(argv[i], true);
}