-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdiagnostics.c
230 lines (195 loc) · 5.08 KB
/
diagnostics.c
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
//
// diagnostics.c
// cctools libstuff
//
// Created by Michael Trent on 6/17/20.
//
// For more information on the CC_LOG_DIAGNOSTICS file format, see also
// rdar://64357409 and the clang's lib/Frontend/LogDiagnosticPrinter.cpp.
#include "diagnostics.h"
#include "stuff/errors.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/*
* diagnostics_state is a ternary value: -1 if uninitialized, 1 if diagnostic
* logging is enabled, 0 if not.
*/
static int diagnostics_state = -1;
struct diagnostics_info {
const char* log_file;
const char* main_file;
const char* tool;
const char* args;
};
struct diagnostic {
const char* level;
char* message;
};
/*
* diagnostics is the array of diagnostic messages logged during program
* operation. The count of this array is stored in ndiagnostic.
*/
static struct diagnostic* diagnostics;
static int ndiagnostic;
static struct diagnostics_info info;
static void diagnostics_atexit(void);
/*
* diagnostic_level_name is a utility for converting enum diagnostic_level to
* a well-known string value.
*/
static const char* diagnostic_level_name(enum diagnostic_level level)
{
const char* names[] = {
"warning",
"error",
"fatal error",
};
int nname = sizeof(names) / sizeof(*names);
if (level >= 0 && level < nname)
return names[level];
return names[ERROR];
}
void diagnostics_enable(enum bool enable)
{
if (diagnostics_state == -1) {
atexit(diagnostics_atexit);
}
diagnostics_state = enable ? 1 : 0;
}
void diagnostics_output(const char* logfile)
{
if (info.log_file)
free((void*)info.log_file);
info.log_file = logfile ? strdup(logfile) : NULL;
}
enum bool diagnostics_enabled(void)
{
return diagnostics_state == 1 ? TRUE : FALSE;
}
void diagnostics_log_args(int argc, char** argv)
{
char* buf;
size_t len;
FILE* stream;
if (argc > 0)
info.tool = argv[0];
if (info.args) {
free((void*)info.args);
info.args = NULL;
}
stream = open_memstream(&buf, &len);
if (stream) {
for (int i = 1; i < argc; ++i) {
fprintf(stream, "%s%s", i == 1 ? "" : " ", argv[i]);
}
fclose(stream);
info.args = strdup(buf);
free(buf);
}
}
void diagnostics_log_msg(enum diagnostic_level level, const char* message)
{
diagnostics = reallocf(diagnostics, sizeof(*diagnostics) * (ndiagnostic+1));
struct diagnostic* d = &diagnostics[ndiagnostic++];
d->level = diagnostic_level_name(level);
d->message = strdup(message);
}
void diagnostics_write(void)
{
char* buf;
size_t len;
FILE* stream;
int fd;
/*
* do nothing if nothing to say. this prevents tools from writing noise
* into the CC_LOG_DIAGNOSTICS file on exit.
*/
if (ndiagnostic == 0)
return;
/*
* write XML data to a dynamic in-memory buffer.
*/
stream = open_memstream(&buf, &len);
if (!stream)
return;
fprintf(stream, "<dict>\n");
if (info.tool) {
fprintf(stream, " <key>tool</key>\n");
fprintf(stream, " <string>%s</string>\n", info.tool);
}
else if (progname) {
fprintf(stream, " <key>tool</key>\n");
fprintf(stream, " <string>%s</string>\n", progname);
}
if (info.args) {
fprintf(stream, " <key>args</key>\n");
fprintf(stream, " <string>%s</string>\n", info.args);
}
fprintf(stream, " <key>diagnostics</key>\n");
fprintf(stream, " <array>\n");
for (int i = 0; i < ndiagnostic; ++i) {
struct diagnostic* d = &diagnostics[i];
fprintf(stream, " <dict>\n");
fprintf(stream, " <key>level</key>\n");
fprintf(stream, " <string>%s</string>\n", d->level);
/*
* ID is not meaningful to cctools.
*
*fprintf(stream, " <key>ID</key>\n");
*fprintf(stream, " <integer>%d</integer>\n", 0);
*/
if (d->message) {
fprintf(stream, " <key>message</key>\n");
fprintf(stream, " <string>%s</string>\n", d->message);
}
fprintf(stream, " </dict>\n");
}
fprintf(stream, " </array>\n");
fprintf(stream, "</dict>\n");
fclose(stream);
/*
* open the CC_LOG_DIAGNOSTICS file. If no file was specified, write to
* the stderr filehandle.
*/
if (info.log_file) {
fd = open(info.log_file, O_WRONLY | O_APPEND | O_CREAT, 0644);
if (-1 == fd) {
fprintf(stderr, "error: cannot open file at %s: %s\n",
info.log_file, strerror(errno));
free(buf);
return;
}
}
else {
fd = STDERR_FILENO;
}
/*
* write the XML data to the log file. By writing the entire contents to
* file in a single syscall, we ensure the write is atomic. (Also, because
* we need the write to be atomic, we will use the write syscall directly
* rather than libstuff's write64.)
*/
write(fd, buf, len);
/*
* clean up
*/
if (fd != STDERR_FILENO)
close(fd);
free(buf);
for (int i = 0; i < ndiagnostic; ++i) {
struct diagnostic* d = &diagnostics[i];
free(d->message);
}
free(diagnostics);
diagnostics = NULL;
ndiagnostic = 0;
}
void diagnostics_atexit(void)
{
if (diagnostics_enabled())
diagnostics_write();
}